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