Pull request #22: Feat/improve comment parsing

Merge in FRAG/50ohm.de from feat/improve-comment-parsing to main

* commit 'f2cf3231279982f74110b5d11c71accb7192b98c':
  refactor: remove superflous comment
  refactor: use f string formatting
  refactor: remove unused code
  fix: add line break after LaTeX comment
  feat: render comments in LaTeX
  feat: detect and remove comments
This commit is contained in:
Tim Grelka DA2TG
2025-02-16 18:19:49 +00:00
4 changed files with 30 additions and 33 deletions
+10 -11
View File
@@ -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
+9 -7
View File
@@ -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'<a href="{self.section_url}#ref_{token.first}" onclick="highlightRef(\'{token.first}\');">{self.ref_id}</a>'
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 ""
+2 -6
View File
@@ -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)}}}"
+9 -9
View File
@@ -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: