feat: Allow extensions to add templates#569
Merged
Conversation
0067246 to
414065a
Compare
An extension here is simply a Python package
that defines an entry-point for a specific handler.
For example, an extension can add templates to the Python handler
thanks to this entry-point:
```toml
[project.entry-points."mkdocstrings.python.templates"]
extension-name = "extension_package:get_templates_path"
```
This entry-point assumes that the extension provides
a `get_templates_path` function directly under
the `extension_package` package. This function doesn't
accept any argument and returns the path to a directory
containing templates. The directory must contain one
subfolder for each supported theme, for example:
```
templates/
material/
readthedocs/
mkdocs/
```
mkdocstrings will add the folders corresponding
to the user-selected theme, and to the handler's defined
fallback theme, as usual, to the Jinja loader.
The names of the extension templates must not
overlap with the handler's original templates.
The extension is then responsible, in collaboration
with its target handler, for mutating the collected
data in order to instruct the handler to use one of
the extension template when rendering particular objects.
For example, the Python handler will look for a `template`
attribute on objects, and use it to render the object.
This `template` attribute will be set by Griffe extensions
(Griffe is the tool used by the Python handler to collect data).
Member
Author
|
@oprypin I've added docs and tests, will merge in one or two days 🙂 |
Member
|
Thanks. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
An extension here is simply a Python package that defines an entry-point for a specific handler.
For example, an extension can add templates to the Python handler thanks to this entry-point:
This entry-point assumes that the extension provides a
get_templates_pathfunction directly under theextension_packagepackage. This function doesn't accept any argument and returns the path to a directory containing templates. The directory must contain one subfolder for each supported theme, for example:mkdocstrings will add the folders corresponding to the user-selected theme, and to the handler's defined fallback theme, as usual.
The names of the extension templates must not overlap with the handler's original templates.
The extension is then responsible, in collaboration with its target handler, for mutating the collected data in order to instruct the handler to use one of the extension template when rendering particular objects.
For example, the Python handler will look for a
templateattribute on objects, and use it to render the object. Thistemplateattribute will be set by Griffe extensions (Griffe is the tool used by the Python handler to collect data).See related PR: mkdocstrings/python#70
TODO