refactor: combine reading and rendering

This commit is contained in:
Tim Grelka
2026-02-15 17:06:38 +01:00
parent b79874c154
commit 6ab442ba0c
+61 -43
View File
@@ -153,7 +153,7 @@ class Build:
except FileNotFoundError:
tqdm.write(f"\033[31mPhoto #{id} not found\033[0m")
def __build_chapter(self, edition, edition_name, number, chapter, next_chapter=None):
def __build_chapter_index(self, edition, edition_name, number, chapter, next_chapter=None):
chapter_template = self.env.get_template("html/chapter.html")
next_chapter_template = self.env.get_template("html/next_chapter.html")
with (self.config.p_build / f"{edition}_chapter_{chapter['ident']}.html").open("w") as file:
@@ -242,7 +242,9 @@ class Build:
result = self.__build_page(result, course_wrapper=True)
file.write(result)
def __build_chapter_slidedeck(self, edition, chapter, sections, next_chapter, chapter_number=None):
def __build_chapter_slidedeck(
self, edition, chapter, sections, next_chapter, chapter_number=None, progress: Progress = 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")
@@ -271,7 +273,10 @@ class Build:
result += "</section>\n"
section_counter = 0
for section in sections:
slides_task = progress.add_task("Rendering slides ...")
for section in progress.track(sections, task_id=slides_task):
progress.update(slides_task, description=f"Rendering slides of {section['title']}")
if section["slide"] is None:
continue
@@ -294,6 +299,8 @@ class Build:
tmp += renderer.render(doc)
result += f"<section>{tmp}</section>\n"
progress.remove_task(slides_task)
result += next_template.render(
edition=edition,
next_chapter=next_chapter,
@@ -333,17 +340,45 @@ class Build:
result = self.__build_page(result)
file.write(result)
def build_edition(self, edition):
def build_edition(self, edition: str):
self.config.p_build.mkdir(exist_ok=True)
edition = edition.upper()
with (self.config.p_data_toc / f"{edition}.json").open() as file:
with (
(self.config.p_data_toc / f"{edition}.json").open() as file,
Progress(
TaskProgressColumn(),
BarColumn(),
TimeRemainingColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress,
):
chapter_task = progress.add_task(f"Building edition {edition} ...")
book = json.load(file)
edition_name = book["title"]
# Create index pages for chapters and slides.
self.__build_book_index(book)
self.__build_slide_index(book)
chapters = book["chapters"]
for chapter in chapters:
for section in chapter["sections"]:
for chapter_number, chapter in enumerate(progress.track(chapters, task_id=chapter_task), 1):
progress.update(chapter_task, description=f"Building edition {edition}: Chapter {chapter['title']}")
# Determine next chapter for navigation (None if this is the last chapter)
next_chapter = chapters[chapter_number] if chapter_number < len(chapters) else None
self.__build_chapter_index(edition, edition_name, chapter_number, chapter, next_chapter)
# Open, parse and render each section.
section_task = progress.add_task(description="Rendering sections ...")
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']}")
# Read section and slide content from the corresponding files.
ident = section["ident"]
section["content"] = None
section["slide"] = None
@@ -354,44 +389,27 @@ class Build:
section_content = sfile.read()
section["slide"] = section_content
edition_name = book["title"]
self.__build_book_index(book)
self.__build_slide_index(book)
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,
)
with Progress(
TaskProgressColumn(),
BarColumn(),
TimeRemainingColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
chapter_task = progress.add_task(f"Building edition {edition} ...")
for chapter_number, chapter in enumerate(progress.track(chapters, task_id=chapter_task), 1):
progress.update(chapter_task, description=f"Building edition {edition}: Chapter {chapter['title']}")
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)
progress.remove_task(section_task)
# Build the slidedeck after all sections have been processed, as they require the files to be read.
self.__build_chapter_slidedeck(
edition, chapter, chapter["sections"], next_chapter, chapter_number, progress
)
section_task = progress.add_task(description="Rendering sections ...")
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"][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)
progress.remove_task(chapter_task)
def build_assets(self):
self.config.p_build.mkdir(exist_ok=True)