| def apply_search_replace_changes(original_content: str, changes_text: str) -> str: | |
| """Apply search/replace changes to content (HTML, Python, etc.)""" | |
| if not changes_text.strip(): | |
| return original_content | |
| blocks=[] | |
| current_block="" | |
| lines=changes_text.split('\n') | |
| for line in lines: | |
| if line.strip()==SEARCH_START: | |
| if current_block.strip(): blocks.append(current_block.strip()) | |
| current_block=line+"\n" | |
| elif line.strip()==REPLACE_END: | |
| current_block+=line+"\n" | |
| blocks.append(current_block.strip()) | |
| current_block="" | |
| else: | |
| current_block+=line+"\n" | |
| if current_block.strip(): blocks.append(current_block.strip()) | |
| modified_content=original_content | |
| for block in blocks: | |
| lines=block.split('\n') | |
| search_lines=[]; replace_lines=[] | |
| in_search=False; in_replace=False | |
| for ln in lines: | |
| if ln.strip()==SEARCH_START: | |
| in_search=True; in_replace=False | |
| elif ln.strip()==DIVIDER: | |
| in_search=False; in_replace=True | |
| elif ln.strip()==REPLACE_END: | |
| in_replace=False | |
| elif in_search: | |
| search_lines.append(ln) | |
| elif in_replace: | |
| replace_lines.append(ln) | |
| if search_lines: | |
| search_text='\n'.join(search_lines).strip() | |
| replace_text='\n'.join(replace_lines).strip() | |
| if search_text in modified_content: | |
| modified_content=modified_content.replace(search_text,replace_text) | |
| else: | |
| print(f"Warning: Search text not found in content: {search_text[:100]}...") | |
| return modified_content | |
| def apply_transformers_js_search_replace_changes(original_formatted_content: str, changes_text: str) -> str: | |
| """Apply search/replace changes to transformers.js formatted content (three files)""" | |
| if not changes_text.strip(): | |
| return original_formatted_content | |
| files=parse_transformers_js_output(original_formatted_content) | |
| blocks=[]; current_block="" | |
| lines=changes_text.split('\n') | |
| for line in lines: | |
| if line.strip()==SEARCH_START: | |
| if current_block.strip(): blocks.append(current_block.strip()) | |
| current_block=line+"\n" | |
| elif line.strip()==REPLACE_END: | |
| current_block+=line+"\n" | |
| blocks.append(current_block.strip()) | |
| current_block="" | |
| else: | |
| current_block+=line+"\n" | |
| if current_block.strip(): blocks.append(current_block.strip()) | |
| for block in blocks: | |
| lines=block.split('\n') | |
| search_lines=[]; replace_lines=[] | |
| in_search=False; in_replace=False; target_file=None | |
| for ln in lines: | |
| if ln.strip()==SEARCH_START: | |
| in_search=True; in_replace=False | |
| elif ln.strip()==DIVIDER: | |
| in_search=False; in_replace=True | |
| elif ln.strip()==REPLACE_END: | |
| in_replace=False | |
| elif in_search: | |
| search_lines.append(ln) | |
| elif in_replace: | |
| replace_lines.append(ln) | |
| if search_lines: | |
| search_text='\n'.join(search_lines).strip() | |
| replace_text='\n'.join(replace_lines).strip() | |
| if search_text in files['index.html']: | |
| target_file='index.html' | |
| elif search_text in files['index.js']: | |
| target_file='index.js' | |
| elif search_text in files['style.css']: | |
| target_file='style.css' | |
| if target_file and search_text in files[target_file]: | |
| files[target_file]=files[target_file].replace(search_text,replace_text) | |
| else: | |
| print(f"Warning: Search text not found in any transformers.js file: {search_text[:100]}...") | |
| return format_transformers_js_output(files) | |