feature: add latex renderer

This commit is contained in:
Prof. Dr. Matthias Jung DL9MJ
2024-12-08 17:01:21 +01:00
parent af6c18c47a
commit 13763f6c7f
3 changed files with 101 additions and 4 deletions
+2 -2
View File
@@ -74,8 +74,8 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
if(token.tagtype == "webmargin"):
type = "margin"
elif(token.tagtype == "webtipp"):
type = "tipp"
elif(token.tagtype == "webtip"):
type = "tip"
elif(token.tagtype == "webindepth"):
type = "indepth"
else :
+97
View File
@@ -0,0 +1,97 @@
from mistletoe.latex_renderer import LaTeXRenderer
from renderer.comment import BlockComment, SpanComment
from renderer.dash import Dash
from renderer.halfwidth_spaces import HalfwidthSpaces
from renderer.nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
from renderer.quote import Quote
from renderer.tag import Tag
from renderer.underline import Underline
class FiftyOhmLaTeXRenderer(LaTeXRenderer):
def __init__(self):
super().__init__(
Dash,
BlockComment,
SpanComment,
Quote,
Underline,
Tag,
HalfwidthSpaces,
NonbreakingSpaces,
NonbreakingSpacesDots,
)
def render_document(self, token):
self.footnotes.update(token.footnotes)
return self.render_inner(token)
def render_dash(self, token):
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
def render_quote(self, token):
return fr"\enquote{{{self.render_inner(token)}}}"
def render_emphasis(self, token):
return fr"\emph{{{self.render_inner(token)}}}"
def render_underline(self, token):
return fr"\underline{{{self.render_inner(token)}}}"
def render_thematic_break(self, token):
return ""
def render_tag(self, token): # noqa: C901
tagtype = None
match token.tagtype:
case "danger":
tagtype = "MarginDanger"
case "warning":
tagtype = "MarginWarning"
case "attention":
tagtype = "MarginAttention"
case "tip":
tagtype = "MarginTip"
case "webtip":
tagtype = "WebTip"
case "unit":
tagtype = "MarginUnit"
case "indepth":
tagtype = "MarginInDepth"
case "webindepth":
tagtype = "MarginWebInDepth"
case "webmargin":
tagtype = "WebMargin"
case "fullwidth":
tagtype = "FullWidth"
case "latexonly":
return self.render_inner(token)
case "webonly":
return ""
case _:
tagtype = "Margin"
return fr"\{tagtype}{{{self.render_inner(token)}}}"
def render_halfwidth_spaces(self, token):
return f"{token.first}.\,{token.second}."
def render_nonbreaking_spaces(self, token):
return f"{token.first}~{token.second}"
def render_nonbreaking_spaces_dots(self, token):
lookup = {
"" : "",
" " : "~"
}
return f"{lookup[token.first]}{token.second}{lookup[token.third]}"
+2 -2
View File
@@ -19,8 +19,8 @@ def test_tag_html() :
"<margin>\nFoo\n</margin>" : FiftyOhmHtmlRenderer.render_tag_helper("margin","<p>Foo</p>",1,0)+'\n',
"<indepth>\nFoo\n</indepth>" : FiftyOhmHtmlRenderer.render_tag_helper("indepth","<p>Foo</p>",1,0)+'\n',
"<webmargin>\nFoo\n</webmargin>" : FiftyOhmHtmlRenderer.render_tag_helper("margin","<p>Foo</p>",1,0)+'\n',
"<tipp>\nFoo\n</tipp>" : FiftyOhmHtmlRenderer.render_tag_helper("tipp","<p>Foo</p>",1,0)+'\n',
"<webtipp>\nFoo\n</webtipp>" : FiftyOhmHtmlRenderer.render_tag_helper("tipp","<p>Foo</p>",1,0)+'\n',
"<tip>\nFoo\n</tip>" : FiftyOhmHtmlRenderer.render_tag_helper("tip","<p>Foo</p>",1,0)+'\n',
"<webtip>\nFoo\n</webtip>" : FiftyOhmHtmlRenderer.render_tag_helper("tip","<p>Foo</p>",1,0)+'\n',
"<webindepth>\nFoo\n</webindepth>" : FiftyOhmHtmlRenderer.render_tag_helper("indepth","<p>Foo</p>",1,0)+'\n',
"<unit>\nFoo\n</unit>" : FiftyOhmHtmlRenderer.render_tag_helper("unit","<p>Foo</p>",1,0)+'\n',
"<danger>\nFoo\n</danger>" : FiftyOhmHtmlRenderer.render_tag_helper("danger","<p>Foo</p>",1,0)+'\n',