refactor: put margin in jina template

This commit is contained in:
Prof. Dr. Matthias Jung DL9MJ
2025-06-24 22:04:10 +02:00
committed by Tim Grelka
parent e323954495
commit ea5144cebd
2 changed files with 38 additions and 25 deletions
+19 -25
View File
@@ -1,5 +1,6 @@
import textwrap
from jinja2 import Environment, FileSystemLoader
from mistletoe import Document, HtmlRenderer from mistletoe import Document, HtmlRenderer
from .comment import BlockComment from .comment import BlockComment
@@ -26,7 +27,7 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
section_url = "section.html" section_url = "section.html"
ref_id = 0 ref_id = 0
def __init__(self, question_renderer=None): def __init__(self, question_renderer=None, picture_handler=None, photo_handler=None):
super().__init__( super().__init__(
Dash, Dash,
BlockComment, BlockComment,
@@ -45,6 +46,8 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
Table, Table,
) )
self.question_renderer = question_renderer self.question_renderer = question_renderer
self.picture_handler = picture_handler
self.photo_handler = photo_handler
def render_dash(self, token): def render_dash(self, token):
return " – " return " – "
@@ -114,27 +117,14 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
def render_tag_helper(type, content, margin_id, margin_anchor_id): def render_tag_helper(type, content, margin_id, margin_anchor_id):
"""This function is used to render the different types of tags. It is """This function is used to render the different types of tags. It is
used in the HtmlRenderer class and also in the test class""" used in the HtmlRenderer class and also in the test class"""
return textwrap.dedent(f"""\ env = Environment(loader=FileSystemLoader("templates/html"))
<div class="margin {type}" id="margin_for_{margin_id}"> margin_template = env.get_template("margin.html")
{content} return margin_template.render(
</div> type=type,
<a id="margin_orig_{margin_id}"></a> content=content,
<script> id=margin_id,
(() => {{ margin_anchor_id=margin_anchor_id,
function moveFunc() {{ )
const targetEl = document.getElementById(
window.innerWidth > 800 ? "margin_{margin_anchor_id}" : "margin_orig_{margin_id}"
);
targetEl.parentNode.insertBefore(
document.getElementById("margin_for_{margin_id}"),
targetEl
);
}}
addEventListener("resize", moveFunc);
moveFunc();
}})();
</script>
""")
def render_tag(self, token): def render_tag(self, token):
if token.tagtype == "latexonly": if token.tagtype == "latexonly":
@@ -181,24 +171,28 @@ 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="picture/picture_{id}.svg"> <img src="pictures/{id}.svg">
<figcaption>Abbildung {number}: {text}</figcaption> <figcaption>Abbildung {number}: {text}</figcaption>
</figure> </figure>
""" """
def render_picture(self, token): def render_picture(self, token):
if self.picture_handler is not None:
self.picture_handler(token.id)
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 @staticmethod
def render_photo_helper(id, ref, text, number): def render_photo_helper(id, ref, text, number):
return f""" return f"""
<figure class="photo" id="ref_{ref}" name="{ref}"> <figure class="photo" id="ref_{ref}" name="{ref}">
<img src="photo/photo_{id}.png"> <img src="photos/{id}.jpg">
<figcaption>Abbildung {number}: {text}</figcaption> <figcaption>Abbildung {number}: {text}</figcaption>
</figure> </figure>
""" """
def render_photo(self, token): def render_photo(self, token):
if self.photo_handler is not None:
self.photo_handler(token.id)
return self.render_photo_helper(token.id, token.ref, token.text, token.number) return self.render_photo_helper(token.id, token.ref, token.text, token.number)
@staticmethod @staticmethod
+19
View File
@@ -0,0 +1,19 @@
<div class="margin {{ type }}" id="margin_for_{{ id }}">
{{ content }}
</div>
<a id="margin_orig_{{ id }}"></a>
<script>
(() => {
function moveFunc() {
const targetEl = document.getElementById(
window.innerWidth > 800 ? "margin_{{ margin_anchor_id }}" : "margin_orig_{{ id }}"
);
targetEl.parentNode.insertBefore(
document.getElementById("margin_for_{{ id }}"),
targetEl
);
}
addEventListener("resize", moveFunc);
moveFunc();
})();
</script>