-
Notifications
You must be signed in to change notification settings - Fork 8
Move mkdocstrings-macros integration to a library #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
41edfeb
Add a new module for using numbered code annotations
llucax 7268922
Add a module to hook mkdocstrings and macros
llucax 10a10bd
Add a `slugify` function and filter
llucax b92cf61
Add and utility function and variables for getting version info
llucax 2380404
Add a function to get the default behaviour
llucax 30cc92a
Update documentation with a simplified setup
llucax bc3b64e
Fetch the `repo_url` from the `mkdocs.yaml`
llucax 831faf0
Add support for SHAs in `version_requirement`
llucax 6fde950
Add `version_requirement` with the sha if we can get it from Git
llucax af7ad87
Add `RepoVersionInfo.__repr__()`
llucax 147869a
Use the new macros *pluglet*
llucax bd5a72c
Bump template dependencies
llucax dbe36d5
Remove macros script from templates
llucax 1019f6a
Bump the repo-config version in templates in preparation for 0.12
llucax 0c3bc44
Add mgration script
llucax d01b08e
Update release notes
llucax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # License: MIT | ||
| # Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Integration between `mkdocstrings` and `macros` extensions to work together. | ||
|
|
||
| # Introduction | ||
|
|
||
| To be able to use macros inside docstrings, we need to integrate the | ||
| `mkdocs-macros` extension into `mkdocstrings`. This module provides the | ||
| necessary functions to make this integration work. | ||
|
|
||
| To use this module, add the following configuration to your `mkdocs.yml`: | ||
|
|
||
| ```yaml title="mkdocs.yml" | ||
| plugins: | ||
| # Order is important! mkdocstrings must come before macros! | ||
| - mkdocstrings: | ||
| default_handler: python | ||
| # ... | ||
| - macros: | ||
| module_name: path/to/macros | ||
| on_undefined: strict | ||
| on_error_fail: true | ||
| ``` | ||
|
|
||
| Then you need to add the `path/to/macros.py` file. The contents will vary depending on | ||
| if you want to use use the convenience default hooking or not. | ||
|
|
||
| # Basic usage | ||
|
|
||
| A convenience [`define_env()`] function is provided to provide a [macros | ||
| extension](https://mkdocs-macros-plugin.readthedocs.io/en/latest/macros/) to do the | ||
| hooking and provide some useful variables and filters: | ||
|
|
||
| * [`code_annotation_marker`]: A variable to inject the appropriate HTML code for showing | ||
| an example code annotation as a number (see | ||
| [frequenz.repo.config.mkdocs.annotations][] for more information). | ||
|
|
||
| If you are happy with the defaults, your `path/to/macros.py` can look as simple as: | ||
|
|
||
| ```py title="path/to/macros.py" | ||
| from frequenz.repo.config.mkdocs.mkdocstrings_macros import define_env | ||
| ``` | ||
|
|
||
| # Advanced usage | ||
|
|
||
| If you want to define your own macros, variables or filters, you'll need to provide your | ||
| own `define_env()` function, and call the hook to integrate with `mkdocstrings` at the | ||
| end. | ||
|
|
||
| Here is an example of how to do it: | ||
|
|
||
| ```py title="path/to/macros.py" | ||
| from frequenz.repo.config.mkdocs.mkdocstrings_macros import hook_macros_plugin | ||
|
|
||
| def define_env(env: macros.MacrosPlugin) -> None: | ||
| env.variables.my_var = "Example" | ||
|
|
||
| # This hook needs to be done at the end of the `define_env` function. | ||
| hook_macros_plugin(env) | ||
| ``` | ||
| """ | ||
|
|
||
|
|
||
| from typing import Any | ||
|
|
||
| import markdown as md | ||
| from mkdocs_macros import plugin as macros | ||
|
|
||
| from .annotations import CODE_ANNOTATION_MARKER | ||
|
|
||
|
|
||
| def hook_macros_plugin(env: macros.MacrosPlugin) -> None: | ||
| """Integrate the `mkdocs-macros` plugin into `mkdocstrings`. | ||
|
|
||
| This is a temporary workaround to make `mkdocs-macros` work with | ||
| `mkdocstrings` until a proper `mkdocs-macros` *pluglet* is available. See | ||
| https://github.com/mkdocstrings/mkdocstrings/issues/615 for details. | ||
|
|
||
| Args: | ||
| env: The environment to hook the plugin into. | ||
| """ | ||
| # get mkdocstrings' Python handler | ||
| python_handler = env.conf["plugins"]["mkdocstrings"].get_handler("python") | ||
|
|
||
| # get the `update_env` method of the Python handler | ||
| update_env = python_handler.update_env | ||
|
|
||
| # override the `update_env` method of the Python handler | ||
| def patched_update_env(md: md.Markdown, config: dict[str, Any]) -> None: | ||
| update_env(md, config) | ||
|
|
||
| # get the `convert_markdown` filter of the env | ||
| convert_markdown = python_handler.env.filters["convert_markdown"] | ||
|
|
||
| # build a chimera made of macros+mkdocstrings | ||
| def render_convert(markdown: str, *args: Any, **kwargs: Any) -> Any: | ||
| return convert_markdown(env.render(markdown), *args, **kwargs) | ||
|
|
||
| # patch the filter | ||
| python_handler.env.filters["convert_markdown"] = render_convert | ||
|
|
||
| # patch the method | ||
| python_handler.update_env = patched_update_env | ||
|
|
||
|
|
||
| def define_env(env: macros.MacrosPlugin) -> None: | ||
| """Define the hook to create macro functions for use in Markdown. | ||
|
|
||
| Args: | ||
| env: The environment to define the macro functions in. | ||
| """ | ||
| env.variables.code_annotation_marker = CODE_ANNOTATION_MARKER | ||
|
|
||
| # This hook needs to be done at the end of the `define_env` function. | ||
| hook_macros_plugin(env) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That text is not present in the latest code, I wouldn't go through the trouble of fixing up the commit, but I can do it if you think it is important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I wasn't aware it wasn't the latest code, it doesn't have to be fixed.