from pathlib import Path import re,html src=Path("/mnt/data/Pasted markdown.md") out=Path("/mnt/data/beauno-style-optimized.css") text=src.read_text(encoding="utf-8") # Convert markdown-escaped paste back to normal CSS characters. for old,new in [(r"\*","*"),(r"\#","#"),(r"\-","-"),(r"\:",":"),(r"\.","."),(r"\~","~"),]:text=text.replace(old,new) text=html.unescape(text) # Modify only @font-face blocks whose family is IRANSansXFaNum:# 1) add font-display:swap # 2) put woff2 before woff in src block_re=re.compile(r"@font-face\s*\{.*?\}",re.S) def optimize_block(match):block=match.group(0) if not re.search(r"font-family\s*:\s*IRANSansXFaNum\s*;",block,re.I):return block # Add font-display:swap if missing. if not re.search(r"font-display\s*:",block,re.I):m=re.search(r"(font-style\s*:\s*[^;]+;)",block,re.I) if m:block=block[:m.end()]+"\n    font-display: swap;"+block[m.end():] else:block=block.replace("{","{\n    font-display: swap;",1) # Reorder the common two-source src declaration:woff first,woff2 second ->woff2 first. src_re=re.compile(r"src\s*:\s*" r"url\((['\"]?)([^)'\"]*?/woff/[^)'\"]+\.woff)\1\)\s*format\((['\"]?)woff\3\)\s*,\s*" r"url\((['\"]?)([^)'\"]*?/woff2/[^)'\"]+\.woff2)\4\)\s*format\((['\"]?)woff2\6\)\s*;",re.I | re.S,) def swap_sources(sm):woff=sm.group(2) woff2=sm.group(5) return ("src: url('../../../../themes/woodmart-child/"+woff2+"') format('woff2'),\n" "         url('../../../../themes/woodmart-child/"+woff+"') format('woff');") block=src_re.sub(swap_sources,block) return block text=block_re.sub(optimize_block,text) out.write_text(text,encoding="utf-8") # Verification summary target_blocks=[b.group(0) for b in block_re.finditer(text) if re.search(r"font-family\s*:\s*IRANSansXFaNum\s*;",b.group(0),re.I)] checks={"IRANSansXFaNum blocks":len(target_blocks),"all have font-display: swap":all("font-display: swap;" in b for b in target_blocks),"all woff2 listed before woff when both exist":all(("/woff2/" not in b or "/woff/" not in b or b.find("/woff2/") < b.find("/woff/")) for b in target_blocks),}print(checks) print(f"Created: {out}")