Skip to content

MkAnnotations

:fa-brands-github: Show source on GitHub

Node containing a list of MkAnnotations.

Bases: MkContainer

__init__

__init__(
    annotations: Mapping[int, str | MkNode] | list[MkNode | str] | None = None,
    **kwargs: Any
) -> None

Parameters:

Name Type Description Default
annotations Mapping[int, str | MkNode] | list[MkNode | str] | None

Annotations data (Can be given in different shapes)

None
kwargs Any

Keyword arguments passed to parent

{}

get_content async

get_content() -> NodeContent

get_items

get_items() -> list[MkAnnotation]
Name Children Inherits
MkContainer
mknodes.basenodes.mkcontainer
A node containing other MkNodes.
graph TD
  3461153190["mkannotations.MkAnnotations"]
  835538629["mkcontainer.MkContainer"]
  1956333208["mkcontainer.MkContainerBase"]
  1359323199["mknode.MkNode"]
  2751680898["builtins.object"]
  835538629 --> 3461153190
  1956333208 --> 835538629
  1359323199 --> 1956333208
  2751680898 --> 1359323199
/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mkannotations/metadata.toml
[metadata]
name = "MkAnnotations"
icon = "mdi:alert-box"

[requirements.extension."pymdownx.superfences"]

[examples.python]
python = """
import mknodes as mk

page += mk.MkCode.for_object(mk.MkCode.for_object, extract_body=True)
node = MkAnnotations()
page += node
text = mk.MkText("The MkAnnotations node aggregates annotations(1).")
page += text
info = r"Annotations are numbered and can be set via \\__setitem__."
node[1] = info  # (1)
admonition = mk.MkAdmonition("They can also contain other Markdown.")
node[2] = admonition  # (2)
text.annotations[1] = "Every MkNode can annotate via the 'annotations' attribute"
page += mk.MkCode(str(node), language="markdown", header="Markdown")
"""

[fragments.annotation]
template = """
{{ node.num }}.{{ 3 - self.num | string | length }}{{ node.get_items() | join("\n\n") | indent }}
"""

[output.markdown]
template = """
{{ node.get_items() | sort(attribute="num") | join }}
"""
mknodes.basenodes.mkannotations.MkAnnotations
class MkAnnotations(mkcontainer.MkContainer):
    """Node containing a list of MkAnnotations."""

    REQUIRED_EXTENSIONS = [resources.Extension("pymdownx.superfences")]
    ICON = "material/alert-box"

    def __init__(
        self,
        annotations: Mapping[int, str | mk.MkNode] | list[mk.MkNode | str] | None = None,
        **kwargs: Any,
    ) -> None:
        """Constructor.

        Args:
            annotations: Annotations data (Can be given in different shapes)
            kwargs: Keyword arguments passed to parent
        """
        match annotations:
            case None:
                items = []
            case list():
                items = [
                    (ann if isinstance(ann, MkAnnotation) else MkAnnotation(i, ann))
                    for i, ann in enumerate(annotations, start=1)
                ]
            case Mapping():
                items = [MkAnnotation(k, content=v) for k, v in annotations.items()]
            case _:  # pyright: ignore[reportUnnecessaryComparison]
                raise TypeError(annotations)
        super().__init__(content=items, **kwargs)

    def get_items(self) -> list[MkAnnotation]:  # type: ignore[override]
        """Return the list of annotations."""
        return self._items  # type: ignore[return-value]

    def __getitem__(self, item: int) -> MkAnnotation:
        for node in self.get_items():
            if node.num == item:
                return node
        raise IndexError(item)

    def __contains__(self, item: int | MkAnnotation) -> bool:
        match item:
            case MkAnnotation():
                return item in self.get_items()
            case int():
                return any(i.num == item for i in self.get_items())
            case _:
                raise TypeError(item)

    def __repr__(self) -> str:
        notes: list[str | MkAnnotation] = []
        for item in self.get_items():
            item_children = item.get_items()
            if len(item_children) == 1:
                string = reprhelpers.to_str_if_textnode(item_children[0])
                notes.append(string)
            else:
                notes.append(item)
        return reprhelpers.get_repr(self, annotations=notes)

    def _get_item_pos(self, num: int) -> int:
        items = self.get_items()
        item = next(i for i in items if i.num == num)
        return items.index(item)

    def __setitem__(self, index: int, value: mk.MkNode | str) -> None:
        import mknodes as mk

        match value:
            case str():
                item = mk.MkText(value)
                node = MkAnnotation(index, content=item, parent=self)
            case MkAnnotation():
                node = value
            case mk.MkNode():
                node = MkAnnotation(index, content=value, parent=self)
        items = self.get_items()
        if index in self:
            pos = self._get_item_pos(index)
            items[pos] = node
        else:
            items.append(node)

    async def get_content(self) -> resources.NodeContent:
        """Single-pass: get content from sorted annotations and resources."""
        items = self.get_items()
        if not items:
            return resources.NodeContent(
                markdown="",
                resources=await self._build_node_resources(),
            )

        items = sorted(items, key=lambda x: x.num)

        # Collect content from children
        renderer = rendering.current()
        child_contents = [await renderer.render_child(item) for item in items]
        child_markdowns = [content.markdown for content in child_contents]

        md = "".join(child_markdowns)

        # Aggregate resources
        aggregated = await self._build_node_resources()
        for child_content in child_contents:
            aggregated.merge(child_content.resources)

        return resources.NodeContent(markdown=md, resources=aggregated)

    async def to_md_unprocessed(self) -> str:
        content = await self.get_content()
        return content.markdown

    def annotate_text(self, markdown: str) -> str:
        if not self.get_items():
            return markdown
        return f'<div class="annotate" markdown>\n{markdown}\n</div>\n\n{self}'
Page info
Code for this page
mknodes.manual.nodes_section.create_section_for_nodes
def create_section_for_nodes(
    nav: mk.MkNav,
    klasses: Sequence[type[mk.MkNode]],
) -> mk.MkTable:
    table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
    for kls in klasses:
        tpl = "classpage_custom.jinja"
        page = mk.MkClassPage(kls, icon=kls.ICON, template_path=tpl, parent=nav, hide="toc")
        nav += page
        if kls.STATUS:
            page.metadata.status = kls.STATUS
        elif kls.JS_FILES:
            page.metadata.status = "js"
        elif kls.CSS:
            page.metadata.status = "css"
        page.created_by = create_section_for_nodes
        link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
        extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
        table.add_row((link, kls.__doc__, extensions))
    return table
Resources
Resources(css=[],
          markdown_extensions={'pymdownx.highlight': {'anchor_linenums': True,
                                                      'line_spans': '__span',
                                                      'pygments_lang_class': True},
                               'pymdownx.superfences': {'custom_fences': [{'class': 'mermaid',
                                                                           'format': <function fence_code_format at 0x703933662e50>,
                                                                           'name': 'mermaid'}]},
                               '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: 95
hide:
- toc
icon: material/alert-box
title: MkAnnotations