Pull request #32: feature: add qso rendering
Merge in FRAG/50ohm.de from qso to main * commit 'f9aeed4aca44d852fc3821ec7b1265028c25f027': Angewendeter Vorschlag feature: add qso rendering
This commit is contained in:
@@ -9,6 +9,7 @@ from .morse import Morse
|
||||
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
||||
from .photo import Photo
|
||||
from .picture import Picture
|
||||
from .qso import Qso
|
||||
from .question import Question
|
||||
from .quote import Quote
|
||||
from .references import References
|
||||
@@ -43,6 +44,7 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
||||
Picture,
|
||||
Photo,
|
||||
Table,
|
||||
Qso,
|
||||
)
|
||||
self.question_renderer = question_renderer
|
||||
|
||||
@@ -155,6 +157,14 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
||||
|
||||
return self.render_tag_helper(type, self.render_inner(token), self.margin_id, self.margin_anchor_id)
|
||||
|
||||
def render_qso(self, token):
|
||||
self.margin_id += 1
|
||||
qso = ""
|
||||
for child in token.children:
|
||||
direction = "other" if child.received else "own"
|
||||
qso += f'<div class="qso_{direction}">{self.render_inner(child)}</div>\n'
|
||||
return self.render_tag_helper("qso", qso, self.margin_id, self.margin_anchor_id)
|
||||
|
||||
def render_halfwidth_spaces(self, token):
|
||||
return f"{token.first}. {token.second}."
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import re
|
||||
|
||||
from mistletoe import span_token
|
||||
from mistletoe.block_token import BlockToken
|
||||
|
||||
|
||||
class Qso(BlockToken):
|
||||
@staticmethod
|
||||
def start(line):
|
||||
return re.match(r"^\s*<qso>", line)
|
||||
|
||||
@classmethod
|
||||
def read(cls, lines):
|
||||
child_lines = []
|
||||
|
||||
for line in lines:
|
||||
if line.startswith("<qso>"):
|
||||
continue
|
||||
if line.startswith("</qso>"):
|
||||
break
|
||||
else:
|
||||
child_lines.append(QsoLine(line))
|
||||
return child_lines
|
||||
|
||||
def __init__(self, match):
|
||||
self.children = match
|
||||
|
||||
class QsoLine(BlockToken):
|
||||
@classmethod
|
||||
def parse_line(cls, line):
|
||||
if line.startswith(">"):
|
||||
return True, line[1:].strip()
|
||||
else:
|
||||
return False, line.strip()
|
||||
|
||||
def __init__(self, match):
|
||||
self.received, self.text = self.parse_line(match)
|
||||
super().__init__(self.text, span_token.tokenize_inner)
|
||||
@@ -0,0 +1,17 @@
|
||||
import mistletoe
|
||||
import pytest
|
||||
|
||||
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
|
||||
|
||||
|
||||
@pytest.mark.html
|
||||
def test_table_html():
|
||||
assertions = {
|
||||
"<qso>\nfoo\n</qso>": FiftyOhmHtmlRenderer.render_tag_helper("qso", '<div class="qso_own">foo</div>', 1, 0) +"\n",
|
||||
"<qso>\nfoo\n> bar\n</qso>": FiftyOhmHtmlRenderer.render_tag_helper("qso", '<div class="qso_own">foo</div><div class="qso_other">bar</div>', 1, 0) +"\n",
|
||||
"<qso>\n*foo*\n> bar\n</qso>": FiftyOhmHtmlRenderer.render_tag_helper("qso", '<div class="qso_own"><em>foo</em></div><div class="qso_other">bar</div>', 1, 0) +"\n",
|
||||
}
|
||||
|
||||
for assertion in assertions:
|
||||
assert mistletoe.markdown(assertion, FiftyOhmHtmlRenderer) == assertions[assertion]
|
||||
|
||||
Reference in New Issue
Block a user