-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmkdocs_hooks.py
More file actions
310 lines (268 loc) · 9.23 KB
/
Copy pathmkdocs_hooks.py
File metadata and controls
310 lines (268 loc) · 9.23 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""MkDocs hooks for repository-relative source links."""
import re
from collections.abc import Mapping
from pathlib import Path
from typing import Protocol
from urllib.parse import quote, unquote, urlsplit
_FENCE_RE = re.compile(
r"^(?P<prefix>[> \t]*)(?:(?P<list_marker>[-+*]|\d{1,9}[.)])"
r"(?P<list_padding>[ \t]{1,4}))?(?P<marker>`{3,}|~{3,})(?P<rest>[^\r\n]*)"
)
_BACKTICK_RUN_RE = re.compile(r"`+")
_LINK_DESTINATION_RE = re.compile(
r"(?P<prefix>\]\(\s*)(?:<(?P<angled>[^>\r\n]+)>|(?P<plain>[^\s)\r\n]+))"
)
class _PageFile(Protocol):
abs_src_path: str
class _Page(Protocol):
file: _PageFile
class _MkDocsConfig(Protocol):
config_file_path: str
def __getitem__(self, key: str) -> object: ...
def _target_url(
destination: str,
*,
source_path: Path,
docs_dir: Path,
repo_root: Path,
repo_url: str,
source_ref: str,
) -> str | None:
parsed = urlsplit(destination)
if parsed.scheme or parsed.netloc or not parsed.path or parsed.path.startswith(("/", "\\")):
return None
try:
target = (source_path.parent / unquote(parsed.path)).resolve()
except (OSError, RuntimeError):
return None
if target.is_relative_to(docs_dir) or not target.is_relative_to(repo_root):
return None
if not target.exists():
return None
object_type = "tree" if target.is_dir() else "blob"
relative_target = quote(target.relative_to(repo_root).as_posix(), safe="/")
encoded_ref = quote(source_ref, safe="/")
suffix = destination[len(parsed.path) :]
return f"{repo_url.rstrip('/')}/{object_type}/{encoded_ref}/{relative_target}{suffix}"
def _rewrite_link_destinations(
markdown: str,
*,
source_path: Path,
docs_dir: Path,
repo_root: Path,
repo_url: str,
source_ref: str,
) -> str:
def replace(match: re.Match[str]) -> str:
destination = match.group("angled") or match.group("plain")
rewritten = _target_url(
destination,
source_path=source_path,
docs_dir=docs_dir,
repo_root=repo_root,
repo_url=repo_url,
source_ref=source_ref,
)
if rewritten is None:
return match.group(0)
if match.group("angled") is not None:
rewritten = f"<{rewritten}>"
return f"{match.group('prefix')}{rewritten}"
return _LINK_DESTINATION_RE.sub(replace, markdown)
def _rewrite_outside_code_spans(
markdown: str,
*,
source_path: Path,
docs_dir: Path,
repo_root: Path,
repo_url: str,
source_ref: str,
) -> str:
def rewrite(text: str) -> str:
return _rewrite_link_destinations(
text,
source_path=source_path,
docs_dir=docs_dir,
repo_root=repo_root,
repo_url=repo_url,
source_ref=source_ref,
)
output: list[str] = []
cursor = 0
while opening := _BACKTICK_RUN_RE.search(markdown, cursor):
output.append(rewrite(markdown[cursor : opening.start()]))
closing = next(
(
candidate
for candidate in _BACKTICK_RUN_RE.finditer(markdown, opening.end())
if len(candidate.group(0)) == len(opening.group(0))
),
None,
)
if closing is None:
output.append(rewrite(markdown[opening.start() :]))
return "".join(output)
output.append(markdown[opening.start() : closing.end()])
cursor = closing.end()
output.append(rewrite(markdown[cursor:]))
return "".join(output)
def _container_position(line: str, quote_depth: int) -> tuple[int, int] | None:
cursor = 0
for _ in range(quote_depth):
padding = 0
while cursor < len(line) and line[cursor] == " " and padding < 3:
cursor += 1
padding += 1
if cursor >= len(line) or line[cursor] != ">":
return None
cursor += 1
if cursor < len(line) and line[cursor] in " \t":
cursor += 1
indentation_start = cursor
while cursor < len(line) and line[cursor] in " \t":
cursor += 1
indentation = len(line[indentation_start:cursor].expandtabs(4))
return indentation, cursor
def _fence_container(match: re.Match[str]) -> tuple[int, int] | None:
prefix = match.group("prefix")
quote_depth = prefix.count(">")
position = _container_position(prefix, quote_depth)
if position is None:
return None
indentation, _ = position
list_marker = match.group("list_marker")
if list_marker is not None:
indentation += len(list_marker) + len(match.group("list_padding").expandtabs(4))
elif indentation <= 3:
indentation = 0
return quote_depth, indentation
def _line_is_in_container(line: str, quote_depth: int, indentation: int) -> bool:
if not line.strip():
return True
position = _container_position(line, quote_depth)
if position is None:
return False
line_indentation, content_start = position
return not line[content_start:].strip() or line_indentation >= indentation
def _is_closing_fence(
match: re.Match[str],
*,
fence_character: str,
fence_length: int,
quote_depth: int,
indentation: int,
) -> bool:
marker = match.group("marker")
if (
match.group("list_marker") is not None
or marker[0] != fence_character
or len(marker) < fence_length
or match.group("rest").strip()
or match.group("prefix").count(">") != quote_depth
):
return False
position = _container_position(match.group("prefix"), quote_depth)
if position is None:
return False
closing_indentation, _ = position
return closing_indentation <= 3 if indentation == 0 else closing_indentation == indentation
def rewrite_repository_links(
markdown: str,
*,
source_path: Path,
docs_dir: Path,
repo_root: Path,
repo_url: str,
source_ref: str,
) -> str:
"""Rewrite valid links outside docs_dir to repository source URLs."""
source_path = source_path.resolve()
docs_dir = docs_dir.resolve()
repo_root = repo_root.resolve()
output: list[str] = []
pending_markdown: list[str] = []
fence_lines: list[str] = []
fence_character: str | None = None
fence_length = 0
fence_quote_depth = 0
fence_indentation = 0
def flush_pending_markdown() -> None:
if not pending_markdown:
return
output.append(
_rewrite_outside_code_spans(
"".join(pending_markdown),
source_path=source_path,
docs_dir=docs_dir,
repo_root=repo_root,
repo_url=repo_url,
source_ref=source_ref,
)
)
pending_markdown.clear()
for line in markdown.splitlines(keepends=True):
if fence_character is not None and not _line_is_in_container(
line, fence_quote_depth, fence_indentation
):
pending_markdown.extend(fence_lines)
fence_lines.clear()
fence_character = None
fence_length = 0
fence_quote_depth = 0
fence_indentation = 0
fence = _FENCE_RE.match(line)
if fence_character is None:
container = _fence_container(fence) if fence is not None else None
if fence is None or container is None:
pending_markdown.append(line)
continue
flush_pending_markdown()
marker = fence.group("marker")
fence_character = marker[0]
fence_length = len(marker)
fence_quote_depth, fence_indentation = container
fence_lines.append(line)
continue
fence_lines.append(line)
if fence is not None and _is_closing_fence(
fence,
fence_character=fence_character,
fence_length=fence_length,
quote_depth=fence_quote_depth,
indentation=fence_indentation,
):
output.extend(fence_lines)
fence_lines.clear()
fence_character = None
fence_length = 0
fence_quote_depth = 0
fence_indentation = 0
output.extend(fence_lines)
flush_pending_markdown()
return "".join(output)
def on_page_markdown(
markdown: str,
*,
page: _Page,
config: _MkDocsConfig,
files: object,
) -> str:
"""Rewrite repository-relative links before MkDocs validates Markdown links."""
del files
docs_dir = config["docs_dir"]
repo_url = config["repo_url"]
extra = config["extra"]
if not isinstance(docs_dir, str) or not isinstance(repo_url, str):
raise ValueError("MkDocs docs_dir and repo_url must be configured")
if not isinstance(extra, Mapping) or not isinstance(extra.get("source_ref"), str):
raise ValueError("MkDocs extra.source_ref must be configured")
return rewrite_repository_links(
markdown,
source_path=Path(page.file.abs_src_path),
docs_dir=Path(docs_dir),
repo_root=Path(config.config_file_path).resolve().parent,
repo_url=repo_url,
source_ref=extra["source_ref"],
)