Skip to content

Commit 8cdca51

Browse files
committed
Move the hooking of mkdocstrings and macros to a function
This code is pure noise for users in the `define_env()` function, so we abstract it away and move it into its own function so the noise is reduced to the minimum. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 4ff31e1 commit 8cdca51

File tree

7 files changed

+159
-89
lines changed
  • cookiecutter/{{cookiecutter.github_repo_name}}/docs/_scripts
  • docs/_scripts
  • tests_golden/integration/test_cookiecutter_generation
    • actor/frequenz-actor-test/docs/_scripts
    • api/frequenz-api-test/docs/_scripts
    • app/frequenz-app-test/docs/_scripts
    • lib/frequenz-test-python/docs/_scripts
    • model/frequenz-model-test/docs/_scripts

7 files changed

+159
-89
lines changed

cookiecutter/{{cookiecutter.github_repo_name}}/docs/_scripts/macros.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,16 @@ def _slugify(text: str) -> str:
3333
return toc.slugify_unicode(text, "-") # type: ignore[attr-defined,no-any-return]
3434

3535

36-
def define_env(env: macros.MacrosPlugin) -> None:
37-
"""Define the hook to create macro functions for use in Markdown.
36+
def _hook_macros_plugin(env: macros.MacrosPlugin) -> None:
37+
"""Integrate the `mkdocs-macros` plugin into `mkdocstrings`.
38+
39+
This is a temporary workaround to make `mkdocs-macros` work with
40+
`mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
41+
https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
3842
3943
Args:
40-
env: The environment to define the macro functions in.
44+
env: The environment to hook the plugin into.
4145
"""
42-
# A variable to easily show an example code annotation from mkdocs-material.
43-
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
44-
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
45-
46-
# TODO(cookiecutter): Add any other macros, variables and filters here.
47-
48-
# The code below is a temporary workaround to make `mkdocs-macros` work with
49-
# `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
50-
# https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
51-
5246
# get mkdocstrings' Python handler
5347
python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python")
5448

@@ -71,3 +65,19 @@ def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any:
7165

7266
# patch the method
7367
python_handler.update_env = patched_update_env
68+
69+
70+
def define_env(env: macros.MacrosPlugin) -> None:
71+
"""Define the hook to create macro functions for use in Markdown.
72+
73+
Args:
74+
env: The environment to define the macro functions in.
75+
"""
76+
# A variable to easily show an example code annotation from mkdocs-material.
77+
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
78+
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
79+
80+
# TODO(cookiecutter): Add any other macros, variables and filters here.
81+
82+
# This hook needs to be done at the end of the `define_env` function.
83+
_hook_macros_plugin(env)

docs/_scripts/macros.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,16 @@ def _slugify(text: str) -> str:
3333
return toc.slugify_unicode(text, "-") # type: ignore[attr-defined,no-any-return]
3434

3535

36-
def define_env(env: macros.MacrosPlugin) -> None:
37-
"""Define the hook to create macro functions for use in Markdown.
36+
def _hook_macros_plugin(env: macros.MacrosPlugin) -> None:
37+
"""Integrate the `mkdocs-macros` plugin into `mkdocstrings`.
38+
39+
This is a temporary workaround to make `mkdocs-macros` work with
40+
`mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
41+
https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
3842
3943
Args:
40-
env: The environment to define the macro functions in.
44+
env: The environment to hook the plugin into.
4145
"""
42-
# A variable to easily show an example code annotation from mkdocs-material.
43-
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
44-
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
45-
46-
# The code below is a temporary workaround to make `mkdocs-macros` work with
47-
# `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
48-
# https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
49-
5046
# get mkdocstrings' Python handler
5147
python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python")
5248

@@ -69,3 +65,17 @@ def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any:
6965

7066
# patch the method
7167
python_handler.update_env = patched_update_env
68+
69+
70+
def define_env(env: macros.MacrosPlugin) -> None:
71+
"""Define the hook to create macro functions for use in Markdown.
72+
73+
Args:
74+
env: The environment to define the macro functions in.
75+
"""
76+
# A variable to easily show an example code annotation from mkdocs-material.
77+
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
78+
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
79+
80+
# This hook needs to be done at the end of the `define_env` function.
81+
_hook_macros_plugin(env)

tests_golden/integration/test_cookiecutter_generation/actor/frequenz-actor-test/docs/_scripts/macros.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,16 @@ def _slugify(text: str) -> str:
3333
return toc.slugify_unicode(text, "-") # type: ignore[attr-defined,no-any-return]
3434

3535

36-
def define_env(env: macros.MacrosPlugin) -> None:
37-
"""Define the hook to create macro functions for use in Markdown.
36+
def _hook_macros_plugin(env: macros.MacrosPlugin) -> None:
37+
"""Integrate the `mkdocs-macros` plugin into `mkdocstrings`.
38+
39+
This is a temporary workaround to make `mkdocs-macros` work with
40+
`mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
41+
https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
3842
3943
Args:
40-
env: The environment to define the macro functions in.
44+
env: The environment to hook the plugin into.
4145
"""
42-
# A variable to easily show an example code annotation from mkdocs-material.
43-
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
44-
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
45-
46-
# TODO(cookiecutter): Add any other macros, variables and filters here.
47-
48-
# The code below is a temporary workaround to make `mkdocs-macros` work with
49-
# `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
50-
# https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
51-
5246
# get mkdocstrings' Python handler
5347
python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python")
5448

@@ -71,3 +65,19 @@ def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any:
7165

7266
# patch the method
7367
python_handler.update_env = patched_update_env
68+
69+
70+
def define_env(env: macros.MacrosPlugin) -> None:
71+
"""Define the hook to create macro functions for use in Markdown.
72+
73+
Args:
74+
env: The environment to define the macro functions in.
75+
"""
76+
# A variable to easily show an example code annotation from mkdocs-material.
77+
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
78+
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
79+
80+
# TODO(cookiecutter): Add any other macros, variables and filters here.
81+
82+
# This hook needs to be done at the end of the `define_env` function.
83+
_hook_macros_plugin(env)

tests_golden/integration/test_cookiecutter_generation/api/frequenz-api-test/docs/_scripts/macros.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,16 @@ def _slugify(text: str) -> str:
3333
return toc.slugify_unicode(text, "-") # type: ignore[attr-defined,no-any-return]
3434

3535

36-
def define_env(env: macros.MacrosPlugin) -> None:
37-
"""Define the hook to create macro functions for use in Markdown.
36+
def _hook_macros_plugin(env: macros.MacrosPlugin) -> None:
37+
"""Integrate the `mkdocs-macros` plugin into `mkdocstrings`.
38+
39+
This is a temporary workaround to make `mkdocs-macros` work with
40+
`mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
41+
https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
3842
3943
Args:
40-
env: The environment to define the macro functions in.
44+
env: The environment to hook the plugin into.
4145
"""
42-
# A variable to easily show an example code annotation from mkdocs-material.
43-
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
44-
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
45-
46-
# TODO(cookiecutter): Add any other macros, variables and filters here.
47-
48-
# The code below is a temporary workaround to make `mkdocs-macros` work with
49-
# `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
50-
# https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
51-
5246
# get mkdocstrings' Python handler
5347
python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python")
5448

@@ -71,3 +65,19 @@ def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any:
7165

7266
# patch the method
7367
python_handler.update_env = patched_update_env
68+
69+
70+
def define_env(env: macros.MacrosPlugin) -> None:
71+
"""Define the hook to create macro functions for use in Markdown.
72+
73+
Args:
74+
env: The environment to define the macro functions in.
75+
"""
76+
# A variable to easily show an example code annotation from mkdocs-material.
77+
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
78+
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
79+
80+
# TODO(cookiecutter): Add any other macros, variables and filters here.
81+
82+
# This hook needs to be done at the end of the `define_env` function.
83+
_hook_macros_plugin(env)

tests_golden/integration/test_cookiecutter_generation/app/frequenz-app-test/docs/_scripts/macros.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,16 @@ def _slugify(text: str) -> str:
3333
return toc.slugify_unicode(text, "-") # type: ignore[attr-defined,no-any-return]
3434

3535

36-
def define_env(env: macros.MacrosPlugin) -> None:
37-
"""Define the hook to create macro functions for use in Markdown.
36+
def _hook_macros_plugin(env: macros.MacrosPlugin) -> None:
37+
"""Integrate the `mkdocs-macros` plugin into `mkdocstrings`.
38+
39+
This is a temporary workaround to make `mkdocs-macros` work with
40+
`mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
41+
https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
3842
3943
Args:
40-
env: The environment to define the macro functions in.
44+
env: The environment to hook the plugin into.
4145
"""
42-
# A variable to easily show an example code annotation from mkdocs-material.
43-
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
44-
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
45-
46-
# TODO(cookiecutter): Add any other macros, variables and filters here.
47-
48-
# The code below is a temporary workaround to make `mkdocs-macros` work with
49-
# `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
50-
# https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
51-
5246
# get mkdocstrings' Python handler
5347
python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python")
5448

@@ -71,3 +65,19 @@ def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any:
7165

7266
# patch the method
7367
python_handler.update_env = patched_update_env
68+
69+
70+
def define_env(env: macros.MacrosPlugin) -> None:
71+
"""Define the hook to create macro functions for use in Markdown.
72+
73+
Args:
74+
env: The environment to define the macro functions in.
75+
"""
76+
# A variable to easily show an example code annotation from mkdocs-material.
77+
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
78+
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
79+
80+
# TODO(cookiecutter): Add any other macros, variables and filters here.
81+
82+
# This hook needs to be done at the end of the `define_env` function.
83+
_hook_macros_plugin(env)

tests_golden/integration/test_cookiecutter_generation/lib/frequenz-test-python/docs/_scripts/macros.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,16 @@ def _slugify(text: str) -> str:
3333
return toc.slugify_unicode(text, "-") # type: ignore[attr-defined,no-any-return]
3434

3535

36-
def define_env(env: macros.MacrosPlugin) -> None:
37-
"""Define the hook to create macro functions for use in Markdown.
36+
def _hook_macros_plugin(env: macros.MacrosPlugin) -> None:
37+
"""Integrate the `mkdocs-macros` plugin into `mkdocstrings`.
38+
39+
This is a temporary workaround to make `mkdocs-macros` work with
40+
`mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
41+
https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
3842
3943
Args:
40-
env: The environment to define the macro functions in.
44+
env: The environment to hook the plugin into.
4145
"""
42-
# A variable to easily show an example code annotation from mkdocs-material.
43-
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
44-
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
45-
46-
# TODO(cookiecutter): Add any other macros, variables and filters here.
47-
48-
# The code below is a temporary workaround to make `mkdocs-macros` work with
49-
# `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
50-
# https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
51-
5246
# get mkdocstrings' Python handler
5347
python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python")
5448

@@ -71,3 +65,19 @@ def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any:
7165

7266
# patch the method
7367
python_handler.update_env = patched_update_env
68+
69+
70+
def define_env(env: macros.MacrosPlugin) -> None:
71+
"""Define the hook to create macro functions for use in Markdown.
72+
73+
Args:
74+
env: The environment to define the macro functions in.
75+
"""
76+
# A variable to easily show an example code annotation from mkdocs-material.
77+
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
78+
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
79+
80+
# TODO(cookiecutter): Add any other macros, variables and filters here.
81+
82+
# This hook needs to be done at the end of the `define_env` function.
83+
_hook_macros_plugin(env)

tests_golden/integration/test_cookiecutter_generation/model/frequenz-model-test/docs/_scripts/macros.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,16 @@ def _slugify(text: str) -> str:
3333
return toc.slugify_unicode(text, "-") # type: ignore[attr-defined,no-any-return]
3434

3535

36-
def define_env(env: macros.MacrosPlugin) -> None:
37-
"""Define the hook to create macro functions for use in Markdown.
36+
def _hook_macros_plugin(env: macros.MacrosPlugin) -> None:
37+
"""Integrate the `mkdocs-macros` plugin into `mkdocstrings`.
38+
39+
This is a temporary workaround to make `mkdocs-macros` work with
40+
`mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
41+
https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
3842
3943
Args:
40-
env: The environment to define the macro functions in.
44+
env: The environment to hook the plugin into.
4145
"""
42-
# A variable to easily show an example code annotation from mkdocs-material.
43-
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
44-
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
45-
46-
# TODO(cookiecutter): Add any other macros, variables and filters here.
47-
48-
# The code below is a temporary workaround to make `mkdocs-macros` work with
49-
# `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See
50-
# https://github.com/mkdocstrings/mkdocstrings/issues/615 for details.
51-
5246
# get mkdocstrings' Python handler
5347
python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python")
5448

@@ -71,3 +65,19 @@ def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any:
7165

7266
# patch the method
7367
python_handler.update_env = patched_update_env
68+
69+
70+
def define_env(env: macros.MacrosPlugin) -> None:
71+
"""Define the hook to create macro functions for use in Markdown.
72+
73+
Args:
74+
env: The environment to define the macro functions in.
75+
"""
76+
# A variable to easily show an example code annotation from mkdocs-material.
77+
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
78+
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
79+
80+
# TODO(cookiecutter): Add any other macros, variables and filters here.
81+
82+
# This hook needs to be done at the end of the `define_env` function.
83+
_hook_macros_plugin(env)

0 commit comments

Comments
 (0)