-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplugin.py
218 lines (172 loc) · 8.5 KB
/
plugin.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
"""This module contains the "mkdocs-autorefs" plugin.
After each page is processed by the Markdown converter, this plugin stores absolute URLs of every HTML anchors
it finds to later be able to fix unresolved references.
It stores them during the [`on_page_content` event hook](https://www.mkdocs.org/user-guide/plugins/#on_page_content).
Just before writing the final HTML to the disc, during the
[`on_post_page` event hook](https://www.mkdocs.org/user-guide/plugins/#on_post_page),
this plugin searches for references of the form `[identifier][]` or `[title][identifier]` that were not resolved,
and fixes them using the previously stored identifier-URL mapping.
"""
from __future__ import annotations
import contextlib
import functools
import logging
from typing import TYPE_CHECKING, Any, Callable, Sequence
from urllib.parse import urlsplit
from mkdocs.plugins import BasePlugin
from mkdocs_autorefs.references import AutorefsExtension, fix_refs, relative_url
if TYPE_CHECKING:
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.pages import Page
from mkdocs.structure.toc import AnchorLink
try:
from mkdocs.plugins import get_plugin_logger
log = get_plugin_logger(__name__)
except ImportError:
# TODO: remove once support for MkDocs <1.5 is dropped
log = logging.getLogger(f"mkdocs.plugins.{__name__}") # type: ignore[assignment]
class AutorefsPlugin(BasePlugin):
"""An `mkdocs` plugin.
This plugin defines the following event hooks:
- `on_config`
- `on_page_content`
- `on_post_page`
Check the [Developing Plugins](https://www.mkdocs.org/user-guide/plugins/#developing-plugins) page of `mkdocs`
for more information about its plugin system.
"""
scan_toc: bool = True
current_page: str | None = None
def __init__(self) -> None:
"""Initialize the object."""
super().__init__()
self._url_map: dict[str, str] = {}
self._abs_url_map: dict[str, str] = {}
self.get_fallback_anchor: Callable[[str], str | None] | None = None
def register_anchor(self, page: str, identifier: str) -> None:
"""Register that an anchor corresponding to an identifier was encountered when rendering the page.
Arguments:
page: The relative URL of the current page. Examples: `'foo/bar/'`, `'foo/index.html'`
identifier: The HTML anchor (without '#') as a string.
"""
self._url_map[identifier] = f"{page}#{identifier}"
def register_url(self, identifier: str, url: str) -> None:
"""Register that the identifier should be turned into a link to this URL.
Arguments:
identifier: The new identifier.
url: The absolute URL (including anchor, if needed) where this item can be found.
"""
self._abs_url_map[identifier] = url
def _get_item_url(
self,
identifier: str,
fallback: Callable[[str], Sequence[str]] | None = None,
) -> str:
try:
return self._url_map[identifier]
except KeyError:
if identifier in self._abs_url_map:
return self._abs_url_map[identifier]
if fallback:
new_identifiers = fallback(identifier)
for new_identifier in new_identifiers:
with contextlib.suppress(KeyError):
url = self._get_item_url(new_identifier)
self._url_map[identifier] = url
return url
raise
def get_item_url(
self,
identifier: str,
from_url: str | None = None,
fallback: Callable[[str], Sequence[str]] | None = None,
) -> str:
"""Return a site-relative URL with anchor to the identifier, if it's present anywhere.
Arguments:
identifier: The anchor (without '#').
from_url: The URL of the base page, from which we link towards the targeted pages.
fallback: An optional function to suggest alternative anchors to try on failure.
Returns:
A site-relative URL.
"""
url = self._get_item_url(identifier, fallback)
if from_url is not None:
parsed = urlsplit(url)
if not parsed.scheme and not parsed.netloc:
return relative_url(from_url, url)
return url
def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
"""Instantiate our Markdown extension.
Hook for the [`on_config` event](https://www.mkdocs.org/user-guide/plugins/#on_config).
In this hook, we instantiate our [`AutorefsExtension`][mkdocs_autorefs.references.AutorefsExtension]
and add it to the list of Markdown extensions used by `mkdocs`.
Arguments:
config: The MkDocs config object.
Returns:
The modified config.
"""
log.debug("Adding AutorefsExtension to the list")
config["markdown_extensions"].append(AutorefsExtension())
return config
def on_page_markdown(self, markdown: str, page: Page, **kwargs: Any) -> str: # noqa: ARG002
"""Remember which page is the current one.
Arguments:
markdown: Input Markdown.
page: The related MkDocs page instance.
kwargs: Additional arguments passed by MkDocs.
Returns:
The same Markdown. We only use this hook to map anchors to URLs.
"""
self.current_page = page.url
return markdown
def on_page_content(self, html: str, page: Page, **kwargs: Any) -> str: # noqa: ARG002
"""Map anchors to URLs.
Hook for the [`on_page_content` event](https://www.mkdocs.org/user-guide/plugins/#on_page_content).
In this hook, we map the IDs of every anchor found in the table of contents to the anchors absolute URLs.
This mapping will be used later to fix unresolved reference of the form `[title][identifier]` or
`[identifier][]`.
Arguments:
html: HTML converted from Markdown.
page: The related MkDocs page instance.
kwargs: Additional arguments passed by MkDocs.
Returns:
The same HTML. We only use this hook to map anchors to URLs.
"""
if self.scan_toc:
log.debug(f"Mapping identifiers to URLs for page {page.file.src_path}")
for item in page.toc.items:
self.map_urls(page.url, item)
return html
def map_urls(self, base_url: str, anchor: AnchorLink) -> None:
"""Recurse on every anchor to map its ID to its absolute URL.
This method populates `self.url_map` by side-effect.
Arguments:
base_url: The base URL to use as a prefix for each anchor's relative URL.
anchor: The anchor to process and to recurse on.
"""
self.register_anchor(base_url, anchor.id)
for child in anchor.children:
self.map_urls(base_url, child)
def on_post_page(self, output: str, page: Page, **kwargs: Any) -> str: # noqa: ARG002
"""Fix cross-references.
Hook for the [`on_post_page` event](https://www.mkdocs.org/user-guide/plugins/#on_post_page).
In this hook, we try to fix unresolved references of the form `[title][identifier]` or `[identifier][]`.
Doing that allows the user of `autorefs` to cross-reference objects in their documentation strings.
It uses the native Markdown syntax so it's easy to remember and use.
We log a warning for each reference that we couldn't map to an URL, but try to be smart and ignore identifiers
that do not look legitimate (sometimes documentation can contain strings matching
our [`AUTO_REF_RE`][mkdocs_autorefs.references.AUTO_REF_RE] regular expression that did not intend to reference anything).
We currently ignore references when their identifier contains a space or a slash.
Arguments:
output: HTML converted from Markdown.
page: The related MkDocs page instance.
kwargs: Additional arguments passed by MkDocs.
Returns:
Modified HTML.
"""
log.debug(f"Fixing references in page {page.file.src_path}")
url_mapper = functools.partial(self.get_item_url, from_url=page.url, fallback=self.get_fallback_anchor)
fixed_output, unmapped = fix_refs(output, url_mapper)
if unmapped and log.isEnabledFor(logging.WARNING):
for ref in unmapped:
log.warning(f"{page.file.src_path}: Could not find cross-reference target '[{ref}]'")
return fixed_output