Skip to content

Commit 30cc92a

Browse files
committed
Update documentation with a simplified setup
For the basic usage we can actually use the module itself as a *pluglet*: https://mkdocs-macros-plugin.readthedocs.io/en/latest/pluglets/ Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 2380404 commit 30cc92a

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

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

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
66
# Introduction
77
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.
1111
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/):
1317
1418
```yaml title="mkdocs.yml"
1519
plugins:
@@ -18,19 +22,12 @@
1822
default_handler: python
1923
# ...
2024
- macros:
21-
module_name: path/to/macros
25+
modules: ["frequenz.repo.config.mkdocs.mkdocstrings_macros"]
2226
on_undefined: strict
2327
on_error_fail: true
2428
```
2529
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:
3431
3532
* [`slugify`][frequenz.repo.config.mkdocs.mkdocstrings_macros.slugify]: A filter to
3633
slugify a text. Useful to generate anchors for headings.
@@ -48,22 +45,33 @@
4845
requirement for a branch, you need to provide a repository URL, please read the
4946
[Advanced usage] section for more details.
5047
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-
```
5648
5749
# Customized usage
5850
5951
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:
6255
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.
6775
6876
Here is an example of how to do it:
6977
@@ -72,14 +80,18 @@
7280
7381
def define_env(env: macros.MacrosPlugin) -> None:
7482
env.variables.my_var = "Example"
83+
84+
# This hook needs to be done at the end of the `define_env` function.
7585
hook_env_with_everything(env, "git+https://your-repo-url")
7686
```
7787
7888
# Advanced usage
7989
8090
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.
8395
8496
Here is an example of how to do it:
8597

0 commit comments

Comments
 (0)