Merge pull request #19 from DARC-e-V/feature-alt-texts

Feature alt texts
This commit is contained in:
Tim Grelka
2026-01-09 20:55:53 +01:00
committed by GitHub
6 changed files with 58 additions and 18 deletions
+11 -8
View File
@@ -258,32 +258,35 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
return base.join(filter(lambda x: x is not None, [self.render(child) for child in token.children]))
@staticmethod
def render_picture_helper(id, ref, text, number):
def render_picture_helper(id, ref, text, number, alt_text):
return f"""
<figure class="picture" id="ref_{ref}" name="{ref}">
<img src="pictures/{id}.svg">
<img src="pictures/{id}.svg" alt="{alt_text}">
<figcaption>Abbildung {number}: {text}</figcaption>
</figure>
"""
def render_picture(self, token):
alt_text = ""
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)
alt_text = self.picture_handler(token.id)
return self.render_picture_helper(token.id, token.ref, token.text, token.number, alt_text)
@staticmethod
def render_photo_helper(id, ref, text, number):
def render_photo_helper(id, ref, text, number, alt_text):
return f"""
<figure class="photo" id="ref_{ref}" name="{ref}">
<img src="photos/{id}.jpg">
<img src="photos/{id}.jpg" alt="{alt_text}">
<figcaption>Abbildung {number}: {text}</figcaption>
</figure>
"""
def render_photo(self, token):
alt_text = ""
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)
alt_text = self.photo_handler(token.id) or alt_text
return self.render_photo_helper(token.id, token.ref, token.text, token.number, alt_text)
def render_table(self, token: Table):
# Add id and name attributes if table has a name
+25 -5
View File
@@ -80,10 +80,10 @@ class Build:
answers = []
if metadata["picture_a"] != "":
self.__picture_handler(metadata["picture_a"])
self.__picture_handler(metadata["picture_b"])
self.__picture_handler(metadata["picture_c"])
self.__picture_handler(metadata["picture_d"])
alt_text_a = self.__picture_handler(metadata["picture_a"])
alt_text_b = self.__picture_handler(metadata["picture_b"])
alt_text_c = self.__picture_handler(metadata["picture_c"])
alt_text_d = self.__picture_handler(metadata["picture_d"])
answer_pictures = [
metadata["picture_a"],
@@ -91,14 +91,24 @@ class Build:
metadata["picture_c"],
metadata["picture_d"],
]
alt_text_answers = [
alt_text_a,
alt_text_b,
alt_text_c,
alt_text_d,
]
else:
answer_pictures = []
alt_text_answers = []
if "picture_question" in question and metadata["picture_question"] != "":
picture_question = metadata["picture_question"]
self.__picture_handler(picture_question)
alt_text_question = self.__picture_handler(picture_question)
else:
picture_question = ""
alt_text_question = ""
return question_template.render(
question=question["question"],
@@ -107,6 +117,8 @@ class Build:
picture_question=picture_question,
answers=answers,
answer_pictures=answer_pictures,
alt_text_answers=alt_text_answers,
alt_text_question=alt_text_question,
)
def __build_question_slide(self, input):
@@ -122,6 +134,10 @@ class Build:
file = f"{id}.svg"
try:
shutil.copyfile(self.config.p_data_pictures / file, self.config.p_build_pictures / file)
if (self.config.p_data_pictures / f"{id}.txt").exists():
return (self.config.p_data_pictures / f"{id}.txt").read_text()
else:
return "Bildbeschreibung noch nicht verfügbar"
except FileNotFoundError:
tqdm.write(f"\033[31mPicture #{id} not found\033[0m")
@@ -130,6 +146,10 @@ class Build:
file = f"{id}.jpg"
try:
shutil.copyfile(self.config.p_data_photos / file, self.config.p_build_photos / file)
if (self.config.p_data_photos / f"{id}.txt").exists():
return (self.config.p_data_photos / f"{id}.txt").read_text()
else:
return "Bildbeschreibung noch nicht verfügbar"
except FileNotFoundError:
tqdm.write(f"\033[31mPhoto #{id} not found\033[0m")
+17
View File
@@ -92,10 +92,19 @@ class Download:
photos = self.api.get("items/Fotos", params={"limit": -1})
for photo in tqdm(photos, desc="Downloading photos"):
# Save Photo to file
data = self.api.get_file("assets/" + photo["photo"])
with (self.config.p_data_photos / f"{photo['id']}.jpg").open("wb") as file:
file.write(data)
# Save Alt-Text to file
prefix = ""
if photo["alt_text"] is not None:
if photo["alt_text_reviewed"] is None:
prefix = "Der folgende Alt-Text wurde noch nicht geprüft: "
with (self.config.p_data_photos / f"{photo['id']}.txt").open("w") as file:
file.write(prefix + photo["alt_text"])
def download_pictures(self):
self.config.p_data_pictures.mkdir(parents=True, exist_ok=True)
@@ -106,6 +115,14 @@ class Download:
with (self.config.p_data_pictures / f"{picture['id']}include.tex").open("w") as file:
file.write(picture["latex"])
# Save Alt-Text to file
prefix = ""
if picture["alt_text"] is not None:
if picture["alt_text_reviewed"] is None:
prefix = "Der folgende Alt-Text wurde noch nicht geprüft: "
with (self.config.p_data_pictures / f"{picture['id']}.txt").open("w") as file:
file.write(prefix + picture["alt_text"])
# Download SVG
data = self.api.get_file(f"assets/{picture['picture']}")
with (self.config.p_data_pictures / f"{picture['id']}.svg").open("wb") as file:
+2 -2
View File
@@ -1,7 +1,7 @@
<div class="question layout-{{ layout }}" id="{{ number }}">
<div class="question-text">{{ number }}: {{ question }}</div>
{% if picture_question %}
<div class="question-picture"><img src="pictures/{{ picture_question }}.svg"></div>
<div class="question-picture"><img src="pictures/{{ picture_question }}.svg" alt="{{ alt_text_question }}"></div>
{% endif %}
<div class="answers">
{% for i in range(4) %}
@@ -10,7 +10,7 @@
{{ answers[i] }}
{% endif %}
{% if answer_pictures[i] %}
<div class="question-picture"><img src="pictures/{{ answer_pictures[i] }}.svg"></div>
<div class="question-picture"><img src="pictures/{{ answer_pictures[i] }}.svg" alt="{{ alt_text_answers[i] }}"></div>
{% endif %}
{% if i == 0 %}
<div class="result material-icons">check</div>
+1 -1
View File
@@ -9,7 +9,7 @@ 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"),
"[photo:123:abc:Text]": FiftyOhmHtmlRenderer.render_photo_helper("123", "abc", "Text", "TODO", ""),
}
for assertion in assertions:
+2 -2
View File
@@ -9,7 +9,7 @@ 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"),
"[picture:0:abc:Text]": FiftyOhmHtmlRenderer.render_picture_helper("0", "abc", "Text", "TODO", ""),
}
for assertion in assertions:
@@ -19,7 +19,7 @@ def test_picture_html():
@pytest.mark.latex
def test_picture_latex():
assertions = {
"[picture:123:abc:Text]": FiftyOhmLaTeXRenderer.render_picture_helper("123", "abc", "Text", "TODO"),
"[picture:0:abc:Text]": FiftyOhmLaTeXRenderer.render_picture_helper("0", "abc", "Text", "TODO"),
}
for assertion in assertions: