|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Check the built mkdocs site for visible "```" sequences by stripping HTML tags |
| 4 | +and reporting any matches along with filenames/line numbers. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + ./scripts/check_site_for_backticks.py [--build] |
| 8 | +
|
| 9 | +Options: |
| 10 | + --build : Run `mkdocs build --clean` before checking. Defaults to off. |
| 11 | +
|
| 12 | +Exit codes: |
| 13 | + 0: No matches found (clean) |
| 14 | + 1: Matches found |
| 15 | + 2: Error during build or processing |
| 16 | +""" |
| 17 | + |
| 18 | +import argparse |
| 19 | +import os |
| 20 | +import re |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | + |
| 24 | + |
| 25 | +def build_site(): |
| 26 | + try: |
| 27 | + subprocess.check_call(["mkdocs", "build", "--clean"]) |
| 28 | + return True |
| 29 | + except Exception as e: |
| 30 | + print(f"ERROR: mkdocs build failed: {e}") |
| 31 | + return False |
| 32 | + |
| 33 | + |
| 34 | +def strip_tags_and_find(file_path, needle='```'): |
| 35 | + """Return list of (line_no, line_text) where needle appears in the stripped text.""" |
| 36 | + out = [] |
| 37 | + try: |
| 38 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 39 | + html = f.read() |
| 40 | + except Exception as e: |
| 41 | + print(f"ERROR: Could not read {file_path}: {e}") |
| 42 | + return out |
| 43 | + # Replace tags |
| 44 | + text = re.sub(r'<[^>]*>', '', html) |
| 45 | + for i, line in enumerate(text.splitlines(), 1): |
| 46 | + if '```' in line: |
| 47 | + out.append((i, line)) |
| 48 | + return out |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + parser = argparse.ArgumentParser(description='Build and check site HTML for visible ``` sequences') |
| 53 | + parser.add_argument('--build', action='store_true', help='Run mkdocs build --clean before checking') |
| 54 | + parser.add_argument('--site-dir', default='site', help='Directory of built site (default: site)') |
| 55 | + args = parser.parse_args() |
| 56 | + |
| 57 | + if args.build: |
| 58 | + print('Building site with: mkdocs build --clean') |
| 59 | + if not build_site(): |
| 60 | + sys.exit(2) |
| 61 | + |
| 62 | + if not os.path.isdir(args.site_dir): |
| 63 | + print(f"ERROR: site directory '{args.site_dir}' not found. Run mkdocs build first or use --build") |
| 64 | + sys.exit(2) |
| 65 | + |
| 66 | + matches = [] |
| 67 | + for root, dirs, files in os.walk(args.site_dir): |
| 68 | + for name in files: |
| 69 | + if name.endswith('.html'): |
| 70 | + path = os.path.join(root, name) |
| 71 | + hits = strip_tags_and_find(path) |
| 72 | + if hits: |
| 73 | + matches.append((path, hits)) |
| 74 | + |
| 75 | + if not matches: |
| 76 | + print('OK: No visible triple backticks found in generated site HTML.') |
| 77 | + sys.exit(0) |
| 78 | + |
| 79 | + print('ERROR: Found visible triple backticks in generated HTML:') |
| 80 | + for path, hits in matches: |
| 81 | + print('\nFile:', path) |
| 82 | + for line_no, line in hits: |
| 83 | + preview = (line.strip()[:120] + '...') if len(line.strip()) > 120 else line.strip() |
| 84 | + print(f' line {line_no}: {preview}') |
| 85 | + print('\nPlease inspect associated Markdown files in docs/ to remove stray fences or fix indentation (e.g., dedent code blocks in admonitions).') |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + main() |
0 commit comments