Pull request #27: Feature/picture

Merge in FRAG/50ohm.de from feature/picture to main

* commit '326c925a56d249341743001c9e970994eee417c1':
  bugfix: Fixed bug
  feature: added latex
  Renamed Test
  First draft for pictures
This commit is contained in:
Prof.Dr. Matthias Jung DL9MJ
2025-05-13 07:20:10 +00:00
4 changed files with 72 additions and 0 deletions
+15
View File
@@ -2,6 +2,8 @@ import textwrap
from mistletoe import Document, HtmlRenderer
from renderer.picture import Picture
from .comment import BlockComment
from .dash import Dash
from .halfwidth_spaces import HalfwidthSpaces
@@ -35,6 +37,7 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
NonbreakingSpacesDots,
References,
Question,
Picture
)
self.question_renderer = question_renderer
@@ -168,3 +171,15 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
# 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 ""
@staticmethod
def render_picture_helper(id, ref, text, number):
return f"""
<figure class="picture" id="ref_{ref}" name="{ref}">
<img src="img/picture_{id}.svg">
<figcaption>Abbildung {number}: {text}</figcaption>
</figure>
"""
def render_picture(self, token) :
return self.render_picture_helper(token.id, token.ref, token.text, token.number)
+12
View File
@@ -1,5 +1,7 @@
from mistletoe.latex_renderer import LaTeXRenderer
from renderer.picture import Picture
from .comment import BlockComment
from .dash import Dash
from .halfwidth_spaces import HalfwidthSpaces
@@ -22,6 +24,7 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
NonbreakingSpaces,
NonbreakingSpacesDots,
Question,
Picture
)
self.question_renderer = question_renderer
@@ -92,3 +95,12 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
def render_question(self, token):
return self.question_renderer(token.question_number)
@staticmethod
def render_picture_helper(id, ref, text, number):
return rf"""\DARCimage{{1.0\linewidth}}{{{id}include}}
\captionof{{figure}}{{{text}}}
\label{{{ref}}}"""
def render_picture(self, token) :
return self.render_picture_helper(token.id, token.ref, token.text, token.number)
+19
View File
@@ -0,0 +1,19 @@
import re
from mistletoe.span_token import SpanToken
class Picture(SpanToken):
"""
Span token for pictures.
Example: [picture:727:amplitude_periode_halbwellen:Positive und negative Halbwellen einer Sinusschwingung]
"""
parse_inner = False
pattern = re.compile(r"\[picture:(\d+):(.+):(.+)\]")
def __init__(self, match_object):
self.id = match_object.group(1)
self.ref = match_object.group(2)
self.text = match_object.group(3)
self.number = "TODO"
+26
View File
@@ -0,0 +1,26 @@
import mistletoe
import pytest
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
from renderer.fifty_ohm_latex_renderer import FiftyOhmLaTeXRenderer
from test.util import paragraph
@pytest.mark.html
def test_picture_html():
assertions = {
"[picture:123:abc:Text]": FiftyOhmHtmlRenderer.render_picture_helper("123", "abc", "Text", "TODO"),
}
for assertion in assertions:
assert mistletoe.markdown(assertion, FiftyOhmHtmlRenderer) == paragraph(assertions[assertion])
@pytest.mark.latex
def test_picture_latex():
assertions = {
"[picture:123:abc:Text]": FiftyOhmLaTeXRenderer.render_picture_helper("123", "abc", "Text", "TODO"),
}
for assertion in assertions:
assert mistletoe.markdown(assertion, FiftyOhmLaTeXRenderer) == "\n" + assertions[assertion] + "\n"