refactor: changed question_index layout
This commit is contained in:
+14
-10
@@ -24,7 +24,7 @@ class Build:
|
|||||||
self.env.filters["shuffle_answers"] = self.__filter_shuffle_answers
|
self.env.filters["shuffle_answers"] = self.__filter_shuffle_answers
|
||||||
self.questions = self.__parse_katalog()
|
self.questions = self.__parse_katalog()
|
||||||
|
|
||||||
self.question_index = defaultdict(set)
|
self.question_index = defaultdict(lambda: defaultdict(set))
|
||||||
self.question_token_pattern = re.compile(r"^\s*\[question:([\w\d]+)\]", re.MULTILINE)
|
self.question_token_pattern = re.compile(r"^\s*\[question:([\w\d]+)\]", re.MULTILINE)
|
||||||
|
|
||||||
def __parse_katalog(self):
|
def __parse_katalog(self):
|
||||||
@@ -394,7 +394,7 @@ class Build:
|
|||||||
section["slide"] = section_content
|
section["slide"] = section_content
|
||||||
|
|
||||||
if section["content"] is not None:
|
if section["content"] is not None:
|
||||||
self.__collect_question_links(edition, ident, section["content"])
|
self.__collect_question_occurrences(edition, ident, section["content"])
|
||||||
|
|
||||||
next_section = (
|
next_section = (
|
||||||
chapter["sections"][section_number] if section_number < len(chapter["sections"]) else None
|
chapter["sections"][section_number] if section_number < len(chapter["sections"]) else None
|
||||||
@@ -523,16 +523,20 @@ class Build:
|
|||||||
page = self.__build_page(page, course_wrapper=False)
|
page = self.__build_page(page, course_wrapper=False)
|
||||||
file.write(page)
|
file.write(page)
|
||||||
|
|
||||||
def __collect_question_links(self, edition: str, section_ident: str, section_content: str):
|
def __collect_question_occurrences(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 ""):
|
for question_number in self.question_token_pattern.findall(section_content or ""):
|
||||||
self.question_index[question_number].add(f"{section_filename}#{question_number}")
|
self.question_index[question_number][section_ident].add(edition)
|
||||||
|
|
||||||
def __serialize_question_index(self) -> dict[str, list[str]]:
|
def __serialize_question_index(self) -> dict[str, list[dict[str, list[str]]]]:
|
||||||
serialized: dict[str, list[str]] = {}
|
serialized: dict[str, list[dict[str, list[str]]]] = {}
|
||||||
for question, links in sorted(self.question_index.items()):
|
for question, sections in sorted(self.question_index.items()):
|
||||||
serialized[question] = sorted(links)
|
serialized[question] = [
|
||||||
|
{
|
||||||
|
"section": section,
|
||||||
|
"editions": sorted(editions),
|
||||||
|
}
|
||||||
|
for section, editions in sorted(sections.items())
|
||||||
|
]
|
||||||
return serialized
|
return serialized
|
||||||
|
|
||||||
def build_question_index(self):
|
def build_question_index(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user