-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
rendering.py
287 lines (231 loc) · 10.6 KB
/
rendering.py
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
"""This module holds helpers responsible for augmentations to the Markdown sub-documents produced by handlers."""
from __future__ import annotations
import copy
import re
import textwrap
from typing import TYPE_CHECKING, Any
from markdown.extensions import Extension
from markdown.extensions.codehilite import CodeHiliteExtension
from markdown.treeprocessors import Treeprocessor
from markupsafe import Markup
from pymdownx.highlight import Highlight, HighlightExtension
if TYPE_CHECKING:
from xml.etree.ElementTree import Element
from markdown import Markdown
class Highlighter(Highlight):
"""Code highlighter that tries to match the Markdown configuration.
Picking up the global config and defaults works only if you use the `codehilite` or
`pymdownx.highlight` (recommended) Markdown extension.
- If you use `pymdownx.highlight`, highlighting settings are picked up from it, and the
default CSS class is `.highlight`. This also means the default of `guess_lang: false`.
- Otherwise, if you use the `codehilite` extension, settings are picked up from it, and the
default CSS class is `.codehilite`. Also consider setting `guess_lang: false`.
- If neither are added to `markdown_extensions`, highlighting is enabled anyway. This is for
backwards compatibility. If you really want to disable highlighting even in *mkdocstrings*,
add one of these extensions anyway and set `use_pygments: false`.
The underlying implementation is `pymdownx.highlight` regardless.
"""
# https://raw.githubusercontent.com/facelessuser/pymdown-extensions/main/docs/src/markdown/extensions/highlight.md
_highlight_config_keys = frozenset(
(
"css_class",
"guess_lang",
"pygments_style",
"noclasses",
"use_pygments",
"linenums",
"linenums_special",
"linenums_style",
"linenums_class",
"extend_pygments_lang",
"language_prefix",
"code_attr_on_pre",
"auto_title",
"auto_title_map",
"line_spans",
"anchor_linenums",
"line_anchors",
),
)
def __init__(self, md: Markdown):
"""Configure to match a `markdown.Markdown` instance.
Arguments:
md: The Markdown instance to read configs from.
"""
config: dict[str, Any] = {}
self._highlighter: str | None = None
for ext in md.registeredExtensions:
if isinstance(ext, HighlightExtension) and (ext.enabled or not config):
self._highlighter = "highlight"
config = ext.getConfigs()
break # This one takes priority, no need to continue looking
if isinstance(ext, CodeHiliteExtension) and not config:
self._highlighter = "codehilite"
config = ext.getConfigs()
config["language_prefix"] = config["lang_prefix"]
self._css_class = config.pop("css_class", "highlight")
super().__init__(**{name: opt for name, opt in config.items() if name in self._highlight_config_keys})
def highlight(
self,
src: str,
language: str | None = None,
*,
inline: bool = False,
dedent: bool = True,
linenums: bool | None = None,
**kwargs: Any,
) -> str:
"""Highlight a code-snippet.
Arguments:
src: The code to highlight.
language: Explicitly tell what language to use for highlighting.
inline: Whether to highlight as inline.
dedent: Whether to dedent the code before highlighting it or not.
linenums: Whether to add line numbers in the result.
**kwargs: Pass on to `pymdownx.highlight.Highlight.highlight`.
Returns:
The highlighted code as HTML text, marked safe (not escaped for HTML).
"""
if isinstance(src, Markup):
src = src.unescape()
if dedent:
src = textwrap.dedent(src)
kwargs.setdefault("css_class", self._css_class)
old_linenums = self.linenums # type: ignore[has-type]
if linenums is not None:
self.linenums = linenums
try:
result = super().highlight(src, language, inline=inline, **kwargs)
finally:
self.linenums = old_linenums
if inline:
# From the maintainer of codehilite, the codehilite CSS class, as defined by the user,
# should never be added to inline code, because codehilite does not support inline code.
# See https://github.com/Python-Markdown/markdown/issues/1220#issuecomment-1692160297.
css_class = "" if self._highlighter == "codehilite" else kwargs["css_class"]
return Markup(f'<code class="{css_class} language-{language}">{result.text}</code>')
return Markup(result)
class IdPrependingTreeprocessor(Treeprocessor):
"""Prepend the configured prefix to IDs of all HTML elements."""
name = "mkdocstrings_ids"
id_prefix: str
"""The prefix to add to every ID. It is prepended without any separator; specify your own separator if needed."""
def __init__(self, md: Markdown, id_prefix: str):
"""Initialize the object.
Arguments:
md: A `markdown.Markdown` instance.
id_prefix: The prefix to add to every ID. It is prepended without any separator.
"""
super().__init__(md)
self.id_prefix = id_prefix
def run(self, root: Element) -> None: # noqa: D102 (ignore missing docstring)
if self.id_prefix:
self._prefix_ids(root)
def _prefix_ids(self, root: Element) -> None:
index = -1
for el in reversed(root):
index += 1
self._prefix_ids(el)
href_attr = el.get("href")
if id_attr := el.get("id"):
if el.tag == "a" and not href_attr:
new_el = copy.deepcopy(el)
new_el.set("id", self.id_prefix + id_attr)
root.insert(index + 1, new_el)
else:
el.set("id", self.id_prefix + id_attr)
if href_attr and href_attr.startswith("#"):
el.set("href", "#" + self.id_prefix + href_attr[1:])
if name_attr := el.get("name"):
el.set("name", self.id_prefix + name_attr)
if el.tag == "label":
for_attr = el.get("for")
if for_attr:
el.set("for", self.id_prefix + for_attr)
class HeadingShiftingTreeprocessor(Treeprocessor):
"""Shift levels of all Markdown headings according to the configured base level."""
name = "mkdocstrings_headings"
regex = re.compile(r"([Hh])([1-6])")
shift_by: int
"""The number of heading "levels" to add to every heading. `<h2>` with `shift_by = 3` becomes `<h5>`."""
def __init__(self, md: Markdown, shift_by: int):
"""Initialize the object.
Arguments:
md: A `markdown.Markdown` instance.
shift_by: The number of heading "levels" to add to every heading.
"""
super().__init__(md)
self.shift_by = shift_by
def run(self, root: Element) -> None: # noqa: D102 (ignore missing docstring)
if not self.shift_by:
return
for el in root.iter():
match = self.regex.fullmatch(el.tag)
if match:
level = int(match[2]) + self.shift_by
level = max(1, min(level, 6))
el.tag = f"{match[1]}{level}"
class _HeadingReportingTreeprocessor(Treeprocessor):
"""Records the heading elements encountered in the document."""
name = "mkdocstrings_headings_list"
regex = re.compile(r"[Hh][1-6]")
headings: list[Element]
"""The list (the one passed in the initializer) that is used to record the heading elements (by appending to it)."""
def __init__(self, md: Markdown, headings: list[Element]):
super().__init__(md)
self.headings = headings
def run(self, root: Element) -> None:
permalink_class = self.md.treeprocessors["toc"].permalink_class # type: ignore[attr-defined]
for el in root.iter():
if self.regex.fullmatch(el.tag):
el = copy.copy(el) # noqa: PLW2901
# 'toc' extension's first pass (which we require to build heading stubs/ids) also edits the HTML.
# Undo the permalink edit so we can pass this heading to the outer pass of the 'toc' extension.
if len(el) > 0 and el[-1].get("class") == permalink_class:
del el[-1]
self.headings.append(el)
class ParagraphStrippingTreeprocessor(Treeprocessor):
"""Unwraps the <p> element around the whole output."""
name = "mkdocstrings_strip_paragraph"
strip = False
def run(self, root: Element) -> Element | None: # noqa: D102 (ignore missing docstring)
if self.strip and len(root) == 1 and root[0].tag == "p":
# Turn the single <p> element into the root element and inherit its tag name (it's significant!)
root[0].tag = root.tag
return root[0]
return None
class MkdocstringsInnerExtension(Extension):
"""Extension that should always be added to Markdown sub-documents that handlers request (and *only* them)."""
def __init__(self, headings: list[Element]):
"""Initialize the object.
Arguments:
headings: A list that will be populated with all HTML heading elements encountered in the document.
"""
super().__init__()
self.headings = headings
def extendMarkdown(self, md: Markdown) -> None: # noqa: N802 (casing: parent method's name)
"""Register the extension.
Arguments:
md: A `markdown.Markdown` instance.
"""
md.registerExtension(self)
md.treeprocessors.register(
HeadingShiftingTreeprocessor(md, 0),
HeadingShiftingTreeprocessor.name,
priority=12,
)
md.treeprocessors.register(
IdPrependingTreeprocessor(md, ""),
IdPrependingTreeprocessor.name,
priority=4, # Right after 'toc' (needed because that extension adds ids to headers).
)
md.treeprocessors.register(
_HeadingReportingTreeprocessor(md, self.headings),
_HeadingReportingTreeprocessor.name,
priority=1, # Close to the end.
)
md.treeprocessors.register(
ParagraphStrippingTreeprocessor(md),
ParagraphStrippingTreeprocessor.name,
priority=0.99, # Close to the end.
)