postprocess_stagit_site.py (1833B)
1 #!/usr/bin/env python3 2 import re 3 import sys 4 from pathlib import Path 5 6 7 TOPBAR = """ 8 <div class="topbar"> 9 <div class="wrap"> 10 <a class="brand repo-link" href="/"><span class="mark" aria-label="iuna"><svg viewBox="0 0 32 32" aria-hidden="true" focusable="false"><circle class="mark-dot" cx="9.4" cy="7.6" r="2.8"></circle><path class="mark-loop" d="M9.4 13v7.1c0 3.7 2.9 6.4 6.6 6.4s6.6-2.7 6.6-6.4V13"></path></svg></span><span>iuna</span></a> 11 <nav aria-label="Page sections"> 12 <a href="/#whatisiuna">What is iuna</a> 13 <a href="/#howtojoin">Join</a> 14 <a href="/git/iuna/file/docs/protocol.md.html">Protocol</a> 15 <a href="/git/iuna/file/README.md.html">Source</a> 16 <a href="/downloads/">Downloads</a> 17 </nav> 18 </div> 19 </div>""" 20 21 22 def postprocess_html(html: str) -> str: 23 if '<div class="topbar">' not in html: 24 html = re.sub(r"<body([^>]*)>", r"<body\1>\n" + TOPBAR, html, count=1) 25 26 html = re.sub( 27 r"<td id=\"logo\"[^>]*>.*?</td>", 28 "", 29 html, 30 count=1, 31 flags=re.S, 32 ) 33 html = re.sub( 34 r"<td><a href=\"[^\"]*\"><img src=\"[^\"]*logo\.png\"[^>]*></a></td>", 35 "", 36 html, 37 count=1, 38 flags=re.S, 39 ) 40 return re.sub(r"<tr([^>]*)><td></td><td>", r"<tr\1><td>", html) 41 42 43 def main() -> int: 44 if len(sys.argv) != 2: 45 print(f"Usage: {sys.argv[0]} <generated-git-root>", file=sys.stderr) 46 return 2 47 48 git_root = Path(sys.argv[1]) 49 if not git_root.is_dir(): 50 print(f"error: generated git root does not exist: {git_root}", file=sys.stderr) 51 return 1 52 53 for path in git_root.rglob("*.html"): 54 html = path.read_text(encoding="utf-8") 55 path.write_text(postprocess_html(html), encoding="utf-8") 56 57 return 0 58 59 60 if __name__ == "__main__": 61 raise SystemExit(main())