feat: parse questions von @dl9mj
This commit is contained in:
@@ -7,6 +7,7 @@ from .dash import Dash
|
|||||||
from .halfwidth_spaces import HalfwidthSpaces
|
from .halfwidth_spaces import HalfwidthSpaces
|
||||||
from .morse import Morse
|
from .morse import Morse
|
||||||
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
||||||
|
from .question import Question
|
||||||
from .quote import Quote
|
from .quote import Quote
|
||||||
from .references import References
|
from .references import References
|
||||||
from .tag import Tag
|
from .tag import Tag
|
||||||
@@ -20,7 +21,7 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
section_url = "section.html"
|
section_url = "section.html"
|
||||||
ref_id = 0
|
ref_id = 0
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, question_renderer=None):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
Dash,
|
Dash,
|
||||||
BlockComment,
|
BlockComment,
|
||||||
@@ -33,7 +34,9 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
NonbreakingSpaces,
|
NonbreakingSpaces,
|
||||||
NonbreakingSpacesDots,
|
NonbreakingSpacesDots,
|
||||||
References,
|
References,
|
||||||
|
Question,
|
||||||
)
|
)
|
||||||
|
self.question_renderer = question_renderer
|
||||||
|
|
||||||
def render_dash(self, token):
|
def render_dash(self, token):
|
||||||
return " – "
|
return " – "
|
||||||
@@ -157,6 +160,9 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
def render_references(self, token):
|
def render_references(self, token):
|
||||||
return f'<a href="{self.section_url}#ref_{token.first}" onclick="highlightRef(\'{token.first}\');">{self.ref_id}</a>'
|
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:
|
def render_document(self, token: Document) -> str:
|
||||||
self.footnotes.update(token.footnotes)
|
self.footnotes.update(token.footnotes)
|
||||||
# Filter out None values, so block tokens can return None to not be rendered.
|
# 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 .dash import Dash
|
||||||
from .halfwidth_spaces import HalfwidthSpaces
|
from .halfwidth_spaces import HalfwidthSpaces
|
||||||
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
||||||
|
from .question import Question
|
||||||
from .quote import Quote
|
from .quote import Quote
|
||||||
from .tag import Tag
|
from .tag import Tag
|
||||||
from .underline import Underline
|
from .underline import Underline
|
||||||
|
|
||||||
|
|
||||||
class FiftyOhmLaTeXRenderer(LaTeXRenderer):
|
class FiftyOhmLaTeXRenderer(LaTeXRenderer):
|
||||||
def __init__(self):
|
def __init__(self, question_renderer=None):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
Dash,
|
Dash,
|
||||||
BlockComment,
|
BlockComment,
|
||||||
@@ -20,7 +21,9 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
|
|||||||
HalfwidthSpaces,
|
HalfwidthSpaces,
|
||||||
NonbreakingSpaces,
|
NonbreakingSpaces,
|
||||||
NonbreakingSpacesDots,
|
NonbreakingSpacesDots,
|
||||||
|
Question,
|
||||||
)
|
)
|
||||||
|
self.question_renderer = question_renderer
|
||||||
|
|
||||||
def render_document(self, token):
|
def render_document(self, token):
|
||||||
self.footnotes.update(token.footnotes)
|
self.footnotes.update(token.footnotes)
|
||||||
@@ -86,3 +89,6 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
|
|||||||
def render_nonbreaking_spaces_dots(self, token):
|
def render_nonbreaking_spaces_dots(self, token):
|
||||||
lookup = {"": "", " ": "~"}
|
lookup = {"": "", " ": "~"}
|
||||||
return f"{lookup[token.first]}{token.second}{lookup[token.third]}"
|
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
|
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):
|
def test_html(capsys):
|
||||||
with capsys.disabled():
|
with capsys.disabled():
|
||||||
with open("test/acceptanceTest.md") as file:
|
with open("test/acceptanceTest.md") as file:
|
||||||
content = file.read()
|
content = file.read()
|
||||||
output = mistletoe.markdown(content, FiftyOhmHtmlRenderer)
|
with FiftyOhmHtmlRenderer(question_stub) as renderer:
|
||||||
with open("test/acceptanceTest.html", "w") as output_file:
|
output = renderer.render(Document(content))
|
||||||
output_file.write(output)
|
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