|
| 1 | +""" |
| 2 | +Python-Markdown extension: fix glossary link depth and inject data-preview. |
| 3 | +
|
| 4 | +Background |
| 5 | +---------- |
| 6 | +Zensical's LinksProcessor always prepends exactly one extra "../" to non-index |
| 7 | +relative links when use_directory_urls=True. Because includes/abbreviations.md |
| 8 | +uses "../user-documentation/glossary.md#term" (one leading ".."), LinksProcessor |
| 9 | +always emits "../../user-documentation/glossary/#term" in HTML regardless of |
| 10 | +actual page depth: |
| 11 | +
|
| 12 | + depth-2 page (something/page/): ../../ -> root OK |
| 13 | + depth-4 page (a/b/c/page/): ../../ -> a/b/ WRONG |
| 14 | +
|
| 15 | +Separately, zensical.extensions.preview adds data-preview="" by resolving the |
| 16 | +original .md href relative to the current source file. For deeply nested pages |
| 17 | +the relative "../user-documentation/glossary.md" doesn't resolve to |
| 18 | +"user-documentation/glossary.md", so data-preview is never added. |
| 19 | +
|
| 20 | +Fix |
| 21 | +--- |
| 22 | +Register a Treeprocessor at priority -1 (lower than the priority-0 processors |
| 23 | +PreviewProcessor and LinksProcessor). It runs after both, reads the current |
| 24 | +page's source path from Zensical's `zrelpath` processor, computes the correct |
| 25 | +depth, rewrites every glossary href, and adds data-preview="" where absent. |
| 26 | +""" |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +import re |
| 31 | +from typing import TYPE_CHECKING |
| 32 | + |
| 33 | +from markdown import Extension |
| 34 | +from markdown.treeprocessors import Treeprocessor |
| 35 | +from zensical.extensions.links import LinksProcessor |
| 36 | + |
| 37 | +if TYPE_CHECKING: |
| 38 | + from xml.etree.ElementTree import Element |
| 39 | + |
| 40 | +# Matches the relative prefix (any number of ../) before the glossary path |
| 41 | +_GLOSSARY_RE = re.compile(r"^(?:\.\./)*user-documentation/glossary/") |
| 42 | + |
| 43 | + |
| 44 | +def _url_depth(src_path: str) -> int: |
| 45 | + """Return URL depth with use_directory_urls=True. |
| 46 | +
|
| 47 | + - index.md / README.md at directory depth D -> D |
| 48 | + - any other .md at directory depth D -> D + 1 |
| 49 | + """ |
| 50 | + parts = src_path.replace("\\", "/").split("/") |
| 51 | + is_index = parts[-1].lower() in ("index.md", "readme.md") |
| 52 | + return len(parts) - 1 if is_index else len(parts) |
| 53 | + |
| 54 | + |
| 55 | +def _find_page_path(md) -> str | None: |
| 56 | + """Return the source path of the page being rendered. |
| 57 | +
|
| 58 | + This repo ships a Docker-pinned Zensical version that always registers the |
| 59 | + links treeprocessor as ``zrelpath``. |
| 60 | + """ |
| 61 | + try: |
| 62 | + idx = md.treeprocessors.get_index_for_name("zrelpath") |
| 63 | + proc = md.treeprocessors[idx] |
| 64 | + if isinstance(proc, LinksProcessor) and isinstance(proc.path, str): |
| 65 | + return proc.path |
| 66 | + except Exception: # noqa: BLE001 |
| 67 | + pass |
| 68 | + |
| 69 | + return None |
| 70 | + |
| 71 | + |
| 72 | +class GlossaryFixProcessor(Treeprocessor): |
| 73 | + """Rewrite glossary hrefs to the correct depth and ensure data-preview.""" |
| 74 | + |
| 75 | + def run(self, root: Element) -> None: |
| 76 | + try: |
| 77 | + path = _find_page_path(self.md) |
| 78 | + if path is None: |
| 79 | + return |
| 80 | + depth = _url_depth(path) |
| 81 | + prefix = "../" * depth |
| 82 | + except Exception: # noqa: BLE001 |
| 83 | + return |
| 84 | + |
| 85 | + for el in root.iter("a"): |
| 86 | + href = el.get("href", "") |
| 87 | + if "user-documentation/glossary/" not in href: |
| 88 | + continue |
| 89 | + |
| 90 | + new_href = _GLOSSARY_RE.sub( |
| 91 | + prefix + "user-documentation/glossary/", href |
| 92 | + ) |
| 93 | + if new_href != href: |
| 94 | + el.set("href", new_href) |
| 95 | + |
| 96 | + if "data-preview" not in el.attrib: |
| 97 | + el.set("data-preview", "") |
| 98 | + |
| 99 | + |
| 100 | +class GlossaryFixExtension(Extension): |
| 101 | + def extendMarkdown(self, md) -> None: # noqa: N802 |
| 102 | + md.treeprocessors.register(GlossaryFixProcessor(md), "glossary_fix", -1) |
| 103 | + |
| 104 | + |
| 105 | +def makeExtension(**kwargs): # noqa: N802 |
| 106 | + return GlossaryFixExtension(**kwargs) |
0 commit comments