diff --git a/renderer/fifty_ohm_html_renderer.py b/renderer/fifty_ohm_html_renderer.py index b382db6..2a54114 100644 --- a/renderer/fifty_ohm_html_renderer.py +++ b/renderer/fifty_ohm_html_renderer.py @@ -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"""
- + {alt_text}
Abbildung {number}: {text}
""" 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"""
- + {alt_text}
Abbildung {number}: {text}
""" 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 diff --git a/src/build.py b/src/build.py index 5cac294..4b2dc6e 100644 --- a/src/build.py +++ b/src/build.py @@ -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") diff --git a/src/download.py b/src/download.py index 4c9d586..fd443f6 100644 --- a/src/download.py +++ b/src/download.py @@ -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: diff --git a/templates/html/question.html b/templates/html/question.html index 68c4923..a95851c 100644 --- a/templates/html/question.html +++ b/templates/html/question.html @@ -1,7 +1,7 @@
{{ number }}: {{ question }}
{% if picture_question %} -
+
{{ alt_text_question }}
{% endif %}
{% for i in range(4) %} @@ -10,7 +10,7 @@ {{ answers[i] }} {% endif %} {% if answer_pictures[i] %} -
+
{{ alt_text_answers[i] }}
{% endif %} {% if i == 0 %}
check
diff --git a/test/test_photo.py b/test/test_photo.py index 6e0c042..7e6d27b 100644 --- a/test/test_photo.py +++ b/test/test_photo.py @@ -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: diff --git a/test/test_picture.py b/test/test_picture.py index 23aef01..d9bb240 100644 --- a/test/test_picture.py +++ b/test/test_picture.py @@ -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: