Skip to content

Commit

Permalink
feat: add node.as_html attr
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Sep 23, 2023
1 parent 849d74c commit 0647ac0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion mknodes/basenodes/mknode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
Expand All @@ -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
"""
Expand All @@ -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()
Expand All @@ -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())
Expand Down
9 changes: 6 additions & 3 deletions mknodes/templatenodes/mkreprrawrendered.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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:
Expand Down

0 comments on commit 0647ac0

Please sign in to comment.