|
54 | 54 | from frequenz.repo.config.mkdocs.mkdocstrings_macros import define_env |
55 | 55 | ``` |
56 | 56 |
|
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. |
58 | 62 |
|
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. |
62 | 67 |
|
63 | 68 | Here is an example of how to do it: |
64 | 69 |
|
65 | 70 | ```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 |
67 | 72 |
|
68 | 73 | def define_env(env: macros.MacrosPlugin) -> None: |
69 | 74 | 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. |
71 | 83 |
|
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" |
73 | 91 |
|
74 | 92 | # This hook needs to be done at the end of the `define_env` function. |
75 | 93 | hook_macros_plugin(env) |
@@ -187,16 +205,35 @@ def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any: |
187 | 205 | python_handler.update_env = patched_update_env |
188 | 206 |
|
189 | 207 |
|
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. |
192 | 217 |
|
193 | 218 | 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. |
195 | 223 | """ |
196 | 224 | env.variables.code_annotation_marker = CODE_ANNOTATION_MARKER |
197 | | - add_version_variables(env) |
| 225 | + add_version_variables(env, repo_url=repo_url) |
198 | 226 |
|
199 | 227 | env.filter(slugify, "slugify") # type: ignore[no-untyped-call] |
200 | 228 |
|
201 | 229 | # This hook needs to be done at the end of the `define_env` function. |
202 | 230 | 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