feature: add page rendering with next and previous
This commit is contained in:
+56
-7
@@ -1,4 +1,6 @@
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
@@ -19,6 +21,7 @@ class Build:
|
||||
self.__build_question = memory.cache(self.__build_question)
|
||||
self.__build_chapter = memory.cache(self.__build_chapter)
|
||||
self.__build_section = memory.cache(self.__build_section)
|
||||
self.__build_page = memory.cache(self.__build_page)
|
||||
|
||||
def __parse_katalog(self):
|
||||
with open("data/fragenkatalog3b.json") as fragenkatalog_file:
|
||||
@@ -57,6 +60,11 @@ class Build:
|
||||
answers = []
|
||||
|
||||
if metadata["picture_a"] != "":
|
||||
self.__picture_handler(metadata["picture_a"])
|
||||
self.__picture_handler(metadata["picture_b"])
|
||||
self.__picture_handler(metadata["picture_c"])
|
||||
self.__picture_handler(metadata["picture_d"])
|
||||
|
||||
answer_pictures = [
|
||||
metadata["picture_a"],
|
||||
metadata["picture_b"],
|
||||
@@ -68,6 +76,7 @@ class Build:
|
||||
|
||||
if "picture_question" in question:
|
||||
picture_question = metadata["picture_question"]
|
||||
self.__picture_handler(picture_question)
|
||||
else:
|
||||
picture_question = ""
|
||||
|
||||
@@ -81,22 +90,50 @@ class Build:
|
||||
)
|
||||
|
||||
# cached
|
||||
def __build_chapter(self, edition, number, chapter):
|
||||
def __build_page(self, content) :
|
||||
page_template = self.env.get_template("page.html")
|
||||
return page_template.render(content=content)
|
||||
|
||||
def __picture_handler(self, id):
|
||||
os.makedirs("build/pictures", exist_ok=True)
|
||||
src = f"data/pictures/{id}.svg"
|
||||
dst = f"build/pictures/{id}.svg"
|
||||
shutil.copyfile(src, dst)
|
||||
|
||||
def __photo_handler(self, id):
|
||||
os.makedirs("build/photos", exist_ok=True)
|
||||
src = f"data/photos/{id}.jpg"
|
||||
dst = f"build/photos/{id}.jpg"
|
||||
shutil.copyfile(src, dst)
|
||||
|
||||
# cached
|
||||
def __build_chapter(self, edition, 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,
|
||||
number=number,
|
||||
chapter=chapter,
|
||||
)
|
||||
|
||||
if next_chapter is not None:
|
||||
result += next_chapter_template.render(
|
||||
url=f"{edition}_chapter_{next_chapter["ident"]}.html",
|
||||
title=next_chapter["title"],
|
||||
)
|
||||
|
||||
result = self.__build_page(result)
|
||||
result = BeautifulSoup(result, "html.parser").prettify()
|
||||
file.write(result)
|
||||
|
||||
# cached
|
||||
def __build_section(self, edition, section, chapter):
|
||||
def __build_section(self, edition, section, chapter, next_section=None):
|
||||
section_template = self.env.get_template("section.html")
|
||||
next_section_template = self.env.get_template("next_section.html")
|
||||
with open(f'build/{edition}_{section["ident"]}.html', 'w') as file:
|
||||
|
||||
with FiftyOhmHtmlRenderer(self.__build_question) as renderer:
|
||||
with FiftyOhmHtmlRenderer(self.__build_question, self.__picture_handler, self.__photo_handler) as renderer:
|
||||
section["content"] = renderer.render(Document(section["content"]))
|
||||
|
||||
result = section_template.render(
|
||||
@@ -104,8 +141,15 @@ class Build:
|
||||
section=section,
|
||||
chapter=chapter,
|
||||
)
|
||||
result = BeautifulSoup(result, "html.parser").prettify()
|
||||
|
||||
if next_section is not None:
|
||||
result += next_section_template.render(
|
||||
url=f"{edition}_{next_section["ident"]}.html",
|
||||
title=next_section["title"],
|
||||
)
|
||||
|
||||
result = self.__build_page(result)
|
||||
result = BeautifulSoup(result, "html.parser").prettify()
|
||||
file.write(result)
|
||||
|
||||
def build_edition(self, edition):
|
||||
@@ -115,7 +159,12 @@ class Build:
|
||||
book = json.load(f)
|
||||
|
||||
for number, chapter in enumerate(tqdm(book, desc=f"Build Edition: {edition}"),1):
|
||||
self.__build_chapter(edition, number, chapter)
|
||||
next_chapter = book[number] if number < len(book) else None
|
||||
self.__build_chapter(edition, number, chapter, next_chapter)
|
||||
|
||||
for section in chapter["sections"] :
|
||||
self.__build_section(edition, section, chapter)
|
||||
for number, section in enumerate(chapter["sections"],1) :
|
||||
next_section = chapter["sections"][number] if number < len(chapter["sections"]) else None
|
||||
self.__build_section(edition, section, chapter, next_section)
|
||||
|
||||
def build_assets(self):
|
||||
shutil.copytree("assets", "build/assets", dirs_exist_ok=True)
|
||||
Reference in New Issue
Block a user