diff options
-rw-r--r-- | fakehtmltag.py | 31 | ||||
-rw-r--r-- | quote.py | 4 |
2 files changed, 33 insertions, 2 deletions
diff --git a/fakehtmltag.py b/fakehtmltag.py new file mode 100644 index 0000000..4a7dfa4 --- /dev/null +++ b/fakehtmltag.py @@ -0,0 +1,31 @@ +""" +Encapsulates text in a fake html tag that is colored (class="cyan" and class="yellow") and has a "nobr" class for no wrapping. +These all can be styled in css. + +Usage: +{{my_fake_html_tag}}content{{}} +""" +from marko import inline +from marko.helpers import MarkoExtension + + +class FakeHtmlElement(inline.InlineElement): + pattern = '{{(.*)}}(.*){{}}' + parse_children = True + parse_group = 2 + + def __init__(self, match): + self.tag = match.group(1) + self.content = match.group(2) + + + +class FakeHtmlRenderer: + def render_fake_html_element(self, element: FakeHtmlElement): + return f'<span class="nobr"><span class="cyan"><</span><span class="yellow">{element.tag}</span><span class="cyan">></span></span>{self.render_children(element)}<span class="nobr"><span class="cyan"></</span><span class="yellow">{element.tag}</span><span class="cyan">></span></span>' + + +FakeHtml = MarkoExtension( + elements=[FakeHtmlElement], + renderer_mixins=[FakeHtmlRenderer] +) @@ -16,7 +16,7 @@ class QuoteElement(inline.InlineElement): parse_children = True def __init__(self, match): - self.quote = match.group(1) + self.children = match.group(1) self.source = match.group(2) if match.group(2) is not None and not match.group(2).isspace() else None @@ -24,7 +24,7 @@ class QuoteElement(inline.InlineElement): class QuoteRenderer: def render_quote_element(self, element: QuoteElement): source = f'<p class="quote-source">-- {element.source}</p>' if element.source is not None else '' - return f'<div class="quote"><div class="quote-main"><p>"</p><p>{element.quote}</p><p>"</p></div>{source}</div>' + return f'<div class="quote"><div class="quote-main"><p>"</p><p>{self.render_children(element)}</p><p>"</p></div>{source}</div>' Quote = MarkoExtension( |