Skip to content

Instantly share code, notes, and snippets.

@kadiwa4
Last active January 8, 2024 22:35
Show Gist options
  • Save kadiwa4/8e81b820217fdb8d1fb70e2108d1a58d to your computer and use it in GitHub Desktop.
Save kadiwa4/8e81b820217fdb8d1fb70e2108d1a58d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Workaround for https://github.com/rust-lang/rust/issues/98769
# WebKit issue: https://bugs.webkit.org/show_bug.cgi?id=249887
#
# Modifies a lot of HTML files in <sysroot>/share/doc/rust/html
# to remove their <link rel="preload" as="font" ...> elements.
import os
from pathlib import Path
import re
import sys
if len(sys.argv) != 2:
print(f"""
Usage:
{sys.argv[0]} <path-to-rust-sysroot>
Example:
{sys.argv[0]} ~/.rustup/toolchains/stable-aarch64-unknown-linux-gnu
""")
sys.exit(1)
PATTERN = re.compile('<link rel="preload" as="font"[^>]+>')
def remove_font_preload(path):
with path.open("r+", encoding="utf-8") as file:
contents = file.read()
file.seek(0)
file.truncate()
file.write(PATTERN.sub("", contents))
def iter_dir(path):
with os.scandir(path) as entries:
for entry in entries:
if entry.is_dir(follow_symlinks=False):
iter_dir(entry.path)
if entry.is_file(follow_symlinks=False) and entry.name.endswith(".html"):
remove_font_preload(Path(entry.path))
html_root = Path(sys.argv[1], "share", "doc", "rust", "html")
for file_name in ("settings.html", "help.html"):
remove_font_preload(Path(html_root, file_name))
src_dir = Path(html_root, "src")
for crate in os.listdir(src_dir):
iter_dir(Path(html_root, crate))
iter_dir(src_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment