Skip to content

Commit

Permalink
chore: more code->jinja conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Nov 20, 2023
1 parent 82632af commit 09730fc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 88 deletions.
22 changes: 3 additions & 19 deletions mknodes/basenodes/mkadmonition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from mknodes.basenodes import mkcontainer, mknode
from mknodes.data import datatypes
from mknodes.utils import log, reprhelpers, resources
from mknodes.utils import log, resources


logger = log.get_logger(__name__)
Expand Down Expand Up @@ -52,22 +52,6 @@ def __init__(
self.expanded = expanded
super().__init__(content=content, **kwargs)

def __repr__(self):
if len(self.items) == 1:
content = reprhelpers.to_str_if_textnode(self.items[0])
else:
content = [reprhelpers.to_str_if_textnode(i) for i in self.items]
return reprhelpers.get_repr(
self,
content=content,
typ=self.typ if self.typ != "info" else None,
title=self.title,
collapsible=self.collapsible,
expanded=self.expanded,
_filter_empty=True,
_filter_false=True,
)

def attach_annotations(self, text: str) -> str:
# we deal with attaching annotations ourselves.
return text
Expand Down Expand Up @@ -96,5 +80,5 @@ def _to_markdown(self) -> str:


if __name__ == "__main__":
admonition = MkAdmonition("")
print(admonition)
admonition = MkAdmonition("fdsf", collapsible=True)
print(repr(admonition))
50 changes: 5 additions & 45 deletions mknodes/basenodes/mkcard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from typing import TYPE_CHECKING, Any

from mknodes.basenodes import mkbinaryimage, mknode
from mknodes.utils import log, resources, xmlhelpers as xml
from mknodes.basenodes import mkbinaryimage
from mknodes.templatenodes import mktemplate
from mknodes.utils import log, resources


if TYPE_CHECKING:
Expand All @@ -16,37 +17,7 @@
CARD_DEFAULT_SIZE = 200


def build_html_card(
*,
image: str,
title: str,
link: str | None = None,
size: int = CARD_DEFAULT_SIZE,
caption: str | None = None,
path_dark_mode: str | None = None,
) -> xml.HTMLElement:
card_div = xml.Div("card")
container_div = xml.Div("showcase-container", parent=card_div)
src = f"{image}#only-light" if path_dark_mode else image
style = f"width:{size}px,height:{size}px"
xml.Img(parent=container_div, src=src, alt=title, style=style)
if path_dark_mode:
src = f"{path_dark_mode}#only-dark"
style = f"width:{size}px,height:{size}px"
xml.Img(parent=container_div, src=src, alt=title, style=style)
if caption:
xml.Div("overlay", text=caption, parent=container_div)
p = xml.P(parent=card_div)
button = xml.Button(parent=p)
button.text = title
if link:
tag = xml.A(href=link)
tag.append(card_div)
return tag
return card_div


class MkCard(mknode.MkNode):
class MkCard(mktemplate.MkTemplate):
"""A card node, displaying an image, a button-like label and a hover caption.
This node requires addtional CSS to work.
Expand Down Expand Up @@ -78,7 +49,7 @@ def __init__(
path_dark_mode: Optional alternative image for dark mode
kwargs: keyword arguments passed to parent
"""
super().__init__(**kwargs)
super().__init__("output/markdown/template", **kwargs)
self.target = target
self.title = title
self.image = image
Expand All @@ -90,17 +61,6 @@ def __init__(
def url(self) -> str:
return self.ctx.links.get_url(self.target) if self.target else ""

def _to_markdown(self) -> str:
elem = build_html_card(
title=self.title,
image=self.image,
caption=self.caption,
link=self.url,
size=self.size,
path_dark_mode=self.path_dark_mode,
)
return elem.to_string(space="") + "\n"

@classmethod
def for_page(cls, page):
image = mkbinaryimage.MkBinaryImage.for_icon(page.icon)
Expand Down
27 changes: 4 additions & 23 deletions mknodes/basenodes/mkmaterialbadge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from typing import TYPE_CHECKING, Any

from mknodes.basenodes import mknode
from mknodes.utils import icons, log, resources, xmlhelpers as xml
from mknodes.templatenodes import mktemplate
from mknodes.utils import log, resources


logger = log.get_logger(__name__)
Expand All @@ -13,7 +13,7 @@
from mknodes.info import linkprovider


class MkMaterialBadge(mknode.MkNode):
class MkMaterialBadge(mktemplate.MkTemplate):
"""Node for a CSS-based badge a la MkDocs-Material."""

ICON = "simple/shieldsdotio"
Expand All @@ -39,32 +39,13 @@ def __init__(
target: An optional URL / page target for the badge
kwargs: Keyword arguments passed to parent
"""
super().__init__(**kwargs)
super().__init__("output/html/template", **kwargs)
self.icon = icon
self.text = text
self.animated = animated
self.align_right = align_right
self.target = target

def _to_markdown(self):
classes = "md-typeset mdx-badge"
if self.animated:
classes += " mdx-badge--heart"
if self.align_right:
classes += " mdx-badge--right"
root = xml.Span(classes)
if self.icon:
icon = icons.get_emoji_slug(self.icon)
icon_str = f"[{icon}]({self.url})" if self.url else icon
xml.Span("mdx-badge__icon", parent=root, text=icon_str)
if self.text:
xml.Span("mdx-badge__text", parent=root, text=self.text)
return root.to_string()

@property
def url(self) -> str:
return self.ctx.links.get_url(self.target) if self.target else ""


if __name__ == "__main__":
img = MkMaterialBadge("mdi:wrench", "test", align_right=True, animated=True)
Expand Down
2 changes: 1 addition & 1 deletion mknodes/basenodes/mkmaterialbadge/metadata.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jinja = """
[output.html]
template = """
<span class="md-typeset mdx-badge{{ " mdx-badge--right" if node.align_right else "" }}{{ " mdx-badge--heart" if node.animated else "" }}">
{% set icon = "[" ~ node.icon | get_emoji_slug ~ "](" ~ node.url ~ ")" if node.url else node.icon | get_emoji_slug %}
{% set icon = "[" ~ node.icon | get_emoji_slug ~ "](" ~ node.target | get_url ~ ")" if node.target else node.icon | get_emoji_slug %}
{% if node.icon %} <span class="mdx-badge__icon">{{ icon }}</span>
{% endif %}
{% if node.text %} <span class="mdx-badge__text">{{ node.text }}</span>
Expand Down

0 comments on commit 09730fc

Please sign in to comment.