Skip to content

Commit 1e98806

Browse files
authored
Merge pull request #23 from mkdocstrings/mkdocstrings-028
Update handler for mkdocstrings 0.28
2 parents 9ca4b25 + 99d3e6e commit 1e98806

File tree

6 files changed

+73
-55
lines changed

6 files changed

+73
-55
lines changed

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: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
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,
1211
MutableMapping,
1312
Dict,
1413
Mapping,
1514
Tuple,
15+
ClassVar,
1616
)
1717

1818
from griffe import patch_loggers
19-
from markdown import Markdown
19+
from mkdocs.config.defaults import MkDocsConfig
2020
from mkdocs.exceptions import PluginError
21-
from mkdocstrings.handlers.base import BaseHandler, CollectionError
21+
from mkdocstrings import BaseHandler, CollectionError
2222
from mkdocstrings.loggers import get_logger
2323

2424
from ._crossref import do_crossref, do_multi_crossref
@@ -51,17 +51,17 @@ def __init__(self, *, base_dir: Path, encoding: str, **kwargs: Any) -> None:
5151
self.base_dir = base_dir
5252
self.encoding = encoding
5353

54-
name: str = "vba"
54+
name: ClassVar[str] = "vba"
5555
"""
5656
The handler's name.
5757
"""
5858

59-
domain: str = "vba"
59+
domain: ClassVar[str] = "vba"
6060
"""
6161
The cross-documentation domain/language for this handler.
6262
"""
6363

64-
fallback_theme = "material"
64+
fallback_theme: ClassVar[str] = "material"
6565
"""
6666
The theme to fall back to.
6767
"""
@@ -107,6 +107,17 @@ def __init__(self, *, base_dir: Path, encoding: str, **kwargs: Any) -> None:
107107
**`docstring_section_style`** | `str` | The style used to render docstring sections. Options: `table`, `list`, `spacy`. | `table`
108108
"""
109109

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+
110121
def collect(
111122
self,
112123
identifier: str,
@@ -141,75 +152,81 @@ def collect(
141152
def render(
142153
self,
143154
data: VbaModuleInfo,
144-
config: Mapping[str, Any],
155+
options: MutableMapping[str, Any],
156+
*,
157+
locale: str | None = None,
145158
) -> str:
146-
final_config = ChainMap(dict(copy.deepcopy(config)), self.default_config)
147159
template = self.env.get_template(f"module.html")
148160

149161
# Heading level is a "state" variable, that will change at each step
150162
# of the rendering recursion. Therefore, it's easier to use it as a plain value
151163
# than as an item in a dictionary.
152-
heading_level = final_config["heading_level"]
164+
heading_level = options["heading_level"]
153165
try:
154-
final_config["members_order"] = Order(final_config["members_order"])
166+
options["members_order"] = Order(options["members_order"])
155167
except ValueError:
156168
choices = "', '".join(item.value for item in Order)
157169
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}'."
159171
)
160172

161173
return template.render(
162-
config=final_config,
174+
config=options,
163175
module=data,
164176
heading_level=heading_level,
165177
root=True,
166178
)
167179

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:
170181
self.env.trim_blocks = True
171182
self.env.lstrip_blocks = True
172183
self.env.keep_trailing_newline = False
173184
self.env.filters["crossref"] = do_crossref
174185
self.env.filters["multi_crossref"] = do_multi_crossref
175186
self.env.filters["order_members"] = do_order_members
176187

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

180206

181207
def get_handler(
182208
*,
183-
theme: str = "material",
184-
custom_templates: str | None = None,
185-
config_file_path: str | None = None,
186209
encoding: str = "latin1",
210+
tool_config: MkDocsConfig | None = None,
187211
**kwargs: Any,
188212
) -> VbaHandler:
189213
"""
190214
Get a new `VbaHandler`.
191215
192216
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.
196217
encoding:
197218
The encoding to use when reading VBA files.
198219
Excel exports .bas and .cls files as `latin1`.
199220
See https://en.wikipedia.org/wiki/ISO/IEC_8859-1 .
221+
tool_config: SSG configuration.
200222
kwargs: Extra keyword arguments that we don't use.
201223
202224
Returns:
203225
An instance of `VbaHandler`.
204226
"""
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
215231
)
232+
return VbaHandler(base_dir=base_dir, encoding=encoding, **kwargs)

mkdocstrings_handlers/vba/_util.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import List, Generator
2+
from typing import List, Generator, Tuple
33

44
from griffe import Docstring, Function, Parameters, Parameter, Parser
55

@@ -132,29 +132,30 @@ def parse_signature(line: str) -> VbaSignatureInfo:
132132

133133
def find_procedures(code: str) -> Generator[VbaProcedureInfo, None, None]:
134134
lines = code.splitlines()
135-
procedure = None
135+
136+
procedure: Tuple[VbaSignatureInfo, int] | None = None
137+
"""
138+
The signature and first line number of the procedure currently being parsed,
139+
or None when scanning for the start of the next procedure.
140+
"""
136141

137142
for i, line in enumerate(lines):
138143
if procedure is None:
139144
# Looking for signature. Ignore everything until we find one.
140145
if not is_signature(line):
141146
continue
142147

143-
procedure = {
144-
"signature": parse_signature(line),
145-
"first_line": i + 1,
146-
}
148+
procedure = parse_signature(line), i + 1
147149
continue
148150

149151
if is_end(line):
150152
# Found the end of a procedure.
151-
procedure["last_line"] = i + 1
153+
signature, first_line = procedure
154+
last_line = i + 1
152155

153156
# The docstring consists of the comment lines directly after the signature.
154157
docstring_lines = []
155-
procedure_source = lines[
156-
procedure["first_line"] - 1 : procedure["last_line"] - 1
157-
]
158+
procedure_source = lines[first_line - 1 : last_line - 1]
158159
for source_line in procedure_source[1:]:
159160
if not is_comment(source_line):
160161
break
@@ -167,29 +168,29 @@ def find_procedures(code: str) -> Generator[VbaProcedureInfo, None, None]:
167168
value=docstring_value,
168169
parser=Parser.google,
169170
parser_options={},
170-
lineno=procedure["first_line"] + 1,
171+
lineno=first_line + 1,
171172
parent=Function(
172-
name=procedure["signature"].name,
173+
name=signature.name,
173174
parameters=Parameters(
174175
*(
175176
Parameter(
176177
name=arg.name,
177178
annotation=arg.arg_type,
178179
default=arg.default,
179180
)
180-
for arg in procedure["signature"].args
181+
for arg in signature.args
181182
)
182183
),
183-
returns=procedure["signature"].return_type,
184+
returns=signature.return_type,
184185
),
185186
)
186187

187188
# Yield it and start over.
188189
yield VbaProcedureInfo(
189-
signature=procedure["signature"],
190+
signature=signature,
190191
docstring=docstring,
191-
first_line=procedure["first_line"],
192-
last_line=procedure["last_line"],
192+
first_line=first_line,
193+
last_line=last_line,
193194
source=procedure_source,
194195
)
195196
procedure = None

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
],

test/handler/test_collect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def _test_collect(*, write_bytes: bytes, read_encoding: str) -> VbaModuleInfo:
1212
with TemporaryDirectory() as tmp_dir_str:
1313
tmp_dir = Path(tmp_dir_str)
14-
handler = get_handler(encoding=read_encoding)
14+
handler = get_handler(encoding=read_encoding, mdx=[], mdx_config={})
1515
p = tmp_dir / "source.bas"
1616
p.write_bytes(write_bytes)
1717
return handler.collect(identifier=p.as_posix(), config={})

0 commit comments

Comments
 (0)