-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathhooks.py
More file actions
91 lines (68 loc) · 3.21 KB
/
Copy pathhooks.py
File metadata and controls
91 lines (68 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
MkDocs hooks for the KMS documentation.
on_config: read KMS version from Cargo.toml → config.extra.kms_version
on_page_markdown: transform plain file paths in log-reference.md table cells
into GitHub source links at build time.
"""
import re
from pathlib import Path
# ── on_config ─────────────────────────────────────────────────────────────
def on_config(config):
"""Called once before the build. Reads version and patches config."""
hooks_dir = Path(__file__).parent # documentation/
repo_root = hooks_dir.parent # repo root
cargo_toml = repo_root / "Cargo.toml"
version = _read_workspace_version(cargo_toml)
config.extra["kms_version"] = version or "develop"
return config
def _read_workspace_version(cargo_toml: Path) -> str | None:
if not cargo_toml.exists():
return None
text = cargo_toml.read_text(encoding="utf-8")
m = re.search(
r'\[workspace\.package\].*?^version\s*=\s*"([^"]+)"',
text, re.DOTALL | re.MULTILINE,
)
if m:
return m.group(1)
m2 = re.search(r'^version\s*=\s*"([^"]+)"', text, re.MULTILINE)
return m2.group(1) if m2 else None
# ── on_page_markdown ───────────────────────────────────────────────────────
_FILE_CELL_RE = re.compile(r'^`([^`]+\.(rs|ts|tsx|js|toml|py|md|sh))`$')
_CRATE_PATH_RE = re.compile(r'Crate path:\s*`([^`]+)`')
def on_page_markdown(markdown, **kwargs):
"""
Pre-process log-reference.md: turn plain `src/foo.rs` File column cells
into Markdown links pointing to the exact file on GitHub.
Runs at build time — output is static HTML, no JS required.
State machine: tracks current_crate_path as it moves through sections,
then rewrites column 3 of every 5-column table data row.
"""
page = kwargs.get("page")
config = kwargs.get("config")
if page is None or "log-reference" not in page.file.src_uri:
return markdown
version = config.extra.get("kms_version", "develop") if config else "develop"
github_base = f"https://github.com/Cosmian/kms/blob/{version}"
lines = markdown.splitlines()
out = []
current_crate_path = None
for line in lines:
# Track crate path from "Crate path: `xxx`" lines
m = _CRATE_PATH_RE.search(line)
if m:
current_crate_path = m.group(1).rstrip("/")
# Rewrite File column in 5-column table data rows
if current_crate_path and line.startswith("|"):
parts = line.split("|")
# A 5-column row has 7 parts: ['', col1, col2, col3, col4, col5, '']
if len(parts) == 7:
file_col = parts[3].strip()
fm = _FILE_CELL_RE.match(file_col)
if fm:
file_rel = fm.group(1)
full_url = f"{github_base}/{current_crate_path}/{file_rel}"
parts[3] = f" [{file_rel}]({full_url}) "
line = "|".join(parts)
out.append(line)
return "\n".join(out)