Process an HTML file to localize external JS and CSS references.
(html_file_path, js_dir, css_dir, skip_files=None)
| 76 | return filename |
| 77 | |
| 78 | def process_html_file(html_file_path, js_dir, css_dir, skip_files=None): |
| 79 | """Process an HTML file to localize external JS and CSS references.""" |
| 80 | with open(html_file_path, "r", encoding="utf-8") as f: |
| 81 | lines = f.readlines() |
| 82 | |
| 83 | modified = False |
| 84 | new_lines = [] |
| 85 | |
| 86 | for line in lines: |
| 87 | soup = BeautifulSoup(line, "html.parser") |
| 88 | script = soup.find("script", src=True) |
| 89 | link = soup.find("link", rel="stylesheet", href=True) |
| 90 | |
| 91 | if script and is_external_url(script["src"]): |
| 92 | src = script["src"] |
| 93 | if "MathJax" in src: |
| 94 | src = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" |
| 95 | filename = os.path.basename(urlparse(src).path) |
| 96 | version = extract_version_from_url(src) |
| 97 | if version and not re.search(r'\d+\.\d+(\.\d+)?', filename): |
| 98 | filename = add_version_to_filename(filename, version) |
| 99 | local_path = os.path.join(js_dir, filename) |
| 100 | download_file(src, local_path) |
| 101 | script["src"] = f"js/{filename}" # Relative path to js/ folder |
| 102 | new_lines.append(str(script) + "\n") |
| 103 | modified = True |
| 104 | elif link and is_external_url(link["href"]): |
| 105 | href = link["href"] |
| 106 | if "MathJax" in href: |
| 107 | href = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" |
| 108 | filename = os.path.basename(urlparse(href).path) |
| 109 | if filename not in skip_files: |
| 110 | version = extract_version_from_url(href) |
| 111 | if version and not re.search(r'\d+\.\d+(\.\d+)?', filename): |
| 112 | filename = add_version_to_filename(filename, version) |
| 113 | local_path = os.path.join(css_dir, filename) |
| 114 | download_file(href, local_path) |
| 115 | link["href"] = f"css/{filename}" # Relative path to css/ folder |
| 116 | new_lines.append(str(link) + "\n") |
| 117 | modified = True |
| 118 | else: |
| 119 | new_lines.append(line) |
| 120 | |
| 121 | if modified: |
| 122 | with open(html_file_path, "w", encoding="utf-8") as f: |
| 123 | f.writelines(new_lines) |
| 124 | print(f"Updated {html_file_path}") |
| 125 | |
| 126 | def main(): |
| 127 | parser = argparse.ArgumentParser(description='Process HTML files to localize external JS and CSS references.') |
no test coverage detected
searching dependent graphs…