From 38635a7abd8ee85172af1e06bd3b731a06570277 Mon Sep 17 00:00:00 2001 From: Tim Grelka Date: Sun, 16 Feb 2025 17:51:51 +0100 Subject: [PATCH] feat: detect and remove comments --- renderer/comment.py | 27 +++++++++++++++++---------- renderer/fifty_ohm_latex_renderer.py | 6 +----- test/test_comment.py | 11 ++++++----- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/renderer/comment.py b/renderer/comment.py index ee70ef3..dd3cb65 100644 --- a/renderer/comment.py +++ b/renderer/comment.py @@ -3,22 +3,29 @@ import re 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 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_latex_renderer.py b/renderer/fifty_ohm_latex_renderer.py index 1e1c204..1661884 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, @@ -33,9 +32,6 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer): 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 - 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..07d46a4 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -9,14 +9,15 @@ 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", + # TODOD: Remove \n + "%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