diff --git a/build.py b/build.py index 7646086..bfb2ae0 100644 --- a/build.py +++ b/build.py @@ -14,4 +14,5 @@ bd.build_edition("EA") bd.build_edition("NEA") bd.build_assets() bd.build_solutions() +bd.build_question_index() bd.build_zip() diff --git a/main.py b/main.py index ef5b04b..5b83092 100644 --- a/main.py +++ b/main.py @@ -42,6 +42,7 @@ def main( # Copy assets to build folder bd.build_assets() + bd.build_question_index() if build_zip: # Build zip file of output diff --git a/src/build.py b/src/build.py index 64f71d4..b3edc00 100644 --- a/src/build.py +++ b/src/build.py @@ -25,6 +25,9 @@ class Build: self.env.filters["shuffle_answers"] = self.__filter_shuffle_answers self.questions = self.__parse_katalog() + self.question_index = {} + 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) @@ -391,6 +394,15 @@ class Build: section_content = sfile.read() section["slide"] = section_content + if section["content"] is not None: + self.__collect_question_occurrences( + edition, + chapter["title"], + ident, + section["title"], + section["content"], + ) + next_section = ( chapter["sections"][section_number] if section_number < len(chapter["sections"]) else None ) @@ -497,6 +509,7 @@ class Build: self.__build_course_page(snippets, "patenkarte", "patenkarte") self.__build_html_page(contents, "pruefung") self.__build_html_page(contents, "infos") + self.__build_html_page(contents, "suche") def build_solutions(self): for solution_file in self.config.p_data_solutions.glob("*.md"): @@ -518,6 +531,40 @@ class Build: page = self.__build_page(page, course_wrapper=False) file.write(page) + def __collect_question_occurrences( + self, + edition: str, + chapter_title: str, + section_ident: str, + section_title: str, + section_content: str, + ): + for question_number in self.question_token_pattern.findall(section_content or ""): + question_entry = self.question_index.setdefault( + question_number, + { + "has_solution": self.__has_solution(question_number), + "section": section_ident, + "chapter_title": chapter_title, + "section_title": section_title, + "editions": [], + }, + ) + if edition not in question_entry["editions"]: + question_entry["editions"].append(edition) + + def __has_solution(self, question_number: str) -> bool: + return (self.config.p_data_solutions / f"{question_number}.md").exists() + + def build_question_index(self): + for question_data in self.question_index.values(): + question_data["editions"] = sorted(question_data["editions"]) + + path = self.config.p_build_assets / "question_index.json" + with (path).open("w", encoding="utf-8") as file: + json.dump(self.question_index, file, ensure_ascii=False, indent=2, sort_keys=True) + file.write("\n") + def build_zip(self, zip_name: str | None = None) -> Path: """Create a zip archive of the complete build output directory. diff --git a/templates/html/page.html b/templates/html/page.html index 0f9c786..d05dc54 100644 --- a/templates/html/page.html +++ b/templates/html/page.html @@ -66,6 +66,12 @@ Lernen +