"""Scrub RTD-crawled HTML for self-hosted serving (spec ยง6): strip RTD platform JS, analytics, ad units, and the RTD flyout; localize absolute self/RTD URLs; inject our picker bundle.""" from __future__ import annotations import re STRIP_SCRIPTS = re.compile( r"]*(?:readthedocs|googletagmanager|google-analytics|gtag/js|ethicalads)[^>]*>\s*", re.I, ) STRIP_INLINE = re.compile( r"]*>(?:(?!).)*?(?:READTHEDOCS_DATA|gtag\(|GoogleAnalyticsObject|ethicalads)(?:(?!).)*?", re.I | re.S, ) STRIP_DIVS = re.compile( r']*(?:id="rtd-footer-container"|id="ethical-ad-placement"|class="[^"]*(?:rst-versions|ethical-sidebar)[^"]*")[^>]*>.*?', re.I | re.S, ) ABS_SELF = re.compile(r'(href|src)="https?://docs\.vyos\.io(/[^"]*)"') ABS_RTD = re.compile(r'(href|src)="https?://[a-z0-9.-]*readthedocs\.(?:io|org)(/[^"]*)"') PICKER = ('
' '' '') def scrub_html(html: str, slug: str) -> str: out = STRIP_SCRIPTS.sub("", html) out = STRIP_INLINE.sub("", out) out = STRIP_DIVS.sub("", out) out = ABS_SELF.sub(r'\1="\2"', out) out = ABS_RTD.sub(r'\1="\2"', out) if "vyos-version-picker" not in out: out = out.replace("", PICKER.format(slug=slug) + "", 1) return out