MkText

github Show source on GitHub

Class for any Markup text.

Description

All classes inheriting from MkNode can get converted to this Type.

Example: Regular

Jinja

{{ "Some text" | MkText }}

Python

MkText('Some text')

Some text

Some text
<p>Some text</p>

class MkText(TemplateRenderMixin, mknode.MkNode)

Name

Children

Inherits

MkInclude
mknodes.templatenodes.mkinclude
Node to include the text of other Markdown files / MkNodes.

    MkChangelog
    mknodes.templatenodes.mkchangelog
    Node for a git-based changelog (created by git-changelog).

      MkLicense
      mknodes.templatenodes.mklicense
      Node to show a license.

        MkLlm
        mknodes.templatenodes.mkllm
        Node for LLM-based text generation.

          Name

          Children

          Inherits

          TemplateRenderMixin
          mknodes.jinja.rendercache
          Renders the node’s template once and serves both children and content from it.

          MkNode
          mknodes.basenodes.mknode
          Base class for everything which can be expressed as Markup.

          graph TD
            1315922923["mktext.MkText"]
            3160803176["rendercache.TemplateRenderMixin"]
            2751680898["builtins.object"]
            1359323199["mknode.MkNode"]
            3160803176 --> 1315922923
            2751680898 --> 3160803176
            1359323199 --> 1315922923
            2751680898 --> 1359323199
          
          /home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mktext/metadata.toml
          [metadata]
          icon = "mdi:text"
          name = "MkText"
          group = "base"
          
          [examples.regular]
          title = "Regular"
          jinja = """
          {{ "Some text" | MkText }}
          """
          
          # [examples.from_url]
          # title = "From URL"
          # jinja = """
          # {{ mk.MkText.from_url("https://raw.githubusercontent.com/phil65/mknodes/main/README.md") }}
          # """
          
          [output.markdown]
          template = """
          {{ node.get_text() }}
          """
          
          mknodes.basenodes.mktext.MkText
          13class MkText(TemplateRenderMixin, mknode.MkNode):
          14    """Class for any Markup text.
          15
          16    All classes inheriting from MkNode can get converted to this Type.
          17    """
          18
          19    ICON = "material/text"
          20
          21    def __init__(
          22        self,
          23        text: str | mknode.MkNode | None = "",
          24        *,
          25        render_jinja: bool = False,
          26        **kwargs: Any,
          27    ) -> None:
          28        """Constructor.
          29
          30        Args:
          31            text: Markup text
          32            render_jinja: Whether text should get rendered by this node
          33            kwargs: Keyword arguments passed to parent
          34        """
          35        super().__init__(**kwargs)
          36        self._text = str(text or "")
          37        self.render_jinja = render_jinja
          38
          39    def get_section(self, section_name: str) -> Self | None:
          40        markdown = self.get_text()
          41        section_text = mdfilters.extract_header_section(markdown, section_name)
          42        return None if section_text is None else type(self)(section_text)
          43
          44    def render_kwargs(self) -> dict[str, Any]:
          45        return dict(string=self._text, variables=self.variables)
          46
          47    def get_text(self) -> str:
          48        if not self.render_jinja:
          49            return self._text
          50        return (self.current_render_result()).markdown
          51
          52    def set_text(self, value: str) -> None:
          53        self._text = value
          54
          55    def to_md_unprocessed(self) -> str:
          56        return self.get_text()
          57
          58    def get_children(self) -> list[mknode.MkNode]:
          59        """Return children nodes.
          60
          61        For MkText with render_jinja=True, this triggers Jinja rendering
          62        and returns any nodes created during rendering.
          63        """
          64        if not self.render_jinja:
          65            return []
          66        return self.get_render_result().children
          67
          68    @classmethod
          69    def from_url(cls, url: str) -> Self | None:
          70        """Build a MkText node on a remote markup file.
          71
          72        If the URL contains a "#" (http://.../markdown.md#section),
          73        it will try to extract the given section.
          74
          75        All fsspec protocols are supported as URL.
          76
          77        Args:
          78            url: URL to get markdown from.
          79        """
          80        url, *section = url.split("#")
          81        text = pathhelpers.load_file_cached(url)
          82        if not section:
          83            return cls(text)
          84
          85        extracted = mdfilters.extract_header_section(text, section[0])
          86        return cls(extracted) if extracted is not None else None
          
          Page info
          Code for this page
          mknodes.manual.nodes_section.create_section_for_nodes
           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: material/text
          title: MkText