Merge branch 'main' into feat/cli

This commit is contained in:
Tim Grelka
2026-01-18 18:35:51 +01:00
65 changed files with 21991 additions and 85 deletions
+105 -18
View File
@@ -54,7 +54,7 @@ class Build:
question_template = self.env.get_template(template_file)
with (self.config.p_data / "metadata.json").open() as file:
with (self.config.p_data / "metadata3b.json").open() as file:
metadata = json.load(file)
question = None
@@ -81,10 +81,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"],
@@ -92,14 +92,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"],
@@ -108,6 +118,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):
@@ -123,6 +135,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")
@@ -131,6 +147,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")
@@ -162,19 +182,43 @@ class Build:
# cached
def __build_section(
self, edition, edition_name, section, section_id, chapter, next_section=None, next_chapter=None
self,
edition,
edition_name,
section,
section_id,
chapter,
next_section=None,
next_chapter=None,
chapter_number=None,
):
section_template = self.env.get_template("html/section.html")
next_section_template = self.env.get_template("html/next_section.html")
next_chapter_template = self.env.get_template("html/next_chapter.html")
with (self.config.p_build / f"{edition}_{section['ident']}.html").open("w") as file:
# Use provided chapter_number or fall back to chapter dict
chapter_num = str(chapter_number) if chapter_number is not None else chapter.get("number", "0")
section_num = str(section_id)
# Set the section URL for references
section_filename = f"{edition}_{section['ident']}.html"
with FiftyOhmHtmlRenderer(
question_renderer=self.__build_question,
picture_handler=self.__picture_handler,
photo_handler=self.__photo_handler,
include_handler=self.__include_handler,
edition=edition,
chapter=chapter_num,
section=section_num,
section_url=section_filename,
) as renderer:
section["content"] = renderer.render(Document(section["content"]))
# First pass: collect all figures and assign hierarchical numbers
doc = Document(section["content"])
renderer.collect_figures(doc)
# Second pass: render with hierarchical numbers
section["content"] = renderer.render(doc)
result = section_template.render(
edition=edition,
@@ -198,30 +242,58 @@ class Build:
result = self.__build_page(result, course_wrapper=True)
file.write(result)
def __build_chapter_slidedeck(self, edition, chapter, sections, next_chapter):
def __build_chapter_slidedeck(self, edition, chapter, sections, next_chapter, chapter_number=None):
with (self.config.p_build / f"{edition}_slide_{chapter['ident']}.html").open("w") as file:
slide_template = self.env.get_template("slide/slide.html")
help_template = self.env.get_template("slide/help.html")
next_template = self.env.get_template("slide/next.html")
# Use provided chapter_number or fall back to chapter dict
chapter_num = str(chapter_number) if chapter_number is not None else chapter.get("number", "0")
# Set the slide URL for references (all sections in one file)
# Note: filename uses original edition (e.g., "N"), not modified edition with "S" (e.g., "NS")
slide_filename = f"{edition}_slide_{chapter['ident']}.html"
with FiftyOhmHtmlSlideRenderer(
question_renderer=self.__build_question_slide,
picture_handler=self.__picture_handler,
photo_handler=self.__photo_handler,
include_handler=self.__include_handler,
edition=edition,
chapter=chapter_num,
section="0", # Will be updated per section
section_url=slide_filename,
) as renderer:
result = "<section>\n"
result += f'<section data-background="#DAEEFA">\n<h1>{chapter["title"]}</h1>\n</section>\n'
result += help_template.render()
result += "</section>\n"
section_counter = 0
for section in sections:
if section["slide"] is None:
continue
section_counter += 1
# Update section number for this section
renderer.section = str(section_counter)
# Reset figure counter for each section
renderer.figure_counter = 0
renderer.figure_map = {}
if not section["slide"].startswith("---"):
section["slide"] = "---\n" + section["slide"]
# First pass: collect figures
doc = Document(section["slide"])
renderer.collect_figures(doc)
# Second pass: render with hierarchical numbers
tmp = f'<section data-background="#DAEEFA">\n<h1>{section["title"]}</h1>\n</section>\n'
tmp += renderer.render(Document(section["slide"]))
tmp += renderer.render(doc)
result += f"<section>{tmp}</section>\n"
result += next_template.render(
edition=edition,
next_chapter=next_chapter,
@@ -281,23 +353,38 @@ class Build:
transient=True,
) as progress:
chapter_task = progress.add_task(f"Building edition {edition} ...")
for number, chapter in enumerate(progress.track(chapters, task_id=chapter_task), 1):
for chapter_number, chapter in enumerate(progress.track(chapters, task_id=chapter_task), 1):
progress.update(chapter_task, description=f"Buildign edition {edition}: Chapter {chapter['title']}")
next_chapter = chapters[number] if number < len(chapters) else None
self.__build_chapter(edition, edition_name, number, chapter, next_chapter)
self.__build_chapter_slidedeck(edition, chapter, chapter["sections"], next_chapter)
next_chapter = chapters[chapter_number] if chapter_number < len(chapters) else None
self.__build_chapter(edition, edition_name, chapter_number, chapter, next_chapter)
self.__build_chapter_slidedeck(edition, chapter, chapter["sections"], next_chapter, chapter_number)
section_task = progress.add_task(description="Rendering sections ...")
for i, section in enumerate(progress.track(chapter["sections"], task_id=section_task), 1):
for section_number, section in enumerate(
progress.track(chapter["sections"], task_id=section_task), 1
):
progress.update(section_task, description=f"Rendering section {section['title']}")
next_section = chapter["sections"][i] if i < len(chapter["sections"]) else None
self.__build_section(edition, edition_name, section, i, chapter, next_section, next_chapter)
next_section = (
chapter["sections"][section_number] if section_number < len(chapter["sections"]) else None
)
self.__build_section(
edition,
edition_name,
section,
section_number,
chapter,
next_section,
next_chapter,
chapter_number,
)
progress.remove_task(section_task)
progress.remove_task(chapter_task)
def build_assets(self):
self.config.p_build.mkdir(exist_ok=True)
shutil.copytree(self.config.p_assets, self.config.p_build_assets, dirs_exist_ok=True)
shutil.copytree(
self.config.p_assets, self.config.p_build_assets, dirs_exist_ok=True, ignore=shutil.ignore_patterns(".git")
)
def __parse_snippets(self):
with (self.config.p_data / "snippets.json").open() as file:
+2
View File
@@ -10,6 +10,8 @@ class Config:
if os.path.isfile("config/config.json"):
with open("config/config.json") as file:
self.config = json.load(file)
else:
self.config = {}
self.question_access_token = self.get_config_value("directus_question_api_key")
self.question_base_url = self.get_config_value("directus_question_base_url", "https://fragenkatalog.darc.de/")
+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: