55
66import os
77import pathlib
8- from typing import Any
98
10- import markdown as md
11- from markdown .extensions import toc
12- from mkdocs_macros import plugin as macros
13-
14- _CODE_ANNOTATION_MARKER : str = (
15- r'<span class="md-annotation">'
16- r'<span class="md-annotation__index" tabindex="-1">'
17- r'<span data-md-annotation-id="1"></span>'
18- r"</span>"
19- r"</span>"
9+ from frequenz .repo .config .mkdocs .mkdocstrings_macros import (
10+ hook_env_with_everything ,
11+ slugify ,
2012)
21-
22-
23- def _slugify (text : str ) -> str :
24- """Slugify a text.
25-
26- Args:
27- text: The text to slugify.
28-
29- Returns:
30- The slugified text.
31- """
32- return toc .slugify_unicode (text , "-" )
33-
34-
35- def _hook_macros_plugin (env : macros .MacrosPlugin ) -> None :
36- """Integrate the `mkdocs-macros` plugin into `mkdocstrings`.
37-
38- This is a temporary workaround to make `mkdocs-macros` work with
39- `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
40- https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
41-
42- Args:
43- env: The environment to hook the plugin into.
44- """
45- # get mkdocstrings' Python handler
46- python_handler = env .conf ["plugins" ]["mkdocstrings" ].get_handler ("python" )
47-
48- # get the `update_env` method of the Python handler
49- update_env = python_handler .update_env
50-
51- # override the `update_env` method of the Python handler
52- def patched_update_env (markdown : md .Markdown , config : dict [str , Any ]) -> None :
53- update_env (markdown , config )
54-
55- # get the `convert_markdown` filter of the env
56- convert_markdown = python_handler .env .filters ["convert_markdown" ]
57-
58- # build a chimera made of macros+mkdocstrings
59- def render_convert (markdown : str , * args : Any , ** kwargs : Any ) -> Any :
60- return convert_markdown (env .render (markdown ), * args , ** kwargs )
61-
62- # patch the filter
63- python_handler .env .filters ["convert_markdown" ] = render_convert
64-
65- # patch the method
66- python_handler .update_env = patched_update_env
13+ from mkdocs_macros import plugin as macros
6714
6815
6916def define_env (env : macros .MacrosPlugin ) -> None :
@@ -72,9 +19,6 @@ def define_env(env: macros.MacrosPlugin) -> None:
7219 Args:
7320 env: The environment to define the macro functions in.
7421 """
75- # A variable to easily show an example code annotation from mkdocs-material.
76- # https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
77- env .variables ["code_annotation_marker" ] = _CODE_ANNOTATION_MARKER
7822
7923 @env .macro # type: ignore[misc]
8024 def glossary (term : str , text : str | None = None ) -> str :
@@ -94,7 +38,7 @@ def glossary(term: str, text: str | None = None) -> str:
9438 # always the case, for example when referencing the glossary from the API
9539 # reference.
9640 link_path = os .path .relpath (glossary_path , current_path .parent )
97- return f"[{ text or term } ]({ link_path } #{ _slugify (term )} )"
41+ return f"[{ text or term } ]({ link_path } #{ slugify (term )} )"
9842
99- # This hook needs to be done at the end of the `define_env` function.
100- _hook_macros_plugin (env )
43+ # This must be at the end to enable all standard features
44+ hook_env_with_everything (env )
0 commit comments