import os import re def fix_svg_file(filepath): with open(filepath, 'r', encoding='utf-8') as f: content = f.read() # Find style block style_match = re.search(r'', content, re.DOTALL) if not style_match: print(f"Skipping {os.path.basename(filepath)}: No style block found") return False style_content = style_match.group(1) # Parse class -> fill mappings # Matches .classname { fill: #color; } # Also handles formatting variations mappings = {} # Regex for class definition: .name { ... } # We look for fill: ... inside class_pattern = re.compile(r'\.([\w-]+)\s*\{([^}]+)\}') for match in class_pattern.finditer(style_content): class_name = match.group(1) body = match.group(2) # Extract fill color fill_match = re.search(r'fill:\s*(#[0-9a-fA-F]{3,6})', body) if fill_match: mappings[class_name] = fill_match.group(1) if not mappings: print(f"Skipping {os.path.basename(filepath)}: No fill mappings found in style") return False # Remove style block new_content = re.sub(r'', '', content, flags=re.DOTALL) # Replace class="name" with fill="color" # Note: We keep other attributes. If class is the only one, we replace it. # If other attributes exist, we should append fill and remove class? # Simplest approach: Replace `class="name"` with `fill="color"` changed = False for cls, color in mappings.items(): # Match class="name" or class='name' # Be careful not to replace partial matches (e.g. class="name-suffix") pattern = re.compile(r'class=["\']' + re.escape(cls) + r'["\']') if pattern.search(new_content): new_content = pattern.sub(f'fill="{color}"', new_content) changed = True if changed: with open(filepath, 'w', encoding='utf-8') as f: f.write(new_content) print(f"Fixed {os.path.basename(filepath)}") return True else: print(f"No class usages found for {os.path.basename(filepath)}") return False def main(): target_dir = '/Users/maidong/Desktop/zyc/qiyuan_gitea/rtc_prd/airhub_app/assets/www/icons' count = 0 for filename in os.listdir(target_dir): if filename.endswith('.svg'): if fix_svg_file(os.path.join(target_dir, filename)): count += 1 print(f"Total files fixed: {count}") if __name__ == '__main__': main()