#!/usr/bin/env python3 """Extract inline block with a tag. This allows browsers to cache CSS across page navigations and dramatically reduces HTML payload size. Usage: python3 extract-inline-css.py """ import glob import re from pathlib import Path def extract_css_from_template(html_path, css_output_dir, css_url_prefix): """Extract ', html, re.DOTALL | re.IGNORECASE )) if not style_blocks: return html # No inline CSS to extract # Extract all CSS content css_parts = [] for m in style_blocks: css = m.group(1).strip() if css: css_parts.append(css) if not css_parts: return html # Write external CSS file name = Path(html_path).stem css_filename = f"{name}.css" css_path = Path(css_output_dir) / css_filename css_path.parent.mkdir(parents=True, exist_ok=True) combined_css = '\n\n'.join(css_parts) with open(css_path, 'w', encoding='utf-8') as f: f.write(f"/* Extracted from {Path(html_path).name} */\n\n") f.write(combined_css) f.write('\n') # Replace first