feat: render comments in LaTeX

This commit is contained in:
Tim Grelka
2025-02-16 17:53:03 +01:00
parent 38635a7abd
commit fe7ded0278
3 changed files with 15 additions and 13 deletions
+10 -8
View File
@@ -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)}“"
@@ -158,5 +154,11 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
lookup = {"": "", " ": " "} lookup = {"": "", " ": " "}
return f"{lookup[token.first]}{token.second}{lookup[token.third]}" return f"{lookup[token.first]}{token.second}{lookup[token.third]}"
def render_references(self, token): def render_references(self, token: References):
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 "{}\n".format(inner) if inner else ""
+1 -1
View File
@@ -30,7 +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}"
def render_quote(self, token): def render_quote(self, token):
return rf"\enquote{{{self.render_inner(token)}}}" return rf"\enquote{{{self.render_inner(token)}}}"
+4 -4
View File
@@ -23,10 +23,10 @@ def test_comment_html():
@pytest.mark.latex @pytest.mark.latex
def test_comment_latex(): def test_comment_latex():
assertions = { assertions = {
"%Comment\nBar": "\nBar\n", "%Comment\nBar": "% Comment\nBar\n",
# TODO: This test is not working as expected. The comment should be removed "%Comment\n": "% Comment",
# "%Comment\n" : "a", "Foo 100 % Bar": "\nFoo 100 \% Bar\n",
"Foo\n%Comment\nBar": "\nFoo\nBar\n", "Foo\n%Comment\nBar": "\nFoo\n% Comment\nBar\n",
} }
for assertion in assertions: for assertion in assertions: