1
0

ensure-stable-doc-urls.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #!/usr/bin/env python3
  2. # Doc URLs may change with time because they depend on Doxygen machinery.
  3. # This is unfortunate because it is good practice to keep valid URLs.
  4. # See: “Cool URIs don’t change” at https://www.w3.org/Provider/Style/URI.html.
  5. #
  6. # There is no built-in solution in Doxygen that we are aware of.
  7. # The solution proposed here is to maintain a registry of all URLs and manage
  8. # legacy URLs as redirections to their canonical page.
  9. import argparse
  10. import glob
  11. from enum import IntFlag
  12. from itertools import chain
  13. from pathlib import Path
  14. from string import Template
  15. from typing import NamedTuple, Sequence
  16. import yaml
  17. class Update(NamedTuple):
  18. new: str
  19. old: str
  20. class ExitCode(IntFlag):
  21. NORMAL = 0
  22. INVALID_UPDATES = 1 << 4
  23. MISSING_UPDATES = 1 << 5
  24. NON_UNIQUE_DIRECTIONS = 1 << 6
  25. THIS_SCRIPT_PATH = Path(__file__)
  26. RELATIVE_SCRIPT_PATH = THIS_SCRIPT_PATH.relative_to(THIS_SCRIPT_PATH.parent.parent)
  27. REDIRECTION_DELAY = 6 # in seconds. Note: at least 6s for accessibility
  28. REDIRECTION_TITLE = "xkbcommon: Page Redirection"
  29. OPTIONAL_ENTRY = "__optional__"
  30. # NOTE: The redirection works with the HTML tag: <meta http-equiv="refresh">.
  31. # See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#http-equiv
  32. #
  33. # NOTE: This page is a simplified version of the Doxygen-generated ones.
  34. # It does use the current stylesheets, but it may break if the theme is updated.
  35. # Ideally, we would just let Doxygen generate them, but I (Wismill) could not
  36. # find a way to do this with the redirection feature.
  37. REDIRECTION_PAGE_TEMPLATE = Template(
  38. """<!DOCTYPE HTML>
  39. <html lang="en-US">
  40. <head>
  41. <meta charset="UTF-8">
  42. <meta http-equiv="refresh" content="${delay}; url=${canonical}">
  43. <link href="doxygen.css" rel="stylesheet" type="text/css">
  44. <link href="doxygen-extra.css" rel="stylesheet" type="text/css">
  45. <title>${title}</title>
  46. </head>
  47. <body>
  48. <div id="top">
  49. <div id="titlearea" style="padding: 1em 0 1em 0.5em;">
  50. <div id="projectname">
  51. libxkbcommon
  52. </div>
  53. </div>
  54. </div>
  55. <div>
  56. <div class="header">
  57. <div class="headertitle">
  58. <div class="title">🔀 Redirection</div>
  59. </div>
  60. </div>
  61. <div class="contents">
  62. <p>This page has been moved.</p>
  63. <p>
  64. If you are not redirected automatically,
  65. follow the <a href="${canonical}">link to the current page</a>.
  66. </p>
  67. </div>
  68. </div>
  69. </body>
  70. </html>
  71. """
  72. )
  73. def parse_page_update(update: str) -> Update:
  74. updateʹ = Update(*update.split("="))
  75. if updateʹ.new == updateʹ.old:
  76. raise ValueError(f"Invalid update: {updateʹ}")
  77. return updateʹ
  78. def is_page_redirection(path: Path):
  79. with path.open("rt", encoding="utf-8") as fd:
  80. for line in fd:
  81. if REDIRECTION_TITLE in line:
  82. return True
  83. return False
  84. def update_registry(registry_path: Path, doc_dir: Path, updates: Sequence[str]):
  85. """
  86. Update the URL registry by:
  87. • Adding new pages
  88. • Updating page aliases
  89. """
  90. # Parse updates
  91. updates_ = dict(map(parse_page_update, updates))
  92. # Update
  93. invalid_updates = set(updates_)
  94. # Load previous registry
  95. with registry_path.open("rt", encoding="utf-8") as fd:
  96. registry: dict[str, list[str]] = yaml.safe_load(fd) or {}
  97. registryʹ = dict(
  98. (canonical, aliases)
  99. for canonical, aliases in registry.items()
  100. if canonical != OPTIONAL_ENTRY
  101. )
  102. # Expected updates
  103. missing_updates = set(
  104. canonical for canonical in registryʹ if not (doc_dir / canonical).is_file()
  105. )
  106. # Ensure each page is unique
  107. for d, rs in registryʹ.items():
  108. if clashes := frozenset(rs).intersection(registry):
  109. print(
  110. f"[ERROR] The following redirections of “{d}”",
  111. f"clash with canonical directions: {clashes}",
  112. )
  113. exit(ExitCode.NON_UNIQUE_DIRECTIONS)
  114. redirections = frozenset(chain.from_iterable(registryʹ.values()))
  115. for file in glob.iglob("**/*.html", root_dir=doc_dir, recursive=True):
  116. # Skip redirection pages
  117. if file in redirections:
  118. continue
  119. # Get previous entry and potential update
  120. if old := updates_.get(file):
  121. # Update old entry
  122. invalid_updates.remove(file)
  123. entry = registry.get(old)
  124. if entry is None:
  125. raise ValueError(f"Invalid update: {file}<-{old}")
  126. else:
  127. del registry[old]
  128. missing_updates.remove(old)
  129. registry[file] = [e for e in [old] + entry if e != file]
  130. print(f"[INFO] Updated: “{old}” to “{file}”")
  131. else:
  132. entry = registry.get(file)
  133. if entry is None:
  134. # New entry
  135. registry[file] = []
  136. print(f"[INFO] Added: {file}")
  137. else:
  138. # Keep previous entry
  139. pass
  140. exit_code = ExitCode.NORMAL
  141. # Check
  142. if invalid_updates:
  143. for update in invalid_updates:
  144. print(f"[ERROR] Update not processed: {update}")
  145. exit_code |= ExitCode.INVALID_UPDATES
  146. if missing_updates:
  147. for old in tuple(missing_updates):
  148. # Handle older Doxygen versions
  149. if old in registry.get(OPTIONAL_ENTRY, []):
  150. print(
  151. "[WARNING] Handling old Doxygen version:",
  152. f"skip optional “{old}”",
  153. )
  154. missing_updates.remove(old)
  155. continue
  156. old_redirections = registry[old]
  157. for r in old_redirections:
  158. path = doc_dir / r
  159. if path.is_file() and not is_page_redirection(path):
  160. print(
  161. "[WARNING] Handling old Doxygen version:",
  162. f"use “{r}” instead of “{old}” for the canonical direction",
  163. )
  164. missing_updates.remove(old)
  165. break
  166. else:
  167. print(f"[ERROR] “{old}” not found and has no update.")
  168. if missing_updates:
  169. exit_code |= ExitCode.MISSING_UPDATES
  170. if exit_code:
  171. print("[ERROR] Processing interrupted: please fix the errors above.")
  172. exit(exit_code.value)
  173. # Write changes
  174. with registry_path.open("wt", encoding="utf-8") as fd:
  175. fd.write(f"# WARNING: This file is autogenerated by: {RELATIVE_SCRIPT_PATH}\n")
  176. fd.write("# Do not edit manually.\n")
  177. yaml.dump(registry, fd)
  178. def generate_redirections(registry_path: Path, doc_dir: Path):
  179. """
  180. Create redirection pages using the aliases in the given URL registry.
  181. """
  182. cool = True
  183. # Load registry
  184. with registry_path.open("rt", encoding="utf-8") as fd:
  185. registry: dict[str, list[str]] = yaml.safe_load(fd) or {}
  186. registryʹ = dict(
  187. (canonical, aliases)
  188. for canonical, aliases in registry.items()
  189. if canonical != OPTIONAL_ENTRY
  190. )
  191. for canonical, aliases in registryʹ.items():
  192. # Check canonical path is up-to-date
  193. if not (doc_dir / canonical).is_file():
  194. # Handle older Doxygen versions
  195. if canonical in registry.get(OPTIONAL_ENTRY, []):
  196. print(
  197. "[WARNING] Handling old Doxygen version:",
  198. f"skip optional “{canonical}”",
  199. )
  200. continue
  201. for r in aliases:
  202. path = doc_dir / r
  203. if path.is_file() and not is_page_redirection(path):
  204. print(
  205. "[WARNING] Handling old Doxygen version:",
  206. f"use “{r}” instead of “{canonical}” for the canonical direction",
  207. )
  208. canonical = r
  209. aliases.remove(r)
  210. break
  211. else:
  212. cool = False
  213. print(
  214. f"ERROR: missing canonical documentation page “{canonical}”. "
  215. f"Please update “{registry_path}” using {RELATIVE_SCRIPT_PATH}”."
  216. )
  217. # Add a redirection page
  218. for alias in aliases:
  219. path = doc_dir / alias
  220. with path.open("wt", encoding="utf-8") as fd:
  221. fd.write(
  222. REDIRECTION_PAGE_TEMPLATE.substitute(
  223. canonical=canonical,
  224. delay=REDIRECTION_DELAY,
  225. title=REDIRECTION_TITLE,
  226. )
  227. )
  228. if not cool:
  229. exit(1)
  230. def add_registry_argument(parser):
  231. parser.add_argument(
  232. "registry",
  233. type=Path,
  234. help="Path to the doc URI registry.",
  235. )
  236. def add_docdir_argument(parser):
  237. parser.add_argument(
  238. "docdir",
  239. type=Path,
  240. metavar="DOC_DIR",
  241. help="Path to the generated HTML documentation directory.",
  242. )
  243. if __name__ == "__main__":
  244. parser = argparse.ArgumentParser(
  245. description="Tool to ensure HTML documentation has stable URLs"
  246. )
  247. subparsers = parser.add_subparsers()
  248. parser_registry = subparsers.add_parser(
  249. "update-registry", help="Update the registry of URIs"
  250. )
  251. add_registry_argument(parser_registry)
  252. add_docdir_argument(parser_registry)
  253. parser_registry.add_argument(
  254. "updates",
  255. nargs="*",
  256. type=str,
  257. help="Update: new=previous entries",
  258. )
  259. parser_registry.set_defaults(
  260. run=lambda args: update_registry(args.registry, args.docdir, args.updates)
  261. )
  262. parser_redirections = subparsers.add_parser(
  263. "generate-redirections", help="Generate URIs redirections"
  264. )
  265. add_registry_argument(parser_redirections)
  266. add_docdir_argument(parser_redirections)
  267. parser_redirections.set_defaults(
  268. run=lambda args: generate_redirections(args.registry, args.docdir)
  269. )
  270. args = parser.parse_args()
  271. args.run(args)