|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Reject loopback URLs in generated API reference links and search entries.""" |
| 3 | +from html.parser import HTMLParser |
| 4 | +from pathlib import Path |
| 5 | +import re |
| 6 | +import sys |
| 7 | +from urllib.parse import urlsplit |
| 8 | + |
| 9 | + |
| 10 | +def is_loopback(url): |
| 11 | + return urlsplit(url.replace("\\/", "/")).hostname in { |
| 12 | + "localhost", "127.0.0.1", "0.0.0.0", "::1" |
| 13 | + } |
| 14 | + |
| 15 | + |
| 16 | +class ReferenceLinks(HTMLParser): |
| 17 | + def __init__(self): |
| 18 | + super().__init__() |
| 19 | + self.invalid = [] |
| 20 | + |
| 21 | + def handle_starttag(self, tag, attrs): |
| 22 | + for name, value in attrs: |
| 23 | + if name == "href" and value and is_loopback(value): |
| 24 | + self.invalid.append((self.getpos()[0], value)) |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + root = Path(sys.argv[1] if len(sys.argv) > 1 else "output") |
| 29 | + files = [p for p in root.rglob("*.html") if "documentation" in p.relative_to(root).parts] |
| 30 | + if not files: |
| 31 | + sys.exit("No generated API documentation found in " + str(root)) |
| 32 | + errors = [] |
| 33 | + for path in files: |
| 34 | + parser = ReferenceLinks() |
| 35 | + parser.feed(path.read_text(encoding="utf-8")) |
| 36 | + errors.extend("{}:{}: {}".format(path, line, url) for line, url in parser.invalid) |
| 37 | + search_files = list(root.rglob("tipuesearch_content.js")) |
| 38 | + for path in search_files: |
| 39 | + text = path.read_text(encoding="utf-8") |
| 40 | + for match in re.finditer(r'"url"\s*:\s*"((?:\\.|[^"\\])*)"', text): |
| 41 | + if is_loopback(match.group(1)): |
| 42 | + errors.append("{}: {}".format(path, match.group(1))) |
| 43 | + if errors: |
| 44 | + sys.exit("Loopback documentation URLs:\n" + "\n".join(errors)) |
| 45 | + print("Checked {} API pages and {} search indexes: no loopback URLs".format(len(files), len(search_files))) |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == "__main__": |
| 49 | + main() |
0 commit comments