feature: alt-text for photos

This commit is contained in:
Matthias Jung
2025-11-09 17:48:47 +01:00
parent f00f0481a9
commit ee5197000e
4 changed files with 19 additions and 5 deletions
+5 -4
View File
@@ -206,18 +206,19 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
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 = "Alt-Text noch nicht verfügbar"
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)
@staticmethod
def render_cell_helper(cell, alignment, type):
+2
View File
@@ -144,6 +144,8 @@ 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()
except FileNotFoundError:
tqdm.write(f"\033[31mPhoto #{id} not found\033[0m")
+9
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)
+3 -1
View File
@@ -9,7 +9,9 @@ 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", "Alt-Text noch nicht verfügbar"
),
}
for assertion in assertions: