Pull request #29: feature: add photos

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

* commit 'd73914b2b9c0523dc15e98764d4e39e85c0bf34c':
  feature: add thest for photo
  feature: add photos
This commit is contained in:
Prof.Dr. Matthias Jung DL9MJ
2025-05-14 18:39:27 +00:00
4 changed files with 77 additions and 9 deletions
+18 -5
View File
@@ -2,13 +2,13 @@ import textwrap
from mistletoe import Document, HtmlRenderer from mistletoe import Document, HtmlRenderer
from renderer.picture import Picture
from .comment import BlockComment 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
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
from .photo import Photo
from .picture import Picture
from .question import Question from .question import Question
from .quote import Quote from .quote import Quote
from .references import References from .references import References
@@ -37,7 +37,8 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
NonbreakingSpacesDots, NonbreakingSpacesDots,
References, References,
Question, Question,
Picture Picture,
Photo
) )
self.question_renderer = question_renderer self.question_renderer = question_renderer
@@ -176,10 +177,22 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
def render_picture_helper(id, ref, text, number): def render_picture_helper(id, ref, text, number):
return f""" return f"""
<figure class="picture" id="ref_{ref}" name="{ref}"> <figure class="picture" id="ref_{ref}" name="{ref}">
<img src="img/picture_{id}.svg"> <img src="picture/picture_{id}.svg">
<figcaption>Abbildung {number}: {text}</figcaption> <figcaption>Abbildung {number}: {text}</figcaption>
</figure> </figure>
""" """
def render_picture(self, token) : def render_picture(self, token) :
return self.render_picture_helper(token.id, token.ref, token.text, token.number) return self.render_picture_helper(token.id, token.ref, token.text, token.number)
@staticmethod
def render_photo_helper(id, ref, text, number):
return f"""
<figure class="photo" id="ref_{ref}" name="{ref}">
<img src="photo/photo_{id}.png">
<figcaption>Abbildung {number}: {text}</figcaption>
</figure>
"""
def render_photo(self, token) :
return self.render_photo_helper(token.id, token.ref, token.text, token.number)
+14 -4
View File
@@ -1,11 +1,11 @@
from mistletoe.latex_renderer import LaTeXRenderer from mistletoe.latex_renderer import LaTeXRenderer
from renderer.picture import Picture
from .comment import BlockComment 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
from .photo import Photo
from .picture import Picture
from .question import Question from .question import Question
from .quote import Quote from .quote import Quote
from .tag import Tag from .tag import Tag
@@ -24,7 +24,8 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
NonbreakingSpaces, NonbreakingSpaces,
NonbreakingSpacesDots, NonbreakingSpacesDots,
Question, Question,
Picture Picture,
Photo
) )
self.question_renderer = question_renderer self.question_renderer = question_renderer
@@ -103,4 +104,13 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
\label{{{ref}}}""" \label{{{ref}}}"""
def render_picture(self, token) : def render_picture(self, token) :
return self.render_picture_helper(token.id, token.ref, token.text, token.number) return self.render_picture_helper(token.id, token.ref, token.text, token.number)
@staticmethod
def render_photo_helper(id, ref, text, number):
return rf"""\includegraphics[width=1.0\linewidth]{{photo/{id}}}
\captionof{{figure}}{{{text}}}
\label{{{ref}}}"""
def render_photo(self, token) :
return self.render_photo_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 Photo(SpanToken):
"""
Span token for photos.
Example: [photo:123:n_wasserfalldiagramm_trx_collage:Ansichten verschiedener Transceiver]
"""
parse_inner = False
pattern = re.compile(r"\[photo:(\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_photo_html():
assertions = {
"[photo:123:abc:Text]": FiftyOhmHtmlRenderer.render_photo_helper("123", "abc", "Text", "TODO"),
}
for assertion in assertions:
assert mistletoe.markdown(assertion, FiftyOhmHtmlRenderer) == paragraph(assertions[assertion])
@pytest.mark.latex
def test_photo_latex():
assertions = {
"[photo:123:abc:Text]": FiftyOhmLaTeXRenderer.render_photo_helper("123", "abc", "Text", "TODO"),
}
for assertion in assertions:
assert mistletoe.markdown(assertion, FiftyOhmLaTeXRenderer) == "\n" + assertions[assertion] + "\n"