|
4 | 4 |
|
5 | 5 | from __future__ import annotations |
6 | 6 |
|
7 | | -import copy |
8 | | -from collections import ChainMap |
| 7 | +from copy import deepcopy |
9 | 8 | from pathlib import Path |
10 | 9 | from typing import ( |
11 | 10 | Any, |
12 | 11 | MutableMapping, |
13 | 12 | Dict, |
14 | 13 | Mapping, |
15 | 14 | Tuple, |
| 15 | + ClassVar, |
16 | 16 | ) |
17 | 17 |
|
18 | 18 | from griffe import patch_loggers |
19 | | -from markdown import Markdown |
| 19 | +from mkdocs.config.defaults import MkDocsConfig |
20 | 20 | from mkdocs.exceptions import PluginError |
21 | | -from mkdocstrings.handlers.base import BaseHandler, CollectionError |
| 21 | +from mkdocstrings import BaseHandler, CollectionError |
22 | 22 | from mkdocstrings.loggers import get_logger |
23 | 23 |
|
24 | 24 | from ._crossref import do_crossref, do_multi_crossref |
@@ -51,17 +51,17 @@ def __init__(self, *, base_dir: Path, encoding: str, **kwargs: Any) -> None: |
51 | 51 | self.base_dir = base_dir |
52 | 52 | self.encoding = encoding |
53 | 53 |
|
54 | | - name: str = "vba" |
| 54 | + name: ClassVar[str] = "vba" |
55 | 55 | """ |
56 | 56 | The handler's name. |
57 | 57 | """ |
58 | 58 |
|
59 | | - domain: str = "vba" |
| 59 | + domain: ClassVar[str] = "vba" |
60 | 60 | """ |
61 | 61 | The cross-documentation domain/language for this handler. |
62 | 62 | """ |
63 | 63 |
|
64 | | - fallback_theme = "material" |
| 64 | + fallback_theme: ClassVar[str] = "material" |
65 | 65 | """ |
66 | 66 | The theme to fall back to. |
67 | 67 | """ |
@@ -107,6 +107,17 @@ def __init__(self, *, base_dir: Path, encoding: str, **kwargs: Any) -> None: |
107 | 107 | **`docstring_section_style`** | `str` | The style used to render docstring sections. Options: `table`, `list`, `spacy`. | `table` |
108 | 108 | """ |
109 | 109 |
|
| 110 | + def get_options(self, local_options: Mapping[str, Any]) -> Dict[str, Any]: |
| 111 | + """Combine the default options with the local options. |
| 112 | +
|
| 113 | + Arguments: |
| 114 | + local_options: The options provided in Markdown pages. |
| 115 | +
|
| 116 | + Returns: |
| 117 | + The combined options. |
| 118 | + """ |
| 119 | + return deepcopy({**self.default_config, **local_options}) |
| 120 | + |
110 | 121 | def collect( |
111 | 122 | self, |
112 | 123 | identifier: str, |
@@ -141,75 +152,81 @@ def collect( |
141 | 152 | def render( |
142 | 153 | self, |
143 | 154 | data: VbaModuleInfo, |
144 | | - config: Mapping[str, Any], |
| 155 | + options: MutableMapping[str, Any], |
| 156 | + *, |
| 157 | + locale: str | None = None, |
145 | 158 | ) -> str: |
146 | | - final_config = ChainMap(dict(copy.deepcopy(config)), self.default_config) |
147 | 159 | template = self.env.get_template(f"module.html") |
148 | 160 |
|
149 | 161 | # Heading level is a "state" variable, that will change at each step |
150 | 162 | # of the rendering recursion. Therefore, it's easier to use it as a plain value |
151 | 163 | # than as an item in a dictionary. |
152 | | - heading_level = final_config["heading_level"] |
| 164 | + heading_level = options["heading_level"] |
153 | 165 | try: |
154 | | - final_config["members_order"] = Order(final_config["members_order"]) |
| 166 | + options["members_order"] = Order(options["members_order"]) |
155 | 167 | except ValueError: |
156 | 168 | choices = "', '".join(item.value for item in Order) |
157 | 169 | raise PluginError( |
158 | | - f"Unknown members_order '{final_config['members_order']}', choose between '{choices}'." |
| 170 | + f"Unknown members_order '{options['members_order']}', choose between '{choices}'." |
159 | 171 | ) |
160 | 172 |
|
161 | 173 | return template.render( |
162 | | - config=final_config, |
| 174 | + config=options, |
163 | 175 | module=data, |
164 | 176 | heading_level=heading_level, |
165 | 177 | root=True, |
166 | 178 | ) |
167 | 179 |
|
168 | | - def update_env(self, md: Markdown, config: Dict[Any, Any]) -> None: |
169 | | - super().update_env(md, config) |
| 180 | + def update_env(self, config: Dict[Any, Any]) -> None: |
170 | 181 | self.env.trim_blocks = True |
171 | 182 | self.env.lstrip_blocks = True |
172 | 183 | self.env.keep_trailing_newline = False |
173 | 184 | self.env.filters["crossref"] = do_crossref |
174 | 185 | self.env.filters["multi_crossref"] = do_multi_crossref |
175 | 186 | self.env.filters["order_members"] = do_order_members |
176 | 187 |
|
177 | | - def get_anchors(self, data: VbaModuleInfo) -> Tuple[str, ...]: |
| 188 | + def get_aliases(self, identifier: str) -> Tuple[str, ...]: |
| 189 | + """Get the aliases of the given identifier. |
| 190 | +
|
| 191 | + Aliases are used to register secondary URLs in mkdocs-autorefs, |
| 192 | + to make sure cross-references work with any location of the same object. |
| 193 | +
|
| 194 | + Arguments: |
| 195 | + identifier: The identifier to get aliases for. |
| 196 | +
|
| 197 | + Returns: |
| 198 | + A tuple of aliases for the given identifier. |
| 199 | + """ |
| 200 | + try: |
| 201 | + data = self.collect(identifier, {}) |
| 202 | + except CollectionError: |
| 203 | + return () |
178 | 204 | return data.path.as_posix(), *(p.signature.name for p in data.procedures) |
179 | 205 |
|
180 | 206 |
|
181 | 207 | def get_handler( |
182 | 208 | *, |
183 | | - theme: str = "material", |
184 | | - custom_templates: str | None = None, |
185 | | - config_file_path: str | None = None, |
186 | 209 | encoding: str = "latin1", |
| 210 | + tool_config: MkDocsConfig | None = None, |
187 | 211 | **kwargs: Any, |
188 | 212 | ) -> VbaHandler: |
189 | 213 | """ |
190 | 214 | Get a new `VbaHandler`. |
191 | 215 |
|
192 | 216 | Arguments: |
193 | | - theme: The theme to use when rendering contents. |
194 | | - custom_templates: Directory containing custom templates. |
195 | | - config_file_path: The MkDocs configuration file path. |
196 | 217 | encoding: |
197 | 218 | The encoding to use when reading VBA files. |
198 | 219 | Excel exports .bas and .cls files as `latin1`. |
199 | 220 | See https://en.wikipedia.org/wiki/ISO/IEC_8859-1 . |
| 221 | + tool_config: SSG configuration. |
200 | 222 | kwargs: Extra keyword arguments that we don't use. |
201 | 223 |
|
202 | 224 | Returns: |
203 | 225 | An instance of `VbaHandler`. |
204 | 226 | """ |
205 | | - return VbaHandler( |
206 | | - base_dir=( |
207 | | - Path(config_file_path).resolve().parent |
208 | | - if config_file_path |
209 | | - else Path(".").resolve() |
210 | | - ), |
211 | | - encoding=encoding, |
212 | | - handler="vba", |
213 | | - theme=theme, |
214 | | - custom_templates=custom_templates, |
| 227 | + base_dir = ( |
| 228 | + Path(getattr(tool_config, "config_file_path", None) or "./mkdocs.yml") |
| 229 | + .resolve() |
| 230 | + .parent |
215 | 231 | ) |
| 232 | + return VbaHandler(base_dir=base_dir, encoding=encoding, **kwargs) |
0 commit comments