|
5 | 5 |
|
6 | 6 | # Introduction |
7 | 7 |
|
8 | | -To be able to use macros inside docstrings, we need to integrate the |
9 | | -`mkdocs-macros` extension into `mkdocstrings`. This module provides the |
10 | | -necessary functions to make this integration work. |
| 8 | +To be able to use macros inside docstrings, we need to integrate the `mkdocs-macros` |
| 9 | +extension into `mkdocstrings`. This module provides the necessary functions to make this |
| 10 | +integration work. |
11 | 11 |
|
12 | | -To use this module, add the following configuration to your `mkdocs.yml`: |
| 12 | +
|
| 13 | +# Basic usage |
| 14 | +
|
| 15 | +If you don't want to define your own macros, variables or filters, you can use this |
| 16 | +module as a [pluglet](https://mkdocs-macros-plugin.readthedocs.io/en/latest/pluglets/): |
13 | 17 |
|
14 | 18 | ```yaml title="mkdocs.yml" |
15 | 19 | plugins: |
|
18 | 22 | default_handler: python |
19 | 23 | # ... |
20 | 24 | - macros: |
21 | | - module_name: path/to/macros |
| 25 | + modules: ["frequenz.repo.config.mkdocs.mkdocstrings_macros"] |
22 | 26 | on_undefined: strict |
23 | 27 | on_error_fail: true |
24 | 28 | ``` |
25 | 29 |
|
26 | | -Then you need to add the `path/to/macros.py` file. The contents will vary depending on |
27 | | -if you want to use use the convenience default hooking or not. |
28 | | -
|
29 | | -# Basic usage |
30 | | -
|
31 | | -A convenience [`define_env()`] function is provided to provide a [macros |
32 | | -extension](https://mkdocs-macros-plugin.readthedocs.io/en/latest/macros/) to do the |
33 | | -hooking and provide some useful variables and filters: |
| 30 | +This will do the hooking and provide some useful variables and filters: |
34 | 31 |
|
35 | 32 | * [`slugify`][frequenz.repo.config.mkdocs.mkdocstrings_macros.slugify]: A filter to |
36 | 33 | slugify a text. Useful to generate anchors for headings. |
|
48 | 45 | requirement for a branch, you need to provide a repository URL, please read the |
49 | 46 | [Advanced usage] section for more details. |
50 | 47 |
|
51 | | -If you are happy with the defaults, your `path/to/macros.py` can look as simple as: |
52 | | -
|
53 | | -```py title="path/to/macros.py" |
54 | | -from frequenz.repo.config.mkdocs.mkdocstrings_macros import define_env |
55 | | -``` |
56 | 48 |
|
57 | 49 | # Customized usage |
58 | 50 |
|
59 | 51 | 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. |
| 52 | +the default behaviour described in [Basic usage], you need to provide your own [macros |
| 53 | +module](https://mkdocs-macros-plugin.readthedocs.io/en/latest/macros/) with |
| 54 | +a `define_env()` function. You can specify it in the `mkdocs.yml` configuration file: |
62 | 55 |
|
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. |
| 56 | +```yaml title="mkdocs.yml" |
| 57 | +plugins: |
| 58 | + # Order is important! mkdocstrings must come before macros! |
| 59 | + - mkdocstrings: |
| 60 | + default_handler: python |
| 61 | + # ... |
| 62 | + - macros: |
| 63 | + module_name: "path/to/macros" # Note: no .py extension here! |
| 64 | + on_undefined: strict |
| 65 | + on_error_fail: true |
| 66 | +``` |
| 67 | +
|
| 68 | +Then you need to add the `define_env()` function to the `path/to/macros.py` file. |
| 69 | +A convenience [`hook_env_with_everything()`] is provided to pull all the same default |
| 70 | +variables and filters and call the hooking function at the end as with the *pluglet*. |
| 71 | +
|
| 72 | +You also need to make sure to call the function at the end, after you define your own |
| 73 | +variables, filters and macros. You can optionally pass a `repo_url` in this case so the |
| 74 | +`version_requirement` variable can work when the current version is a branch. |
67 | 75 |
|
68 | 76 | Here is an example of how to do it: |
69 | 77 |
|
|
72 | 80 |
|
73 | 81 | def define_env(env: macros.MacrosPlugin) -> None: |
74 | 82 | env.variables.my_var = "Example" |
| 83 | +
|
| 84 | + # This hook needs to be done at the end of the `define_env` function. |
75 | 85 | hook_env_with_everything(env, "git+https://your-repo-url") |
76 | 86 | ``` |
77 | 87 |
|
78 | 88 | # Advanced usage |
79 | 89 |
|
80 | 90 | 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. |
| 91 | +your own `define_env()` function and do the same configuration in the `mkdocs.yml` file |
| 92 | +as in the [Customized usage] section, but instead call the |
| 93 | +[`hook_macros_plugin()`][frequenz.repo.config.mkdocs.mkdocstrings_macros.hook_macros_plugin] |
| 94 | +at the end. |
83 | 95 |
|
84 | 96 | Here is an example of how to do it: |
85 | 97 |
|
|
0 commit comments