feature: extract question index

This commit is contained in:
Matthias Jung
2026-04-05 15:20:16 +02:00
parent 11b41f2de6
commit 191495d6ce
3 changed files with 26 additions and 0 deletions
+1
View File
@@ -14,3 +14,4 @@ bd.build_edition("EA")
bd.build_edition("NEA")
bd.build_assets()
bd.build_solutions()
bd.build_question_index()
+1
View File
@@ -41,6 +41,7 @@ def main(
# Copy assets to build folder
bd.build_assets()
bd.build_question_index()
if __name__ == "__main__":
+24
View File
@@ -2,6 +2,7 @@ import json
import random
import re
import shutil
from collections import defaultdict
from pathlib import Path
from jinja2 import Environment, FileSystemLoader
@@ -23,6 +24,9 @@ class Build:
self.env.filters["shuffle_answers"] = self.__filter_shuffle_answers
self.questions = self.__parse_katalog()
self.question_index = defaultdict(set)
self.question_token_pattern = re.compile(r"^\s*\[question:([\w\d]+)\]", re.MULTILINE)
def __parse_katalog(self):
with self.config.p_data_fragenkatalog.open() as file:
fragenkatalog = json.load(file)
@@ -389,6 +393,9 @@ class Build:
section_content = sfile.read()
section["slide"] = section_content
if section["content"] is not None:
self.__collect_question_links(edition, ident, section["content"])
next_section = (
chapter["sections"][section_number] if section_number < len(chapter["sections"]) else None
)
@@ -515,3 +522,20 @@ class Build:
page = solution_template.render(question=question, solution=solution, number=solution_file.stem)
page = self.__build_page(page, course_wrapper=False)
file.write(page)
def __collect_question_links(self, edition: str, section_ident: str, section_content: str):
section_filename = f"{edition}_{section_ident}.html"
for question_number in self.question_token_pattern.findall(section_content or ""):
self.question_index[question_number].add(f"{section_filename}#{question_number}")
def __serialize_question_index(self) -> dict[str, list[str]]:
serialized: dict[str, list[str]] = {}
for question, links in sorted(self.question_index.items()):
serialized[question] = sorted(links)
return serialized
def build_question_index(self):
with (self.config.p_build / "question_index.json").open("w", encoding="utf-8") as file:
json.dump(self.__serialize_question_index(), file, ensure_ascii=False, indent=2, sort_keys=True)
file.write("\n")