From 0647ac0b92e258a70c4bac6e2c1fe906784f97d0 Mon Sep 17 00:00:00 2001 From: Philipp Temminghoff Date: Sat, 23 Sep 2023 10:34:44 +0200 Subject: [PATCH] feat: add node.as_html attr --- mknodes/basenodes/mknode.py | 5 ++++- mknodes/templatenodes/mkreprrawrendered.py | 9 ++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/mknodes/basenodes/mknode.py b/mknodes/basenodes/mknode.py index b78690dc..bd158c83 100644 --- a/mknodes/basenodes/mknode.py +++ b/mknodes/basenodes/mknode.py @@ -56,6 +56,7 @@ def __init__( name: str | None = None, shift_header_levels: int = 0, css_classes: Iterable[str] | None = None, + as_html: bool = False, project: project.Project | None = None, parent: MkNode | None = None, ): @@ -67,6 +68,7 @@ def __init__( name: An optional unique identifier (allows getting node via MkNode.get_node) shift_header_levels: Regex-based header level shifting (adds/removes #-chars) css_classes: A sequence of css class names to use for this node + as_html: Converts node to HTML on stringifying. project: Project this Nav is connected to. parent: Parent for building the tree """ @@ -78,6 +80,7 @@ def __init__( self._css_classes: set[str] = set(css_classes or []) self._associated_project = project self._node_name = name + self.as_html = as_html if name is not None: self._name_registry[name] = self self.stats = contexts.NodeBuildStats() @@ -90,7 +93,7 @@ def __init__( self.annotations = None def __str__(self): - return self.to_markdown() + return self.to_html() if self.as_html else self.to_markdown() def __hash__(self): return hash(self.to_markdown()) diff --git a/mknodes/templatenodes/mkreprrawrendered.py b/mknodes/templatenodes/mkreprrawrendered.py index b81dc821..4792fd46 100644 --- a/mknodes/templatenodes/mkreprrawrendered.py +++ b/mknodes/templatenodes/mkreprrawrendered.py @@ -11,7 +11,7 @@ class MkReprRawRendered(mktabcontainer.MkTabbed): - """Node showing a tabbed bock to visualize a node. + """Node showing a tabbed block to visualize a node in different representations. It contains a tab for the repr, one for the rendered output, one for the markdown and a Repr tree in case the node has children. @@ -22,7 +22,7 @@ class MkReprRawRendered(mktabcontainer.MkTabbed): def __init__( self, node: mknode.MkNode, - select_tab: int | str | None = 2, + select_tab: int | str | None = 3, **kwargs: Any, ): """Constructor. @@ -44,9 +44,12 @@ def items(self): # part of the tree. Perhaps add a setting for MkPages to be only-virtual? # Needs a general concept in regards to re-parenting. (should base nodes # be allowed to have pages as children?) + html_node = self.node.__copy__() + html_node.as_html = True tabs: dict[str, str | mknode.MkNode] = dict( # type: ignore[annotation-unchecked] Repr=mkcode.MkCode(repr(self.node)), - Markdown=mkcode.MkCode(self.node, language="markdown"), + Raw=mkcode.MkCode(self.node, language="markdown"), + Html=mkcode.MkCode(html_node, language="html"), Rendered=self.node.__copy__(), ) if len(self.node.children) > 0: