MkTemplate¶
Node representing a jinja template.¶
Description
Renders templates with the context-aware MkNodes jinja environment. Rendered nodes become virtual children of this node. Additional variables can be passed to the render process.
Example: From project
Jinja
{{ "why_mknodes.jinja" | MkTemplate }}
Python
MkTemplate('why_mknodes.jinja', variables={})
Welcome to the MkNodes universe!
Features
Move parts of the documentation generation process into the python space. You can easily switch between programmatically generating documentation and hand-writing documentation.
Easy access to more “advanced” theme customization options. The process of generating HTML page templates in MkDocs can be a bit cumbersome. Using MkNodes, you dont need to mess with HTML, and you dont have to manage a separate folder structure for the templates. Modifying
MkPage.templatewill trigger an automated template generation process which also covers template inheritance, meaning you can also define templates for a complete tree section.Write your site navigation using idiomatic python. As known from frameworks like flask, you can create the website tree hierarchy using a decorator-based routing approach. Moving the navigation part into the python space allows to easily create documentation parts programatically. Also, visual aspects like page icons / subtitles can be defined in the site navigation instead of in the page header metadata.
A large library of python classes representing different kinds of Markdown fragments (the MkNodes). These nodes range from basic elements like lists or tables up to nodes which will generate large documentation chunks for you automatically. A complete API documentation can get created with a handful lines of code.
A dead simple API. Working with MkNodes basically means: Create nodes, attach them to the tree and forget about them. For most nodes, you only need to learn about the
__init__signature.A fully-loaded jinja2 environment, with all information about your project available in the environment namespace. Also, all the MkNodes (approx 70 different nodes right now) are available as Jinja2 filters.
Separate content from layout. The nodes are context-aware, meaning that they show information of the project they are used in. You can write your own website template and use it for multiple projects.
Create complex, composable and re-usable “documentation widgets” yourself. It’s easy to write your own MkNodes. If desired then these can be composed of other MkNodes, making them small sub-trees of your site tree. It is even possible to create nodes which represent a whole website.
Automatic resource management: The MkNodes know about the resources they require, like CSS, Javascript or markdown extensions. When the website gets assembled, this information can be used to automatically add all the required resources to the right place, like to the MkDocs config or to the HTML head by automatically creating HTML page templates.
# Welcome to the **MkNodes** universe!
Features
* Move parts of the documentation generation process into the python space. You can easily switch between programmatically generating documentation and hand-writing documentation.
* Easy access to more "advanced" theme customization options.
The process of generating HTML page templates in MkDocs can be a bit cumbersome. Using MkNodes, you dont need to mess with HTML, and you dont have to manage a separate folder structure for the templates. Modifying `MkPage.template` will trigger an automated template generation process which also covers template inheritance, meaning you can also define templates for a complete tree section.
* Write your site navigation using idiomatic python. As known from frameworks like flask, you can create the website tree hierarchy using a decorator-based routing approach. Moving the navigation part into the python space allows to easily create documentation parts programatically.
Also, visual aspects like page icons / subtitles can be defined in the site navigation instead of in the page header metadata.
* A large library of python classes representing different kinds of Markdown fragments (the **MkNodes**). These nodes range from basic elements like lists or tables up to nodes which will generate large documentation chunks for you automatically. A complete API documentation can get created with a handful lines of code.
* A dead simple API. Working with **MkNodes** basically means: Create nodes, attach them to the tree and forget about them. For most nodes, you only need to learn about the `__init__`signature.
* A fully-loaded **jinja2** environment, with all information about your project available in the environment namespace. Also, all the **MkNodes** (approx 70 different nodes right now) are available as **Jinja2** filters.
* Separate content from layout. The nodes are context-aware, meaning that they show information of the project they are used in. You can write your own website template and use it for multiple projects.
* Create complex, composable and re-usable "documentation widgets" yourself. It's easy to write your own **MkNodes**. If desired then these can be composed of other **MkNodes**, making them small sub-trees of your site tree. It is even possible to create nodes which represent a whole website.
* Automatic resource management: The MkNodes know about the resources they require, like CSS, Javascript or markdown extensions. When the website gets assembled, this information can be used to automatically add all the required resources to the right place, like to the MkDocs config or to the HTML head by automatically creating HTML page templates.
<h1>Welcome to the <strong>MkNodes</strong> universe!</h1>
<p>Features</p>
<ul>
<li>
<p>Move parts of the documentation generation process into the python space. You can easily switch between programmatically generating documentation and hand-writing documentation.</p>
</li>
<li>
<p>Easy access to more "advanced" theme customization options.
The process of generating HTML page templates in MkDocs can be a bit cumbersome. Using MkNodes, you dont need to mess with HTML, and you dont have to manage a separate folder structure for the templates. Modifying <code>MkPage.template</code> will trigger an automated template generation process which also covers template inheritance, meaning you can also define templates for a complete tree section.</p>
</li>
<li>
<p>Write your site navigation using idiomatic python. As known from frameworks like flask, you can create the website tree hierarchy using a decorator-based routing approach. Moving the navigation part into the python space allows to easily create documentation parts programatically.
Also, visual aspects like page icons / subtitles can be defined in the site navigation instead of in the page header metadata.</p>
</li>
<li>
<p>A large library of python classes representing different kinds of Markdown fragments (the <strong>MkNodes</strong>). These nodes range from basic elements like lists or tables up to nodes which will generate large documentation chunks for you automatically. A complete API documentation can get created with a handful lines of code.</p>
</li>
<li>
<p>A dead simple API. Working with <strong>MkNodes</strong> basically means: Create nodes, attach them to the tree and forget about them. For most nodes, you only need to learn about the <code>__init__</code>signature.</p>
</li>
<li>
<p>A fully-loaded <strong>jinja2</strong> environment, with all information about your project available in the environment namespace. Also, all the <strong>MkNodes</strong> (approx 70 different nodes right now) are available as <strong>Jinja2</strong> filters.</p>
</li>
<li>
<p>Separate content from layout. The nodes are context-aware, meaning that they show information of the project they are used in. You can write your own website template and use it for multiple projects.</p>
</li>
<li>
<p>Create complex, composable and re-usable "documentation widgets" yourself. It's easy to write your own <strong>MkNodes</strong>. If desired then these can be composed of other <strong>MkNodes</strong>, making them small sub-trees of your site tree. It is even possible to create nodes which represent a whole website.</p>
</li>
<li>
<p>Automatic resource management: The MkNodes know about the resources they require, like CSS, Javascript or markdown extensions. When the website gets assembled, this information can be used to automatically add all the required resources to the right place, like to the MkDocs config or to the HTML head by automatically creating HTML page templates.</p>
</li>
</ul>
Name |
Children |
Inherits |
|---|---|---|
MkCliDoc |
||
MkMaterialBadge |
||
MkImageCompare |
||
MkImageSlideshow |
||
MkIFrame |
||
MkCard |
||
MkInstallGuide |
||
MkCodeOfConduct |
||
MkCommitConventions |
||
MkDevEnvSetup |
||
MkDevTools |
||
MkReprRawRendered |
||
MkCommandOutput |
||
MkCommentedCode |
||
MkConfigSetting |
Name |
Children |
Inherits |
|---|---|---|
TemplateRenderMixin |
||
MkContainer |
graph TD
1289759389["mktemplate.MkTemplate"]
3160803176["rendercache.TemplateRenderMixin"]
2751680898["builtins.object"]
835538629["mkcontainer.MkContainer"]
1956333208["mkcontainer.MkContainerBase"]
1359323199["mknode.MkNode"]
3160803176 --> 1289759389
2751680898 --> 3160803176
835538629 --> 1289759389
1956333208 --> 835538629
1359323199 --> 1956333208
2751680898 --> 1359323199
[metadata]
icon = "simple-icons:jinja"
name = "MkTemplate"
virtual_children = true
[examples.regular]
title = "From project"
jinja = """
{{ "why_mknodes.jinja" | MkTemplate }}
"""
12class MkTemplate(TemplateRenderMixin, mkcontainer.MkContainer):
13 """Node representing a jinja template.
14
15 Renders templates with the context-aware MkNodes jinja environment.
16 Rendered nodes become virtual children of this node.
17 Additional variables can be passed to the render process.
18 """
19
20 ICON = "simple/jinja"
21 STATUS = "new"
22 VIRTUAL_CHILDREN = True
23
24 def __init__(
25 self,
26 template: str,
27 *,
28 block: str | None = None,
29 variables: dict[str, Any] | None = None,
30 **kwargs: Any,
31 ) -> None:
32 """Constructor.
33
34 Args:
35 template: Jinja template name.
36 block: Name of a specific block of the template which should get rendered
37 variables: Variables to use for rendering
38 kwargs: Keyword arguments passed to parent
39 """
40 super().__init__(**kwargs)
41 self.template = template
42 self.block = block
43 self.variables = variables or {}
44
45 def render_kwargs(self) -> dict[str, Any]:
46 return dict(template_name=self.template, variables=self.variables, block_name=self.block)
47
48 def get_items(self):
49 return self.get_render_result().children
50
51 def get_content(self) -> resources.NodeContent:
52 """Render the template once: Markdown + resources of the created nodes."""
53 result = self.current_render_result()
54 # Strip YAML frontmatter if present
55 _, md = metadata.Metadata.parse(result.markdown)
56 aggregated = self._build_node_resources()
57 for child in result.children:
58 aggregated.merge(result.get_resources(child))
59 return resources.NodeContent(markdown=md, resources=aggregated)
60
61 def to_md_unprocessed(self) -> str:
62 content = self.get_content()
63 return content.markdown
Page info
Code for this page
93def create_section_for_nodes(
94 nav: mk.MkNav,
95 klasses: Sequence[type[mk.MkNode]],
96) -> mk.MkTable:
97 table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
98 for kls in klasses:
99 tpl = "classpage_custom.jinja"
100 page = mk.MkClassPage(kls, icon=kls.ICON, template_path=tpl, parent=nav, hide="toc")
101 nav += page
102 if kls.STATUS:
103 page.metadata.status = kls.STATUS
104 elif kls.JS_FILES:
105 page.metadata.status = "js"
106 elif kls.CSS:
107 page.metadata.status = "css"
108 page.created_by = create_section_for_nodes
109 link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
110 extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
111 table.add_row((link, kls.__doc__, extensions))
112 return table
Resources
Resources(css=[],
markdown_extensions={'admonition': {},
'pymdownx.details': {},
'pymdownx.highlight': {'anchor_linenums': True,
'line_spans': '__span',
'pygments_lang_class': True},
'pymdownx.superfences': {'custom_fences': [{'class': 'mermaid',
'format': <function fence_code_format at 0x7ff77e88b060>,
'name': 'mermaid'}]},
'pymdownx.tabbed': {},
'tables': {}},
plugins=[Plugin('mkdocstrings')],
js=[],
assets=[],
packages=[])
Metadata
created:
source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/nodes_section.py
source_function: create_section_for_nodes
source_line_no: 93
hide:
- toc
icon: simple/jinja
status: new
title: MkTemplate