Skip to content

Commit 5d45e44

Browse files
pawamoyrudolfbyker
authored andcommitted
Update handler for mkdocstrings 0.28
1 parent 9ca4b25 commit 5d45e44

4 files changed

Lines changed: 52 additions & 36 deletions

File tree

ci-constraints.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
mkdocstrings==0.26.1
2-
griffe==1.3.1
1+
mkdocstrings==0.30.0
2+
griffe==1.12.1
33
mkdocs-material==9.2.1

mkdocstrings_handlers/vba/_handler.py

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
from __future__ import annotations
66

7-
import copy
8-
from collections import ChainMap
7+
from copy import deepcopy
98
from pathlib import Path
109
from typing import (
1110
Any,
@@ -16,9 +15,9 @@
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
21-
from mkdocstrings.handlers.base import BaseHandler, CollectionError
20+
from mkdocstrings import BaseHandler, CollectionError
2221
from mkdocstrings.loggers import get_logger
2322

2423
from ._crossref import do_crossref, do_multi_crossref
@@ -51,12 +50,12 @@ def __init__(self, *, base_dir: Path, encoding: str, **kwargs: Any) -> None:
5150
self.base_dir = base_dir
5251
self.encoding = encoding
5352

54-
name: str = "vba"
53+
name: str = "vba" # type: ignore[misc]
5554
"""
5655
The handler's name.
5756
"""
5857

59-
domain: str = "vba"
58+
domain: str = "vba" # type: ignore[misc]
6059
"""
6160
The cross-documentation domain/language for this handler.
6261
"""
@@ -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 deepcopy({**self.default_config, **local_options})
119+
110120
def collect(
111121
self,
112122
identifier: str,
@@ -141,75 +151,81 @@ def collect(
141151
def render(
142152
self,
143153
data: VbaModuleInfo,
144-
config: Mapping[str, Any],
154+
options: MutableMapping[str, Any],
155+
*,
156+
locale: str | None = None,
145157
) -> str:
146-
final_config = ChainMap(dict(copy.deepcopy(config)), self.default_config)
147158
template = self.env.get_template(f"module.html")
148159

149160
# Heading level is a "state" variable, that will change at each step
150161
# of the rendering recursion. Therefore, it's easier to use it as a plain value
151162
# than as an item in a dictionary.
152-
heading_level = final_config["heading_level"]
163+
heading_level = options["heading_level"]
153164
try:
154-
final_config["members_order"] = Order(final_config["members_order"])
165+
options["members_order"] = Order(options["members_order"])
155166
except ValueError:
156167
choices = "', '".join(item.value for item in Order)
157168
raise PluginError(
158-
f"Unknown members_order '{final_config['members_order']}', choose between '{choices}'."
169+
f"Unknown members_order '{options['members_order']}', choose between '{choices}'."
159170
)
160171

161172
return template.render(
162-
config=final_config,
173+
config=options,
163174
module=data,
164175
heading_level=heading_level,
165176
root=True,
166177
)
167178

168-
def update_env(self, md: Markdown, config: Dict[Any, Any]) -> None:
169-
super().update_env(md, config)
179+
def update_env(self, config: Dict[Any, Any]) -> None:
170180
self.env.trim_blocks = True
171181
self.env.lstrip_blocks = True
172182
self.env.keep_trailing_newline = False
173183
self.env.filters["crossref"] = do_crossref
174184
self.env.filters["multi_crossref"] = do_multi_crossref
175185
self.env.filters["order_members"] = do_order_members
176186

177-
def get_anchors(self, data: VbaModuleInfo) -> Tuple[str, ...]:
187+
def get_aliases(self, identifier: str) -> Tuple[str, ...]:
188+
"""Get the aliases of the given identifier.
189+
190+
Aliases are used to register secondary URLs in mkdocs-autorefs,
191+
to make sure cross-references work with any location of the same object.
192+
193+
Arguments:
194+
identifier: The identifier to get aliases for.
195+
196+
Returns:
197+
A tuple of aliases for the given identifier.
198+
"""
199+
try:
200+
data = self.collect(identifier, {})
201+
except CollectionError:
202+
return ()
178203
return data.path.as_posix(), *(p.signature.name for p in data.procedures)
179204

180205

181206
def get_handler(
182207
*,
183-
theme: str = "material",
184-
custom_templates: str | None = None,
185-
config_file_path: str | None = None,
186208
encoding: str = "latin1",
209+
tool_config: MkDocsConfig | None = None,
187210
**kwargs: Any,
188211
) -> VbaHandler:
189212
"""
190213
Get a new `VbaHandler`.
191214
192215
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.
196216
encoding:
197217
The encoding to use when reading VBA files.
198218
Excel exports .bas and .cls files as `latin1`.
199219
See https://en.wikipedia.org/wiki/ISO/IEC_8859-1 .
220+
tool_config: SSG configuration.
200221
kwargs: Extra keyword arguments that we don't use.
201222
202223
Returns:
203224
An instance of `VbaHandler`.
204225
"""
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,
226+
base_dir = (
227+
Path(getattr(tool_config, "config_file_path", None) or "./mkdocs.yml")
228+
.resolve()
229+
.parent
215230
)
231+
return VbaHandler(base_dir=base_dir, encoding=encoding, **kwargs)

mypy-requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
mypy==1.11.2
2-
types-setuptools==75.*
1+
mypy==1.17.*
2+
types-setuptools==80.*
33
types-Markdown==3.*

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.30,<1",
3232
"griffe>=1.3.1,<2",
3333
"mkdocs-material>=9.2,<10",
3434
],

0 commit comments

Comments
 (0)