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:
+10
-11
@@ -4,21 +4,20 @@ from mistletoe.block_token import BlockToken
|
|||||||
from mistletoe.span_token import SpanToken
|
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):
|
class BlockComment(BlockToken):
|
||||||
|
def __init__(self, match):
|
||||||
|
self.content = match
|
||||||
|
self.children = []
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def start(line):
|
def start(line):
|
||||||
return line.startswith("%")
|
return line.startswith("%")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def check_interrupts_paragraph(cls, lines):
|
||||||
|
return cls.start(lines.peek())
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def read(cls, lines):
|
def read(cls, lines):
|
||||||
next(lines)
|
comment = re.match(r"%(.*)", next(lines)).group(1).strip()
|
||||||
return None
|
return comment
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import textwrap
|
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 .dash import Dash
|
||||||
from .halfwidth_spaces import HalfwidthSpaces
|
from .halfwidth_spaces import HalfwidthSpaces
|
||||||
from .morse import Morse
|
from .morse import Morse
|
||||||
@@ -24,7 +24,6 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
super().__init__(
|
super().__init__(
|
||||||
Dash,
|
Dash,
|
||||||
BlockComment,
|
BlockComment,
|
||||||
SpanComment,
|
|
||||||
Quote,
|
Quote,
|
||||||
Unit,
|
Unit,
|
||||||
Underline,
|
Underline,
|
||||||
@@ -40,10 +39,7 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
return " – "
|
return " – "
|
||||||
|
|
||||||
def render_block_comment(self, token):
|
def render_block_comment(self, token):
|
||||||
return ""
|
return None
|
||||||
|
|
||||||
def render_span_comment(self, token):
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def render_quote(self, token):
|
def render_quote(self, token):
|
||||||
return f"„{self.render_inner(token)}“"
|
return f"„{self.render_inner(token)}“"
|
||||||
@@ -160,3 +156,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_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 ""
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from mistletoe.latex_renderer import LaTeXRenderer
|
from mistletoe.latex_renderer import LaTeXRenderer
|
||||||
|
|
||||||
from .comment import BlockComment, SpanComment
|
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
|
||||||
@@ -14,7 +14,6 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
|
|||||||
super().__init__(
|
super().__init__(
|
||||||
Dash,
|
Dash,
|
||||||
BlockComment,
|
BlockComment,
|
||||||
SpanComment,
|
|
||||||
Quote,
|
Quote,
|
||||||
Underline,
|
Underline,
|
||||||
Tag,
|
Tag,
|
||||||
@@ -31,10 +30,7 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
|
|||||||
return " -- "
|
return " -- "
|
||||||
|
|
||||||
def render_block_comment(self, token):
|
def render_block_comment(self, token):
|
||||||
return "" # TODO propagate comments to the LaTeX document
|
return f"% {token.content}\n"
|
||||||
|
|
||||||
def render_span_comment(self, token):
|
|
||||||
return "" # TODO propagate comments to the LaTeX document
|
|
||||||
|
|
||||||
def render_quote(self, token):
|
def render_quote(self, token):
|
||||||
return rf"\enquote{{{self.render_inner(token)}}}"
|
return rf"\enquote{{{self.render_inner(token)}}}"
|
||||||
|
|||||||
@@ -9,23 +9,23 @@ from test.util import paragraph
|
|||||||
@pytest.mark.html
|
@pytest.mark.html
|
||||||
def test_comment_html():
|
def test_comment_html():
|
||||||
assertions = {
|
assertions = {
|
||||||
"%Comment\nBar": "Bar",
|
"%Comment\nBar": paragraph("Bar"),
|
||||||
# TODO: This test is not working as expected. The comment should be removed
|
"%Comment\n": "",
|
||||||
# "%Comment\n" : "a",
|
"Foo 100 % Bar": paragraph("Foo 100% Bar"),
|
||||||
"Foo\n%Comment\nBar": "Foo\nBar",
|
"Foo\n%Comment\nBar": paragraph("Foo") + paragraph("Bar"),
|
||||||
}
|
}
|
||||||
|
|
||||||
for assertion in assertions:
|
for assertion in assertions:
|
||||||
assert mistletoe.markdown(assertion, FiftyOhmHtmlRenderer) == paragraph(assertions[assertion])
|
assert mistletoe.markdown(assertion, FiftyOhmHtmlRenderer) == assertions[assertion]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.latex
|
@pytest.mark.latex
|
||||||
def test_comment_latex():
|
def test_comment_latex():
|
||||||
assertions = {
|
assertions = {
|
||||||
"%Comment\nBar": "\nBar\n",
|
"%Comment\nBar": "% Comment\n\nBar\n",
|
||||||
# TODO: This test is not working as expected. The comment should be removed
|
"%Comment\n": "% Comment\n",
|
||||||
# "%Comment\n" : "a",
|
"Foo 100 % Bar": "\nFoo 100 \% Bar\n",
|
||||||
"Foo\n%Comment\nBar": "\nFoo\nBar\n",
|
"Foo\n%Comment\nBar": "\nFoo\n% Comment\n\nBar\n",
|
||||||
}
|
}
|
||||||
|
|
||||||
for assertion in assertions:
|
for assertion in assertions:
|
||||||
|
|||||||
Reference in New Issue
Block a user