Skip to content

MkDefinitionList

Show source on GitHub

Node for definition lists.

Example: Regular

Jinja

{{ {"something": "A definition", "something_else": "Another
definition"} | MkDefinitionList }}

Python

MkDefinitionList(data={'something': 'A definition', 'something_else': 'Another\ndefinition'})
something
A definition
something_else
Another definition
something
:   A definition
something_else
:   Another
    definition
<dl>
<dt>something</dt>
<dd>
<dl>
<dt>A definition</dt>
<dt>something_else</dt>
<dd>Another
definition</dd>
</dl>
</dd>
</dl>
something
  A definition
something_else
  Another
  definition
MkDefinitionList
├── MkText('A definition')
╰── MkText('Another\ndefinition')

Example: Nested markdown

Jinja

{{ {"something": "A definition", "something_else": ("Nested" | MkAdmonition)} | MkDefinitionList }}

Python

MkContainer([...])

Info

Nested

something
A definition
something_else

Info

Nested
!!! info
    Nested


something
:   A definition
something_else
:   !!! info
        Nested
<div class="admonition info">
<p class="admonition-title">Info</p>
<p>Nested</p>
</div>
<dl>
<dt>something</dt>
<dd>
<dl>
<dt>A definition</dt>
<dt>something_else</dt>
<dd>
<div class="admonition info">
<p class="admonition-title">Info</p>
Nested</div>
</dd>
</dl>
</dd>
</dl>
MkContainer
├── MkAdmonition('Nested')
│   ╰── MkText('Nested')
╰── MkDefinitionList(data={'something': 'A definition', 'something_else': MkAdmonition('Nested')})
    ├── MkText('A definition')
├── MkAdmonition('Nested')
│   ╰── MkText('Nested')

Bases: MkContainer

__init__

__init__(data: Mapping | None = None, **kwargs: Any)

Parameters:

Name Type Description Default
data Mapping | None

Data show for the table

None
kwargs Any

Keyword arguments passed to parent

{}
Name Children Inherits
MkContainer
mknodes.basenodes.mkcontainer
A node containing other MkNodes.
graph TD
  94721311917888["mkdefinitionlist.MkDefinitionList"]
  94721311697232["mkcontainer.MkContainer"]
  94721308848336["mknode.MkNode"]
  94721311766592["node.Node"]
  140564252373184["builtins.object"]
  94721311697232 --> 94721311917888
  94721308848336 --> 94721311697232
  94721311766592 --> 94721308848336
  140564252373184 --> 94721311766592
/home/runner/work/mknodes/mknodes/mknodes/basenodes/mkdefinitionlist/metadata.toml
[metadata]
icon = "mdi:library"
name = "MkDefinitionList"

[requirements.extension.def_list]

[examples.regular]
title = "Regular"
jinja = """
{{ {"something": "A definition", "something_else": "Another\ndefinition"} | MkDefinitionList }}
"""

[examples.nested]
title = "Nested markdown"
jinja = """
{{ {"something": "A definition", "something_else": ("Nested" | MkAdmonition)} | MkDefinitionList }}
"""

[output.markdown]
template = """
{% for k, v in node.data.items() %}
{{ k }}
:   {{ v | string | indent }}
{% endfor %}
"""

[output.rst]
template = """
{% for k, v in node.data.items() %}
{{ k }}
  {{ v | string | indent(width=2) }}
{% endfor %}
"""
mknodes.basenodes.mkdefinitionlist.MkDefinitionList
class MkDefinitionList(mkcontainer.MkContainer):
    """Node for definition lists."""

    REQUIRED_EXTENSIONS = [resources.Extension("def_list")]
    ICON = "material/library"

    def __init__(self, data: Mapping | None = None, **kwargs: Any):
        """Constructor.

        Args:
            data: Data show for the table
            kwargs: Keyword arguments passed to parent
        """
        super().__init__(**kwargs)
        self.data: dict[str, str | mknode.MkNode] = {}
        self.items = data

    def __repr__(self):
        kws = {k: reprhelpers.to_str_if_textnode(v) for k, v in self.data.items()}
        return reprhelpers.get_repr(self, data=kws)

    @property
    def items(self):
        return list(self.data.values())

    @items.setter
    def items(self, data):
        match data:
            case Mapping():
                self.data = {k: self.to_child_node(v) for k, v in data.items()}
            case list():
                self.data = {
                    str(i): self.to_child_node(item) for i, item in enumerate(data)
                }
            case None:
                self.data = {}
            case _:
                raise TypeError(data)

    def _to_markdown(self) -> str:
        items = [f"{k}\n:   {filters.do_indent(str(v))}\n" for k, v in self.data.items()]
        return "".join(items)