Skip to content

Commit 2c8f61f

Browse files
authored
Add mkdocs-macros plugin (#667)
This plugin allow us to avoid repetition by defining variables that can be used throughout the documentation or even functions to automate certain things. Also add a mkdocs macro to link to the glossary. Documentation in both `docs/` or the code can link to glossary terms by just using `{{glossary("Term")}}`.
2 parents 75e80de + 6c066a4 commit 2c8f61f

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

docs/_scripts/macros.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# License: MIT
2+
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
3+
4+
"""This module defines macros for use in Markdown files."""
5+
6+
import pathlib
7+
8+
from markdown.extensions import toc
9+
from mkdocs_macros import plugin as macros
10+
11+
12+
def define_env(env: macros.MacrosPlugin) -> None:
13+
"""Define the hook to create macro functions for use in Markdown.
14+
15+
Args:
16+
env: The environment to define the macro functions in.
17+
"""
18+
19+
def _slugify(text: str) -> str:
20+
"""Slugify a text.
21+
22+
Args:
23+
text: The text to slugify.
24+
25+
Returns:
26+
The slugified text.
27+
"""
28+
# The type of the return value is not defined for the markdown library.
29+
# Also for some reason `mypy` thinks the `toc` module doesn't have a
30+
# `slugify_unicode` function, but it definitely does.
31+
return toc.slugify_unicode(text, "-") # type: ignore[attr-defined,no-any-return]
32+
33+
@env.macro # type: ignore[misc]
34+
def glossary(term: str) -> str:
35+
"""Create a link to the glossary entry for the given term.
36+
37+
Args:
38+
term: The term to link to.
39+
40+
Returns:
41+
The Markdown link to the glossary entry for the given term.
42+
"""
43+
current_path = pathlib.Path(env.page.file.src_uri)
44+
glossary_path = pathlib.Path("intro/glossary.md")
45+
link_path = glossary_path.relative_to(current_path.parent)
46+
return f"[{term}]({link_path}#{_slugify(term)})"

docs/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ python3 -m venv .venv
4949

5050
This will create the virtual environment and activate it automatically for you.
5151

52-
Now you can install the SDK by using `pip`:
52+
Now you can install the SDK by using `pip` (if you don't have `pip` installed
53+
you can follow [the official
54+
instructions](https://pip.pypa.io/en/stable/installation/)):
5355

5456
```sh
5557
python3 -m pip install frequenz-sdk
@@ -63,5 +65,5 @@ $ python3
6365
Python 3.11.4 (main, Jun 7 2023, 10:13:09) [GCC 12.2.0] on linux
6466
Type "help", "copyright", "credits" or "license" for more information.
6567
>>> import frequenz.sdk
66-
>>>
68+
>>>
6769
```

mkdocs.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,13 @@ markdown_extensions:
8585
plugins:
8686
- gen-files:
8787
scripts:
88-
- docs/mkdocstrings_autoapi.py
88+
- docs/_scripts/mkdocstrings_autoapi.py
8989
- literate-nav:
9090
nav_file: SUMMARY.md
91+
- macros:
92+
module_name: docs/_scripts/macros
93+
on_undefined: strict
94+
on_error_fail: true
9195
- mike:
9296
canonical_version: latest
9397
- mkdocstrings:
@@ -120,5 +124,4 @@ plugins:
120124
# Preview controls
121125
watch:
122126
- "src"
123-
- README.md
124127
- CONTRIBUTING.md

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ dev-mkdocs = [
6262
"mike == 1.1.2",
6363
"mkdocs-gen-files == 0.5.0",
6464
"mkdocs-literate-nav == 0.6.1",
65+
"mkdocs-macros-plugin == 1.0.4",
6566
"mkdocs-material == 9.3.1",
66-
"mkdocs-section-index == 0.3.7",
6767
"mkdocstrings[python] == 0.23.0",
6868
"frequenz-repo-config[lib] == 0.6.1",
6969
]
@@ -176,6 +176,7 @@ module = [
176176
"async_solipsism.*",
177177
"grpc.aio",
178178
"grpc.aio.*",
179+
"mkdocs_macros.*",
179180
# There is a stubs package available, but it's not working:
180181
# https://github.com/eggplants/networkx-stubs/issues/1
181182
"networkx",

0 commit comments

Comments
 (0)