Skip to content

MkContainer

Show source on GitHub

A node containing other MkNodes.

Description

This node class is often used as a base class, and can be treated like a list. Nodes added to a container are automatically re-parented.

Example: Regular

Jinja

{{ ["a = 1 + 2" | MkCode, "abc" | MkText] | MkContainer }}

Python

MkContainer([...])
a = 1 + 2

abc

a = 1 + 2

abc

```` {.python }
a = 1 + 2
````

abc

```` {.python }
a = 1 + 2
````

abc
<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">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span>
</span></code></pre></div>
<p>abc</p>
<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">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span>
</span></code></pre></div>
<p>abc</p>
MkContainer
├── MkCode('a = 1 + 2')
│   ╰── MkText('a = 1 + 2')
├── MkText('abc')
╰── MkContainer([...])
├── MkCode('a = 1 + 2')
│   ╰── MkText('a = 1 + 2')
├── MkText('abc')

Bases: MkNode

__init__

__init__(
    content: list | str | MkNode | None = None,
    *,
    block_separator: str = "\n\n",
    **kwargs: Any
)

Parameters:

Name Type Description Default
content list | str | MkNode | None

Child Nodes of this container

None
block_separator str

Separator to put between blocks. Defaults to 2 linebreaks

'\n\n'
kwargs Any

Keyword arguments passed to parent

{}

append

append(other: str | MkNode)

Parameters:

Name Type Description Default
other str | MkNode

The node / text to append

required

insert

insert(index: int, other: str | MkNode)

Parameters:

Name Type Description Default
index int

Position where node should get inserted

required
other str | MkNode

The node / text to insert

required
Name Children Inherits
MkBlock
mknodes.basenodes.mkblock
PyMdown-based block.
MkAdmonition
mknodes.basenodes.mkadmonition
Admonition info box.
    MkBlockQuote
    mknodes.basenodes.mkblockquote
    BlockQuote node.
      MkCode
      mknodes.basenodes.mkcode
      Class representing a Code block.
      MkTemplate
      mknodes.templatenodes.mktemplate
      Node representing a jinja template.
      MkList
      mknodes.basenodes.mklist
      Node for showing a formatted list.
        MkBaseTable
        mknodes.basenodes.mkbasetable
        Base Class for MkTables. Only deals with managing the data.
        MkTab
        mknodes.basenodes.mktabs
        Node representing a single tab.
          MkTabContainer
          mknodes.basenodes.mktabcontainer
          Base class for nodes containing tabs.
          MkCritic
          mknodes.basenodes.mkcritic
          MkCritic block.
            MkAnnotation
            mknodes.basenodes.mkannotations
            Represents a single annotation. It gets managed by an MkAnnotations node.
              MkAnnotations
              mknodes.basenodes.mkannotations
              Node containing a list of MkAnnotations.
                MkFootNote
                mknodes.basenodes.mkfootnotes
                Represents a single footnote. It gets managed by an MkFootNotes node.
                  MkFootNotes
                  mknodes.basenodes.mkfootnotes
                  Node containing a list of MkFootNotes.
                    MkDefinition
                    mknodes.basenodes.mkdefinitionlist
                    Node for a single definition.
                      MkDefinitionList
                      mknodes.basenodes.mkdefinitionlist
                      Node for definition lists.
                        MkPage
                        mknodes.pages.mkpage
                        A node container representing a Markdown page.
                        MkShowcase
                        mknodes.basenodes.mkshowcase
                        Node for showing a html-based image grid.
                          MkSpeechBubble
                          mknodes.basenodes.mkspeechbubble
                          Node for showing a css-based speech bubble.
                            MkTask
                            mknodes.basenodes.mktasklist
                            Node for a single task listitem.
                              MkTaskList
                              mknodes.basenodes.mktasklist
                              Node for task lists.
                                MkTimeline
                                mknodes.basenodes.mktimeline
                                Node to show a JavaScript-supported Timeline.
                                  MkShields
                                  mknodes.templatenodes.mkshields
                                  Container for Shields.io / GitHub badges.
                                    MkMetadataBadges
                                    mknodes.templatenodes.mkmetadatabadges
                                    Container node for a list of metadata badges.
                                      Name Children Inherits
                                      MkNode
                                      mknodes.basenodes.mknode
                                      Base class for everything which can be expressed as Markup.
                                      graph TD
                                        94721311697232["mkcontainer.MkContainer"]
                                        94721308848336["mknode.MkNode"]
                                        94721311766592["node.Node"]
                                        140564252373184["builtins.object"]
                                        94721308848336 --> 94721311697232
                                        94721311766592 --> 94721308848336
                                        140564252373184 --> 94721311766592
                                      /home/runner/work/mknodes/mknodes/mknodes/basenodes/mkcontainer/metadata.toml
                                      [metadata]
                                      icon = "mdi:database"
                                      name = "MkContainer"
                                      
                                      [examples.regular]
                                      title = "Regular"
                                      jinja = """
                                      {{ ["a = 1 + 2" | MkCode, "abc" | MkText] | MkContainer }}
                                      """
                                      
                                      mknodes.basenodes.mkcontainer.MkContainer
                                      class MkContainer(mknode.MkNode):
                                          """A node containing other MkNodes.
                                      
                                          This node class is often used as a base class, and can be treated like a list.
                                          Nodes added to a container are automatically re-parented.
                                          """
                                      
                                          ICON = "material/database"
                                      
                                          def __init__(
                                              self,
                                              content: list | str | mknode.MkNode | None = None,
                                              *,
                                              block_separator: str = "\n\n",
                                              **kwargs: Any,
                                          ):
                                              """Constructor.
                                      
                                              Args:
                                                  content: Child Nodes of this container
                                                  block_separator: Separator to put between blocks. Defaults to 2 linebreaks
                                                  kwargs: Keyword arguments passed to parent
                                              """
                                              super().__init__(**kwargs)
                                              self.block_separator = block_separator
                                              match content:
                                                  case None:
                                                      self.items: list[mknode.MkNode] = []
                                                  case str():
                                                      self.items = [self.to_child_node(content)] if content else []
                                                  case mknode.MkNode():
                                                      self.items = [self.to_child_node(content)]
                                                  case list():
                                                      self.items = [self.to_child_node(i) for i in content]
                                                  case _:
                                                      raise TypeError(content)
                                      
                                          def __bool__(self):
                                              return bool(self.items)
                                      
                                          def __add__(self, other: str | mknode.MkNode):
                                              self.append(other)
                                              return self
                                      
                                          def __iter__(self) -> Iterator[mknode.MkNode]:  # type: ignore
                                              return iter(self.items)
                                      
                                          def _to_markdown(self) -> str:
                                              return self.block_separator.join(i.to_markdown() for i in self.items)
                                      
                                          def append(self, other: str | mknode.MkNode):
                                              """Append a MkNode to the end of given MkPage.
                                      
                                              Args:
                                                  other: The node / text to append
                                              """
                                              node = self.to_child_node(other)
                                              self.items.append(node)  # type: ignore[arg-type]
                                      
                                          def insert(self, index: int, other: str | mknode.MkNode):
                                              """Insert a MkNode into desired position of given MkPage.
                                      
                                              Args:
                                                  index: Position where node should get inserted
                                                  other: The node / text to insert
                                              """
                                              node = self.to_child_node(other)
                                              self.items.insert(index, node)
                                      
                                          @property  # type: ignore
                                          def children(self) -> list[mknode.MkNode]:
                                              return self.items
                                      
                                          @children.setter
                                          def children(self, children: list[mknode.MkNode]):
                                              self.items = children