diff --git a/src/build.py b/src/build.py index 176bc6b..f5aa932 100644 --- a/src/build.py +++ b/src/build.py @@ -107,12 +107,13 @@ class Build: shutil.copyfile(src, dst) # cached - def __build_chapter(self, edition, number, chapter, next_chapter=None): + def __build_chapter(self, edition, edition_name, number, chapter, next_chapter=None): chapter_template = self.env.get_template("chapter.html") next_chapter_template = self.env.get_template("next_chapter.html") with open(f'build/{edition}_chapter_{chapter["ident"]}.html', 'w') as file: result = chapter_template.render( edition=edition, + name=edition_name, number=number, chapter=chapter, ) @@ -128,7 +129,7 @@ class Build: file.write(result) # cached - def __build_section(self, edition, section, chapter, next_section=None, next_chapter=None): + def __build_section(self, edition, edition_name, section, chapter, next_section=None, next_chapter=None): section_template = self.env.get_template("section.html") next_section_template = self.env.get_template("next_section.html") next_chapter_template = self.env.get_template("next_chapter.html") @@ -139,6 +140,7 @@ class Build: result = section_template.render( edition=edition, + name=edition_name, section=section, chapter=chapter, ) @@ -158,19 +160,31 @@ class Build: result = BeautifulSoup(result, "html.parser").prettify() file.write(result) + def __build_book_index(self, book): + template = self.env.get_template("course_index.html") + with open(f"build/{book['edition']}_course_index.html", "w") as file: + result = template.render( + book=book, + ) + result = self.__build_page(result) + result = BeautifulSoup(result, "html.parser").prettify() + file.write(result) + def build_edition(self, edition): edition = edition.upper() with open(f'data/book_{edition}.json') as f: book = json.load(f) - - for number, chapter in enumerate(tqdm(book, desc=f"Build Edition: {edition}"),1): - next_chapter = book[number] if number < len(book) else None - self.__build_chapter(edition, number, chapter, next_chapter) + chapters = book["chapters"] + edition_name = book["title"] + self.__build_book_index(book) + for number, chapter in enumerate(tqdm(chapters, desc=f"Build Edition: {edition}"),1): + next_chapter = chapters[number] if number < len(chapters) else None + self.__build_chapter(edition, edition_name, number, chapter, next_chapter) for i, section in enumerate(chapter["sections"],1) : next_section = chapter["sections"][i] if i < len(chapter["sections"]) else None - self.__build_section(edition, section, chapter, next_section, next_chapter) + self.__build_section(edition, edition_name, section, chapter, next_section, next_chapter) def build_assets(self): shutil.copytree("assets", "build/assets", dirs_exist_ok=True) \ No newline at end of file diff --git a/src/download.py b/src/download.py index 39aaa11..72f37b4 100644 --- a/src/download.py +++ b/src/download.py @@ -47,13 +47,20 @@ class Download: "class": in_section["class"], "content": in_section["content"], "slide": in_section["slide"], - "video": in_section["video_url"], + "video_url": in_section["video_url"], "questions": [], } ) + out_edition = { + "title": in_edition["title"], + "abstract": in_edition["abstract"], + "chapters": out_chapters, + "edition": edition, + } + with open("data/book_" + edition + ".json", "w", encoding="utf-8") as file: - json.dump(out_chapters, file, ensure_ascii=False, indent=4) + json.dump(out_edition, file, ensure_ascii=False, indent=4) def download_question_metadata(self): questions = self.api.get( diff --git a/templates/html/chapter.html b/templates/html/chapter.html index 4cb0218..969f08f 100644 --- a/templates/html/chapter.html +++ b/templates/html/chapter.html @@ -1,6 +1,6 @@
{{ book["abstract"] }}
+