MkCommentedCode
Node which displays a list of code / comment blocks for given code.¶
Description
Lines beginning with # are shown in dedicated blocks and can be used to inline-explain the code. Lines can be hidden by ending a line with "##".
Example: From text¶
Jinja
Python
Comment sections automatically get converted to non-codeblock sections.
That way you can explain your code in-line.
you can use headers.¶
Note
Admonitions and everything else work, too.
<div class="speech bottom" markdown="1">
Comment sections automatically get converted to non-codeblock sections.
That way you can explain your code in-line.
## you can use headers.
</div>
```` {.python }
some_code = 1 + 2
````
<div class="speech bottom" markdown="1">
!!! note
Admonitions and everything else work, too.
</div>
<div class="speech bottom">
<p>Comment sections automatically get converted to non-codeblock sections.
That way you can explain your code in-line.</p>
<h2 id="you-can-use-headers">you can use headers.</h2>
</div>
<div class="language-python highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">some_code</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span>
</span></code></pre></div>
<div class="speech bottom">
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Admonitions and everything else work, too.</p>
</div>
</div>
MkCommentedCode
├── MkSpeechBubble('Comment sections automatically get converted to...lain your code in-line.\n## you can use headers.')
│ ╰── MkText('Comment sections automatically get converted to non-codeblock sections.\nThat way you can explain your code in-line.\n## you can use headers.')
├── MkCode('some_code = 1 + 2')
│ ╰── MkText('some_code = 1 + 2')
╰── MkSpeechBubble('!!! note\n Admonitions and everything else work, too.\n')
╰── MkText('!!! note\n Admonitions and everything else work, too.\n')
Example: From function¶
Python
Comment sections automatically get converted to non-codeblock sections.
That way you can explain your code in-line.
you can use headers.¶
Note
Admonitions and everything else work, too.
```` {.python }
@classmethod
def create_example_page(cls, page):
````
<div class="speech bottom" markdown="1">
Comment sections automatically get converted to non-codeblock sections.
That way you can explain your code in-line.
## you can use headers.
</div>
```` {.python }
page += MkCommentedCode(MkCommentedCode.create_example_page)
````
<div class="speech bottom" markdown="1">
!!! note
Admonitions and everything else work, too.
</div>
```` {.python }
page += MkCommentedCode(MkCommentedCode.create_example_page, style="admonition")
````
<div class="language-python highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a> <span class="nd">@classmethod</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a> <span class="k">def</span> <span class="nf">create_example_page</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">page</span><span class="p">):</span>
</span></code></pre></div>
<div class="speech bottom">
<p>Comment sections automatically get converted to non-codeblock sections.
That way you can explain your code in-line.</p>
<h2 id="you-can-use-headers">you can use headers.</h2>
</div>
<div class="language-python highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a> <span class="n">page</span> <span class="o">+=</span> <span class="n">MkCommentedCode</span><span class="p">(</span><span class="n">MkCommentedCode</span><span class="o">.</span><span class="n">create_example_page</span><span class="p">)</span>
</span></code></pre></div>
<div class="speech bottom">
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Admonitions and everything else work, too.</p>
</div>
</div>
<div class="language-python highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a> <span class="n">page</span> <span class="o">+=</span> <span class="n">MkCommentedCode</span><span class="p">(</span><span class="n">MkCommentedCode</span><span class="o">.</span><span class="n">create_example_page</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="s2">"admonition"</span><span class="p">)</span>
</span></code></pre></div>
MkCommentedCode
├── MkCode(' @classmethod\n def create_example_page(cls, page):')
│ ╰── MkText(' @classmethod\n def create_example_page(cls, page):')
├── MkSpeechBubble('Comment sections automatically get converted to...lain your code in-line.\n## you can use headers.')
│ ╰── MkText('Comment sections automatically get converted to non-codeblock sections.\nThat way you can explain your code in-line.\n## you can use headers.')
├── MkCode(' page += MkCommentedCode(MkCommentedCode.create_example_page)')
│ ╰── MkText(' page += MkCommentedCode(MkCommentedCode.create_example_page)')
├── MkSpeechBubble('!!! note\n Admonitions and everything else work, too.\n')
│ ╰── MkText('!!! note\n Admonitions and everything else work, too.\n')
╰── MkCode(' page += MkCommentedCode(MkCommentedCode.create_example_page, style="admonition")')
╰── MkText(' page += MkCommentedCode(MkCommentedCode.create_example_page, style="admonition")')
Bases: MkTemplate
__init__
¶
__init__(
code: str | HasCodeType,
language: str = "py",
*,
linenums: int | None = None,
style: Literal["text", "admonition", "bubble"] = "bubble",
**kwargs: Any
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str | HasCodeType
|
Code to show |
required |
language
|
str
|
language for syntax highlighting |
'py'
|
linenums
|
int | None
|
If set, use as start linenumber |
None
|
style
|
Literal['text', 'admonition', 'bubble']
|
Comment style |
'bubble'
|
kwargs
|
Any
|
Keyword arguments passed to parent |
{}
|
Name | Children | Inherits |
---|---|---|
MkTemplate mknodes.templatenodes.mktemplate Node representing a jinja template. |
graph TD
94721312016336["mkcommentedcode.MkCommentedCode"]
94721308869584["mktemplate.MkTemplate"]
94721311697232["mkcontainer.MkContainer"]
94721308848336["mknode.MkNode"]
94721311766592["node.Node"]
140564252373184["builtins.object"]
94721308869584 --> 94721312016336
94721311697232 --> 94721308869584
94721308848336 --> 94721311697232
94721311766592 --> 94721308848336
140564252373184 --> 94721311766592
/home/runner/work/mknodes/mknodes/mknodes/templatenodes/mkcommentedcode/metadata.toml
[metadata]
icon = "mdi:code-json"
name = "MkCommentedCode"
status = "new"
group = "documentation"
virtual_children = true
[examples.from_text]
title = "From text"
jinja = """
{{
"
# Comment sections automatically get converted to non-codeblock sections.
# That way you can explain your code in-line.
# ## you can use headers.
some_code = 1 + 2
# !!! note
# Admonitions and everything else work, too.
#
" | MkCommentedCode
}}
"""
[examples.from_function]
title = "From function"
jinja = """
{{ mk.MkCommentedCode.create_example_page | MkCommentedCode }}
"""
[output.markdown]
template = """
{% for section in inspecthelpers.iter_code_sections(node.code, node.linenums) %}
{% if section.typ == "code" %}
{{ section.code | MkCode(linenums=section.start_line) }}
{% elif node.style == "text" %}
{{ section.code }}
{% elif node.style == "bubble" %}
{{ section.code | MkSpeechBubble }}
{% elif node.style == "admonition" %}
{{ section.code | MkAdmonition }}
{% endif %}
{% endfor %}
"""