|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Shared helpers for turning option anchors into mdbook links. |
| 3 | +
|
| 4 | +Both the manual conversion and the option page rendering refer to options |
| 5 | +through `opt-`, `nixos-opt-` and `nix-darwin-opt-` anchors. The options are |
| 6 | +split over one page per namespace, and per module for the namespaces in |
| 7 | +`DEEP_SPLIT_NAMESPACES`, so an anchor has to be resolved to a page before it |
| 8 | +can be linked to. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import re |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +OPTION_LINK = re.compile( |
| 18 | + r"\[(?P<label>[^\]]*)\]\(#(?P<anchor>(?:opt|nixos-opt|nix-darwin-opt)-[^)]+)\)" |
| 19 | +) |
| 20 | +OPTION_HREF = re.compile(r'href="#(?P<anchor>(?:opt|nixos-opt|nix-darwin-opt)-[^"]+)"') |
| 21 | +DEEP_SPLIT_NAMESPACES = {"programs", "services"} |
| 22 | +ANCHOR_BASES = ( |
| 23 | + ("nix-darwin-opt-", "options/nix-darwin"), |
| 24 | + ("nixos-opt-", "options/nixos"), |
| 25 | + ("opt-", "options/home-manager"), |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def option_label(anchor: str) -> str: |
| 30 | + """Return the option name an anchor refers to.""" |
| 31 | + for prefix, _ in ANCHOR_BASES: |
| 32 | + if anchor.startswith(prefix): |
| 33 | + return anchor.removeprefix(prefix) |
| 34 | + return anchor |
| 35 | + |
| 36 | + |
| 37 | +def option_page_parts(option_name: str) -> list[str]: |
| 38 | + """Return the path segments of the page documenting an option.""" |
| 39 | + parts = option_name.split(".") |
| 40 | + namespace = parts[0] |
| 41 | + if namespace in DEEP_SPLIT_NAMESPACES and len(parts) > 1: |
| 42 | + return parts[:2] |
| 43 | + return [namespace] |
| 44 | + |
| 45 | + |
| 46 | +def option_fragment(option_name: str, page_parts: list[str], anchor: str) -> str: |
| 47 | + """Return the URL fragment addressing an option on its page. |
| 48 | +
|
| 49 | + A `programs.foo` or `services.foo` path names a module rather than an |
| 50 | + option, so its page holds no matching anchor and the fragment is empty, |
| 51 | + which links to the page itself. |
| 52 | + """ |
| 53 | + if len(page_parts) > 1 and option_name.split(".") == page_parts: |
| 54 | + return "" |
| 55 | + return f"#{anchor}" |
| 56 | + |
| 57 | + |
| 58 | +def option_target(anchor: str, current_file: Path, base_depth: int = 0) -> str: |
| 59 | + """Return a link from `current_file` to the option an anchor refers to. |
| 60 | +
|
| 61 | + `base_depth` is the depth of `current_file` below the manual source root. |
| 62 | + """ |
| 63 | + for prefix, base in ANCHOR_BASES: |
| 64 | + if anchor.startswith(prefix): |
| 65 | + option = anchor.removeprefix(prefix).replace("<", "_").replace(">", "_") |
| 66 | + anchor = f"{prefix}{option}" |
| 67 | + break |
| 68 | + else: |
| 69 | + raise ValueError(f"not an option anchor: {anchor}") |
| 70 | + |
| 71 | + page_parts = option_page_parts(option) |
| 72 | + prefix = "../" * (base_depth + len(current_file.parent.parts)) |
| 73 | + fragment = option_fragment(option, page_parts, anchor) |
| 74 | + return f"{prefix}{base}/{'/'.join(page_parts)}.md{fragment}" |
0 commit comments