Skip to content

Commit c65217f

Browse files
committed
Update handler for mkdocstrings 0.28
1 parent 9ca4b25 commit c65217f

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

mkdocstrings_handlers/vba/_handler.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44

55
from __future__ import annotations
66

7-
import copy
8-
from collections import ChainMap
97
from pathlib import Path
108
from typing import (
119
Any,
10+
ClassVar,
1211
MutableMapping,
1312
Dict,
1413
Mapping,
1514
Tuple,
1615
)
1716

1817
from griffe import patch_loggers
19-
from markdown import Markdown
18+
from mkdocs.config.defaults import MkDocsConfig
2019
from mkdocs.exceptions import PluginError
2120
from mkdocstrings.handlers.base import BaseHandler, CollectionError
2221
from mkdocstrings.loggers import get_logger
@@ -107,6 +106,17 @@ def __init__(self, *, base_dir: Path, encoding: str, **kwargs: Any) -> None:
107106
**`docstring_section_style`** | `str` | The style used to render docstring sections. Options: `table`, `list`, `spacy`. | `table`
108107
"""
109108

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+
110120
def collect(
111121
self,
112122
identifier: str,
@@ -141,75 +151,70 @@ def collect(
141151
def render(
142152
self,
143153
data: VbaModuleInfo,
144-
config: Mapping[str, Any],
154+
config: MutableMapping[str, Any],
145155
) -> str:
146-
final_config = ChainMap(dict(copy.deepcopy(config)), self.default_config)
147156
template = self.env.get_template(f"module.html")
148157

149158
# Heading level is a "state" variable, that will change at each step
150159
# of the rendering recursion. Therefore, it's easier to use it as a plain value
151160
# than as an item in a dictionary.
152-
heading_level = final_config["heading_level"]
161+
heading_level = config["heading_level"]
153162
try:
154-
final_config["members_order"] = Order(final_config["members_order"])
163+
config["members_order"] = Order(config["members_order"])
155164
except ValueError:
156165
choices = "', '".join(item.value for item in Order)
157166
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}'."
159168
)
160169

161170
return template.render(
162-
config=final_config,
171+
config=config,
163172
module=data,
164173
heading_level=heading_level,
165174
root=True,
166175
)
167176

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:
170178
self.env.trim_blocks = True
171179
self.env.lstrip_blocks = True
172180
self.env.keep_trailing_newline = False
173181
self.env.filters["crossref"] = do_crossref
174182
self.env.filters["multi_crossref"] = do_multi_crossref
175183
self.env.filters["order_members"] = do_order_members
176184

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 ()
178201
return data.path.as_posix(), *(p.signature.name for p in data.procedures)
179202

180203

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:
189205
"""
190206
Get a new `VbaHandler`.
191207
192208
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.
196209
encoding:
197210
The encoding to use when reading VBA files.
198211
Excel exports .bas and .cls files as `latin1`.
199212
See https://en.wikipedia.org/wiki/ISO/IEC_8859-1 .
213+
tool_config: SSG configuration.
200214
kwargs: Extra keyword arguments that we don't use.
201215
202216
Returns:
203217
An instance of `VbaHandler`.
204218
"""
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)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"setuptools_scm",
2929
],
3030
install_requires=[
31-
"mkdocstrings>=0.26.1,<1",
31+
"mkdocstrings>=0.28,<1",
3232
"griffe>=1.3.1,<2",
3333
"mkdocs-material>=9.2,<10",
3434
],

0 commit comments

Comments
 (0)