Skip to content

Commit 2380404

Browse files
committed
Add a function to get the default behaviour
The new function `hook_env_with_everything()` will do all the default hooking, so it is easier to define a custom `define_env()` where only a few macros are added, but still gets all the variables macros and filters provided by this module. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent b92cf61 commit 2380404

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

src/frequenz/repo/config/mkdocs/mkdocstrings_macros.py

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,40 @@
5454
from frequenz.repo.config.mkdocs.mkdocstrings_macros import define_env
5555
```
5656
57-
# Advanced usage
57+
# Customized usage
58+
59+
If you want to define your own macros, variables or filters, but you also want to get
60+
the default behaviour described in [Basic usage], you can define your own
61+
`define_env()` function and call the [`hook_env_with_everything()`] function at the end.
5862
59-
If you want to define your own macros, variables or filters, you'll need to provide your
60-
own `define_env()` function, and call the hook to integrate with `mkdocstrings` at the
61-
end.
63+
This function will add all the default variables and filters and call do the hooking as
64+
described in [Basic usage]. You just need to be sure to call the function at the end.
65+
You can also pass a `repo_url` in this case so the `version_requirement` variable can
66+
work when the current version is a branch.
6267
6368
Here is an example of how to do it:
6469
6570
```py title="path/to/macros.py"
66-
from frequenz.repo.config.mkdocs.mkdocstrings_macros import hook_macros_plugin, slugify
71+
from frequenz.repo.config.mkdocs.mkdocstrings_macros import hook_env_with_everything
6772
6873
def define_env(env: macros.MacrosPlugin) -> None:
6974
env.variables.my_var = "Example"
70-
add_version_variables(env, "git+https://your-repo-url")
75+
hook_env_with_everything(env, "git+https://your-repo-url")
76+
```
77+
78+
# Advanced usage
79+
80+
If you don't want to pull in all the default variables and filters, you can still define
81+
your own `define_env()` function, and call the hook to integrate with `mkdocstrings` at
82+
the end.
7183
72-
env.filter(slugify, "slugify")
84+
Here is an example of how to do it:
85+
86+
```py title="path/to/macros.py"
87+
from frequenz.repo.config.mkdocs.mkdocstrings_macros import hook_macros_plugin
88+
89+
def define_env(env: macros.MacrosPlugin) -> None:
90+
env.variables.my_var = "Example"
7391
7492
# This hook needs to be done at the end of the `define_env` function.
7593
hook_macros_plugin(env)
@@ -187,16 +205,35 @@ def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any:
187205
python_handler.update_env = patched_update_env
188206

189207

190-
def define_env(env: macros.MacrosPlugin) -> None:
191-
"""Define the hook to create macro functions for use in Markdown.
208+
def hook_env_with_everything(
209+
env: macros.MacrosPlugin, *, repo_url: str | None = None
210+
) -> None:
211+
"""Hooks the `env` with all the default variables and filters.
212+
213+
This function is a convenience function that adds all variables and filters and
214+
macros provided by this module and calls
215+
[`hook_macros_plugin()`][frequenz.repo.config.mkdocs.mkdocstrings_macros.hook_macros_plugin]
216+
at the end.
192217
193218
Args:
194-
env: The environment to define the macro functions in.
219+
env: The environment to hook.
220+
repo_url: The URL of the repository to use in the `version_requirement`
221+
variable. Only needed if you want to use the `version_requirement` variable
222+
for branches.
195223
"""
196224
env.variables.code_annotation_marker = CODE_ANNOTATION_MARKER
197-
add_version_variables(env)
225+
add_version_variables(env, repo_url=repo_url)
198226

199227
env.filter(slugify, "slugify") # type: ignore[no-untyped-call]
200228

201229
# This hook needs to be done at the end of the `define_env` function.
202230
hook_macros_plugin(env)
231+
232+
233+
def define_env(env: macros.MacrosPlugin) -> None:
234+
"""Define the hook to create macro functions for use in Markdown.
235+
236+
Args:
237+
env: The environment to define the macro functions in.
238+
"""
239+
hook_env_with_everything(env)

0 commit comments

Comments
 (0)