|
| 1 | +"""Support for client-side redirects for removed HTML anchors.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | +from urllib.parse import urlsplit |
| 7 | + |
| 8 | +from docutils import nodes |
| 9 | +from sphinx.util.docutils import SphinxDirective |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from sphinx.application import Sphinx |
| 13 | + from sphinx.util.typing import ExtensionMetadata |
| 14 | + |
| 15 | + |
| 16 | +class AnchorMapEntryNode(nodes.Element): |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +class AnchorMap(SphinxDirective): |
| 21 | + has_content = True |
| 22 | + |
| 23 | + def run(self) -> list[nodes.Node]: |
| 24 | + self.assert_has_content() |
| 25 | + |
| 26 | + entries = [] |
| 27 | + messages = [] |
| 28 | + |
| 29 | + for index, line in enumerate(self.content): |
| 30 | + if not (line := line.strip()): |
| 31 | + continue |
| 32 | + |
| 33 | + old_anchor, sep, target = line.partition(": ") |
| 34 | + old_anchor, target = old_anchor.strip(), target.strip() |
| 35 | + |
| 36 | + if not sep or not old_anchor or not target: |
| 37 | + raise self.error( |
| 38 | + "anchormap entries should be like: 'old-html-fragment: target'" |
| 39 | + ) |
| 40 | + |
| 41 | + children, parse_messages = self.parse_inline( |
| 42 | + target, |
| 43 | + lineno=self.content_offset + index, |
| 44 | + ) |
| 45 | + entry = AnchorMapEntryNode("", *children, old_anchor=old_anchor) |
| 46 | + self.set_source_info(entry) |
| 47 | + entries.append(entry) |
| 48 | + messages.extend(parse_messages) |
| 49 | + |
| 50 | + if not entries: |
| 51 | + raise self.error("anchormap must contain at least one entry") |
| 52 | + |
| 53 | + return entries + messages |
| 54 | + |
| 55 | + |
| 56 | +def process_anchor_maps( |
| 57 | + app: Sphinx, |
| 58 | + doctree: nodes.document, |
| 59 | + _docname: str, |
| 60 | +) -> None: |
| 61 | + redirects = {} |
| 62 | + |
| 63 | + for entry in list(doctree.findall(AnchorMapEntryNode)): |
| 64 | + target = None |
| 65 | + references = list(entry.findall(nodes.reference)) |
| 66 | + |
| 67 | + if len(references) == 1: |
| 68 | + if refuri := references[0].get("refuri"): |
| 69 | + parts = urlsplit(refuri) |
| 70 | + if ( |
| 71 | + not parts.scheme and not parts.netloc |
| 72 | + ): # Check it's internal |
| 73 | + target = refuri |
| 74 | + elif refid := references[0].get("refid"): |
| 75 | + target = f"#{refid}" |
| 76 | + |
| 77 | + if target is not None: |
| 78 | + redirects[entry["old_anchor"]] = target |
| 79 | + |
| 80 | + entry.parent.remove(entry) |
| 81 | + |
| 82 | + if app.builder.format == "html" and not app.builder.embedded: |
| 83 | + doctree["anchor_redirects"] = redirects |
| 84 | + |
| 85 | + |
| 86 | +def add_anchor_redirects_to_context( |
| 87 | + app: Sphinx, |
| 88 | + _pagename: str, |
| 89 | + _templatename: str, |
| 90 | + context: dict[str, object], |
| 91 | + doctree: nodes.document | None, |
| 92 | +) -> None: |
| 93 | + if doctree is None: |
| 94 | + return |
| 95 | + |
| 96 | + if redirects := doctree.get("anchor_redirects"): |
| 97 | + context["anchor_redirects"] = redirects |
| 98 | + |
| 99 | + |
| 100 | +def setup(app: Sphinx) -> ExtensionMetadata: |
| 101 | + app.add_directive("anchormap", AnchorMap) |
| 102 | + app.add_node(AnchorMapEntryNode) |
| 103 | + app.connect("doctree-resolved", process_anchor_maps) |
| 104 | + app.connect("html-page-context", add_anchor_redirects_to_context) |
| 105 | + |
| 106 | + return { |
| 107 | + "version": "1.0", |
| 108 | + "parallel_read_safe": True, |
| 109 | + "parallel_write_safe": True, |
| 110 | + } |
0 commit comments