diff --git a/renderer/comment.py b/renderer/comment.py index ee70ef3..7626af8 100644 --- a/renderer/comment.py +++ b/renderer/comment.py @@ -4,21 +4,20 @@ from mistletoe.block_token import BlockToken from mistletoe.span_token import SpanToken -class SpanComment(SpanToken): - """ - Coment span token (% Eiersalat). - Identifies a comment in text. - """ - - pattern = re.compile(r"(%.*\n?)") - - class BlockComment(BlockToken): + def __init__(self, match): + self.content = match + self.children = [] + @staticmethod def start(line): return line.startswith("%") + @classmethod + def check_interrupts_paragraph(cls, lines): + return cls.start(lines.peek()) + @classmethod def read(cls, lines): - next(lines) - return None + comment = re.match(r"%(.*)", next(lines)).group(1).strip() + return comment diff --git a/renderer/fifty_ohm_html_renderer.py b/renderer/fifty_ohm_html_renderer.py index db8a67d..0881c04 100644 --- a/renderer/fifty_ohm_html_renderer.py +++ b/renderer/fifty_ohm_html_renderer.py @@ -1,8 +1,8 @@ import textwrap -from mistletoe import HtmlRenderer +from mistletoe import Document, HtmlRenderer -from .comment import BlockComment, SpanComment +from .comment import BlockComment from .dash import Dash from .halfwidth_spaces import HalfwidthSpaces from .morse import Morse @@ -24,7 +24,6 @@ class FiftyOhmHtmlRenderer(HtmlRenderer): super().__init__( Dash, BlockComment, - SpanComment, Quote, Unit, Underline, @@ -40,10 +39,7 @@ class FiftyOhmHtmlRenderer(HtmlRenderer): return " – " def render_block_comment(self, token): - return "" - - def render_span_comment(self, token): - return "" + return None def render_quote(self, token): return f"„{self.render_inner(token)}“" @@ -160,3 +156,9 @@ class FiftyOhmHtmlRenderer(HtmlRenderer): def render_references(self, token): return f'{self.ref_id}' + + 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. + inner = "\n".join(filter(lambda x: x is not None, [self.render(child) for child in token.children])) + return f"{inner}\n" if inner else "" diff --git a/renderer/fifty_ohm_latex_renderer.py b/renderer/fifty_ohm_latex_renderer.py index 1e1c204..802cd96 100644 --- a/renderer/fifty_ohm_latex_renderer.py +++ b/renderer/fifty_ohm_latex_renderer.py @@ -1,6 +1,6 @@ from mistletoe.latex_renderer import LaTeXRenderer -from .comment import BlockComment, SpanComment +from .comment import BlockComment from .dash import Dash from .halfwidth_spaces import HalfwidthSpaces from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots @@ -14,7 +14,6 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer): super().__init__( Dash, BlockComment, - SpanComment, Quote, Underline, Tag, @@ -31,10 +30,7 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer): return " -- " def render_block_comment(self, token): - return "" # TODO propagate comments to the LaTeX document - - def render_span_comment(self, token): - return "" # TODO propagate comments to the LaTeX document + return f"% {token.content}\n" def render_quote(self, token): return rf"\enquote{{{self.render_inner(token)}}}" diff --git a/test/test_comment.py b/test/test_comment.py index 28c3612..dedb47f 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -9,23 +9,23 @@ from test.util import paragraph @pytest.mark.html def test_comment_html(): assertions = { - "%Comment\nBar": "Bar", - # TODO: This test is not working as expected. The comment should be removed - # "%Comment\n" : "a", - "Foo\n%Comment\nBar": "Foo\nBar", + "%Comment\nBar": paragraph("Bar"), + "%Comment\n": "", + "Foo 100 % Bar": paragraph("Foo 100% Bar"), + "Foo\n%Comment\nBar": paragraph("Foo") + paragraph("Bar"), } for assertion in assertions: - assert mistletoe.markdown(assertion, FiftyOhmHtmlRenderer) == paragraph(assertions[assertion]) + assert mistletoe.markdown(assertion, FiftyOhmHtmlRenderer) == assertions[assertion] @pytest.mark.latex def test_comment_latex(): assertions = { - "%Comment\nBar": "\nBar\n", - # TODO: This test is not working as expected. The comment should be removed - # "%Comment\n" : "a", - "Foo\n%Comment\nBar": "\nFoo\nBar\n", + "%Comment\nBar": "% Comment\n\nBar\n", + "%Comment\n": "% Comment\n", + "Foo 100 % Bar": "\nFoo 100 \% Bar\n", + "Foo\n%Comment\nBar": "\nFoo\n% Comment\n\nBar\n", } for assertion in assertions: