|
| 1 | +""" |
| 2 | +Scan docs/_site-offline/ for any https://docs.twinbasic.com/<path> |
| 3 | +reference outside of <code> / <pre> blocks. Exit 1 if any found, |
| 4 | +0 otherwise. |
| 5 | +
|
| 6 | +Run by docs/check.bat after the offline lychee pass. After |
| 7 | +_plugins/offlinify.rb's SEO-block strip, no live-site references |
| 8 | +should remain except: |
| 9 | +
|
| 10 | + * Sample URLs inside <code> / <pre> blocks (tutorial code that |
| 11 | + legitimately shows live URLs as data, e.g. the VBRUN.Hyperlink |
| 12 | + `NavigateTo "https://docs.twinbasic.com/"` example). Skipped |
| 13 | + via the same code-block shape offlinify uses for its URL |
| 14 | + rewrite. |
| 15 | + * The bare root URL `https://docs.twinbasic.com` or |
| 16 | + `https://docs.twinbasic.com/` -- intentional "go to the live |
| 17 | + docs site" links (e.g. the Documentation entry in the FAQ |
| 18 | + resource list). Skipped via the tail check below. |
| 19 | +
|
| 20 | +Anything deeper (`https://docs.twinbasic.com/tB/Core/Const`, |
| 21 | +`https://docs.twinbasic.comi`, ...) is flagged: in the offline |
| 22 | +copy those navigate back to the live site, undermining the local |
| 23 | +read; in source they should be a relative link or a /tB/... |
| 24 | +permalink that resolves locally. |
| 25 | +
|
| 26 | +Run from anywhere: |
| 27 | + python scripts/check_offline_live_links.py |
| 28 | +""" |
| 29 | + |
| 30 | +import re |
| 31 | +import sys |
| 32 | +from pathlib import Path |
| 33 | + |
| 34 | +SCRIPT_DIR = Path(__file__).resolve().parent |
| 35 | +REPO_ROOT = SCRIPT_DIR.parent |
| 36 | +OFFLINE_TREE = REPO_ROOT / "docs" / "_site-offline" |
| 37 | + |
| 38 | +# Matches a <code>...</code> or <pre>...</pre> block. Same shape as |
| 39 | +# _plugins/offlinify.rb CODE_BLOCK_RE so sample URLs in tutorial |
| 40 | +# code are skipped here too. |
| 41 | +CODE_BLOCK_RE = re.compile(r"<(code|pre)\b[^>]*>.*?</\1>", re.DOTALL) |
| 42 | + |
| 43 | +# Captures the trailing path/typo characters after the domain. An |
| 44 | +# empty tail or `/` means the bare root URL (intentional). Anything |
| 45 | +# else is a deep link or a typo (`.comi`, `.com/tB/...`). |
| 46 | +LIVE_LINK_RE = re.compile(r"https://docs\.twinbasic\.com(?P<tail>[^\s\"'<>]*)") |
| 47 | + |
| 48 | + |
| 49 | +def main() -> int: |
| 50 | + if not OFFLINE_TREE.is_dir(): |
| 51 | + print( |
| 52 | + f"_site-offline/ not found at {OFFLINE_TREE} -- run docs/build.bat first." |
| 53 | + ) |
| 54 | + return 2 |
| 55 | + |
| 56 | + hits = [] |
| 57 | + for html in sorted(OFFLINE_TREE.rglob("*.html")): |
| 58 | + content = html.read_text(encoding="utf-8") |
| 59 | + link_matches = list(LIVE_LINK_RE.finditer(content)) |
| 60 | + if not link_matches: |
| 61 | + continue |
| 62 | + code_ranges = [(m.start(), m.end()) for m in CODE_BLOCK_RE.finditer(content)] |
| 63 | + for m in link_matches: |
| 64 | + tail = m.group("tail") |
| 65 | + if tail == "" or tail == "/": |
| 66 | + continue |
| 67 | + if any(s <= m.start() < e for s, e in code_ranges): |
| 68 | + continue |
| 69 | + line_num = content.count("\n", 0, m.start()) + 1 |
| 70 | + start = max(0, m.start() - 60) |
| 71 | + end = min(len(content), m.start() + 80) |
| 72 | + snippet = re.sub(r"[\r\n]+", " ", content[start:end]) |
| 73 | + hits.append((html, line_num, snippet)) |
| 74 | + |
| 75 | + if hits: |
| 76 | + print( |
| 77 | + f"FAIL: {len(hits)} reference(s) to docs.twinbasic.com in " |
| 78 | + f"_site-offline/ outside code blocks:" |
| 79 | + ) |
| 80 | + for path, line_num, snippet in hits: |
| 81 | + try: |
| 82 | + rel = path.relative_to(REPO_ROOT) |
| 83 | + except ValueError: |
| 84 | + rel = path |
| 85 | + print(f" {rel}:{line_num}: ...{snippet}...") |
| 86 | + print() |
| 87 | + print( |
| 88 | + "Update the source markdown to use a relative link or /tB/... " |
| 89 | + "permalink instead." |
| 90 | + ) |
| 91 | + return 1 |
| 92 | + |
| 93 | + return 0 |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + sys.exit(main()) |
0 commit comments