Pull request #24: feat: parse questions von @dl9mj
Merge in FRAG/50ohm.de from feat/parse-questions to main * commit '9ea98a941d85128fe93dba3b3b81bfe1afb53b90': feat: parse questions von @dl9mj
This commit is contained in:
@@ -7,6 +7,7 @@ from .dash import Dash
|
||||
from .halfwidth_spaces import HalfwidthSpaces
|
||||
from .morse import Morse
|
||||
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
||||
from .question import Question
|
||||
from .quote import Quote
|
||||
from .references import References
|
||||
from .tag import Tag
|
||||
@@ -20,7 +21,7 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
||||
section_url = "section.html"
|
||||
ref_id = 0
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, question_renderer=None):
|
||||
super().__init__(
|
||||
Dash,
|
||||
BlockComment,
|
||||
@@ -33,7 +34,9 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
||||
NonbreakingSpaces,
|
||||
NonbreakingSpacesDots,
|
||||
References,
|
||||
Question,
|
||||
)
|
||||
self.question_renderer = question_renderer
|
||||
|
||||
def render_dash(self, token):
|
||||
return " – "
|
||||
@@ -157,6 +160,9 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
||||
def render_references(self, token):
|
||||
return f'<a href="{self.section_url}#ref_{token.first}" onclick="highlightRef(\'{token.first}\');">{self.ref_id}</a>'
|
||||
|
||||
def render_question(self, token):
|
||||
return self.question_renderer(token.question_number)
|
||||
|
||||
def render_document(self, token: Document) -> str:
|
||||
self.footnotes.update(token.footnotes)
|
||||
# Filter out None values, so block tokens can return None to not be rendered.
|
||||
|
||||
@@ -4,13 +4,14 @@ from .comment import BlockComment
|
||||
from .dash import Dash
|
||||
from .halfwidth_spaces import HalfwidthSpaces
|
||||
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
||||
from .question import Question
|
||||
from .quote import Quote
|
||||
from .tag import Tag
|
||||
from .underline import Underline
|
||||
|
||||
|
||||
class FiftyOhmLaTeXRenderer(LaTeXRenderer):
|
||||
def __init__(self):
|
||||
def __init__(self, question_renderer=None):
|
||||
super().__init__(
|
||||
Dash,
|
||||
BlockComment,
|
||||
@@ -20,7 +21,9 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
|
||||
HalfwidthSpaces,
|
||||
NonbreakingSpaces,
|
||||
NonbreakingSpacesDots,
|
||||
Question,
|
||||
)
|
||||
self.question_renderer = question_renderer
|
||||
|
||||
def render_document(self, token):
|
||||
self.footnotes.update(token.footnotes)
|
||||
@@ -86,3 +89,6 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
|
||||
def render_nonbreaking_spaces_dots(self, token):
|
||||
lookup = {"": "", " ": "~"}
|
||||
return f"{lookup[token.first]}{token.second}{lookup[token.third]}"
|
||||
|
||||
def render_question(self, token):
|
||||
return self.question_renderer(token.question_number)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import re
|
||||
|
||||
from mistletoe.block_token import BlockToken
|
||||
|
||||
|
||||
class Question(BlockToken):
|
||||
@staticmethod
|
||||
def start(line):
|
||||
# Search for:
|
||||
# [question:123]
|
||||
return re.match(r"^\s*\[question:\d+\]", line)
|
||||
|
||||
@classmethod
|
||||
def read(cls, lines):
|
||||
first_line = next(lines)
|
||||
question_number = re.match(r"^\s*\[question:(\d+)\]", first_line).group(1)
|
||||
return question_number
|
||||
|
||||
@classmethod
|
||||
def check_interrupts_paragraph(cls, lines):
|
||||
return cls.start(lines.peek())
|
||||
|
||||
def __init__(self, match):
|
||||
self.question_number = match
|
||||
+9
-4
@@ -1,12 +1,17 @@
|
||||
import mistletoe
|
||||
from mistletoe import Document
|
||||
|
||||
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
|
||||
|
||||
|
||||
def question_stub(input):
|
||||
return f"<question>{input}</question>" # TODO implement this stub
|
||||
|
||||
|
||||
def test_html(capsys):
|
||||
with capsys.disabled():
|
||||
with open("test/acceptanceTest.md") as file:
|
||||
content = file.read()
|
||||
output = mistletoe.markdown(content, FiftyOhmHtmlRenderer)
|
||||
with open("test/acceptanceTest.html", "w") as output_file:
|
||||
output_file.write(output)
|
||||
with FiftyOhmHtmlRenderer(question_stub) as renderer:
|
||||
output = renderer.render(Document(content))
|
||||
with open("test/acceptanceTest.html", "w") as output_file:
|
||||
output_file.write(output)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import pytest
|
||||
from mistletoe import Document
|
||||
|
||||
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
|
||||
from renderer.fifty_ohm_latex_renderer import FiftyOhmLaTeXRenderer
|
||||
from test.util import paragraph
|
||||
|
||||
|
||||
@pytest.mark.html
|
||||
def test_comment_html():
|
||||
assertions = {
|
||||
"[question:123]": "123\n",
|
||||
"\n[question:123]\n": "123\n",
|
||||
"\n[question:123]\n[question:123]": "123\n123\n",
|
||||
"Foo\n[question:123]\nBar": paragraph("Foo") + "123\n" + paragraph("Bar"),
|
||||
}
|
||||
|
||||
def test_function(input):
|
||||
return f"{input}"
|
||||
|
||||
with FiftyOhmHtmlRenderer(test_function) as renderer:
|
||||
for assertion in assertions:
|
||||
assert renderer.render(Document(assertion)) == assertions[assertion]
|
||||
|
||||
|
||||
@pytest.mark.latex
|
||||
def test_comment_latex():
|
||||
assertions = {
|
||||
"[question:123]": "123",
|
||||
"\n[question:123]\n": "123",
|
||||
"\n[question:123]\n[question:123]": "123123",
|
||||
"Foo\n[question:123]\nBar": "\nFoo\n123\nBar\n",
|
||||
}
|
||||
|
||||
def test_function(input):
|
||||
return f"{input}"
|
||||
|
||||
with FiftyOhmLaTeXRenderer(test_function) as renderer:
|
||||
for assertion in assertions:
|
||||
assert renderer.render(Document(assertion)) == assertions[assertion]
|
||||
Reference in New Issue
Block a user