Skip to content

Commit 1c8bfea

Browse files
committed
Add macros variables with project versions
This allows to give instructions that match the current status of the repository (a released tag, a development branch, etc.). The new macro variables are: - `version`: An object with all the repository version information. - `version_requirement`: A string with the version requirement to use the repository at the current version. Can be used to specify dependency requirements in `pyproject.toml` files. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent a65747b commit 1c8bfea

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/_scripts/macros.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
"""This module defines macros for use in Markdown files."""
55

6+
import logging
67
from typing import Any
78

89
import markdown as md
910
from markdown.extensions import toc
1011
from mkdocs_macros import plugin as macros
1112

13+
from frequenz.repo.config import github
14+
15+
_logger = logging.getLogger(__name__)
16+
1217
_CODE_ANNOTATION_MARKER: str = (
1318
r'<span class="md-annotation">'
1419
r'<span class="md-annotation__index" tabindex="-1">'
@@ -33,6 +38,29 @@ def _slugify(text: str) -> str:
3338
return toc.slugify_unicode(text, "-") # type: ignore[attr-defined,no-any-return]
3439

3540

41+
def _add_version_variables(env: macros.MacrosPlugin) -> None:
42+
"""Add variables with git information to the environment.
43+
44+
Args:
45+
env: The environment to add the variables to.
46+
"""
47+
env.variables["version"] = None
48+
env.variables["version_requirement"] = ""
49+
try:
50+
version_info = github.get_repo_version_info()
51+
except Exception as exc: # pylint: disable=broad-except
52+
_logger.warning("Failed to get version info: %s", exc)
53+
else:
54+
env.variables["version"] = version_info
55+
if version_info.current_tag:
56+
env.variables["version_requirement"] = f" == {version_info.current_tag}"
57+
elif version_info.current_branch:
58+
env.variables["version_requirement"] = (
59+
" @ git+https://github.com/frequenz-floss/frequenz-repo-config-python"
60+
f"@{version_info.current_branch}"
61+
)
62+
63+
3664
def _hook_macros_plugin(env: macros.MacrosPlugin) -> None:
3765
"""Integrate the `mkdocs-macros` plugin into `mkdocstrings`.
3866
@@ -77,5 +105,7 @@ def define_env(env: macros.MacrosPlugin) -> None:
77105
# https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-annotations
78106
env.variables["code_annotation_marker"] = _CODE_ANNOTATION_MARKER
79107

108+
_add_version_variables(env)
109+
80110
# This hook needs to be done at the end of the `define_env` function.
81111
_hook_macros_plugin(env)

0 commit comments

Comments
 (0)