Merge branch 'main' into feature/slides
This commit is contained in:
+89
-10
@@ -3,7 +3,6 @@ import os
|
||||
import random
|
||||
import shutil
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from joblib import Memory
|
||||
from mistletoe import Document
|
||||
@@ -98,9 +97,13 @@ class Build:
|
||||
return self.__build_question(input, template_file="slide/question.html")
|
||||
|
||||
# cached
|
||||
def __build_page(self, content) :
|
||||
def __build_page(self, content, course_wrapper=False, sidebar=None) :
|
||||
page_template = self.env.get_template("html/page.html")
|
||||
return page_template.render(content=content)
|
||||
return page_template.render(
|
||||
content=content,
|
||||
course_wrapper=course_wrapper,
|
||||
sidebar=sidebar
|
||||
)
|
||||
|
||||
def __picture_handler(self, id):
|
||||
os.makedirs("build/pictures", exist_ok=True)
|
||||
@@ -132,8 +135,7 @@ class Build:
|
||||
title=next_chapter["title"],
|
||||
)
|
||||
|
||||
result = self.__build_page(result)
|
||||
result = BeautifulSoup(result, "html.parser").prettify()
|
||||
result = self.__build_page(result, course_wrapper=True)
|
||||
file.write(result)
|
||||
|
||||
# cached
|
||||
@@ -169,8 +171,34 @@ class Build:
|
||||
title=next_chapter["title"],
|
||||
)
|
||||
|
||||
result = self.__build_page(result)
|
||||
result = BeautifulSoup(result, "html.parser").prettify()
|
||||
result = self.__build_page(result, course_wrapper=True)
|
||||
file.write(result)
|
||||
|
||||
def __build_chapter_slidedeck(self, edition, chapter, sections, next_chapter):
|
||||
with open(f'build/{edition}_slide_{chapter["ident"]}.html', 'w') as file:
|
||||
slide_template = self.env.get_template("slide/slide.html")
|
||||
help_template = self.env.get_template("slide/help.html")
|
||||
next_template = self.env.get_template("slide/next.html")
|
||||
with FiftyOhmHtmlSlideRenderer(
|
||||
question_renderer=self.__build_question_slide,
|
||||
picture_handler=self.__picture_handler,
|
||||
photo_handler=self.__photo_handler
|
||||
) as renderer:
|
||||
result = "<section>\n"
|
||||
result += f'<section data-background="#DAEEFA">\n<h1>{chapter["title"]}</h1>\n</section>\n'
|
||||
result += help_template.render()
|
||||
result += "</section>\n"
|
||||
for section in sections:
|
||||
tmp = f'<section data-background="#DAEEFA">\n<h1>{section["title"]}</h1>\n</section>\n'
|
||||
tmp += renderer.render_wrapper(section["slide"])
|
||||
result += f"<section>{tmp}</section>\n"
|
||||
result += next_template.render(
|
||||
edition=edition,
|
||||
next_chapter=next_chapter,
|
||||
chapter=chapter,
|
||||
)
|
||||
|
||||
result = slide_template.render(content=result)
|
||||
file.write(result)
|
||||
|
||||
def __build_chapter_slidedeck(self, edition, chapter, sections, next_chapter):
|
||||
@@ -219,7 +247,6 @@ class Build:
|
||||
book=book,
|
||||
)
|
||||
result = self.__build_page(result)
|
||||
result = BeautifulSoup(result, "html.parser").prettify()
|
||||
file.write(result)
|
||||
|
||||
def __build_slide_index(self, book):
|
||||
@@ -229,7 +256,6 @@ class Build:
|
||||
book=book,
|
||||
)
|
||||
result = self.__build_page(result)
|
||||
result = BeautifulSoup(result, "html.parser").prettify()
|
||||
file.write(result)
|
||||
|
||||
def build_edition(self, edition):
|
||||
@@ -251,4 +277,57 @@ class Build:
|
||||
self.__build_section(edition, edition_name, section, i, chapter, next_section, next_chapter)
|
||||
|
||||
def build_assets(self):
|
||||
shutil.copytree("assets", "build/assets", dirs_exist_ok=True)
|
||||
shutil.copytree("assets", "build/assets", dirs_exist_ok=True)
|
||||
|
||||
def __parse_snippets(self):
|
||||
with open("data/snippets.json") as f:
|
||||
snippets = json.load(f)
|
||||
|
||||
with FiftyOhmHtmlRenderer(self.__build_question, self.__picture_handler, self.__photo_handler) as renderer:
|
||||
for key, value in snippets.items():
|
||||
snippets[key] = renderer.render_inner(Document(value))
|
||||
# Remove leading <p> and trailing </p>:
|
||||
snippets[key] = snippets[key][3:-4]
|
||||
return snippets
|
||||
|
||||
def __parse_contents(self):
|
||||
with open("data/content.json") as f:
|
||||
contents = json.load(f)
|
||||
return contents
|
||||
|
||||
def __build_index(self, snippets):
|
||||
template = self.env.get_template("index.html")
|
||||
result = template.render({"snippets": snippets})
|
||||
|
||||
with open("build/index.html", "w") as file:
|
||||
result = self.__build_page(result)
|
||||
|
||||
file.write(result)
|
||||
|
||||
def __build_course_page(self, snippets, template, page):
|
||||
template = self.env.get_template(f"{template}.html")
|
||||
result=template.render({"snippets": snippets})
|
||||
|
||||
with open(f"build/{page}.html", "w") as file:
|
||||
result = self.__build_page(result)
|
||||
file.write(result)
|
||||
|
||||
def __build_html_page(self, contents, page):
|
||||
for content in contents:
|
||||
if content["url_part"] == page:
|
||||
with open(f"build/{page}.html", "w") as file:
|
||||
result = self.__build_page(
|
||||
content=content["content"],
|
||||
sidebar=content["sidebar"]
|
||||
)
|
||||
file.write(result)
|
||||
|
||||
def build_website(self):
|
||||
snippets = self.__parse_snippets()
|
||||
contents = self.__parse_contents()
|
||||
self.__build_index(snippets)
|
||||
self.__build_course_page(snippets, "kurse-karte", "kurse_vor_ort_karte")
|
||||
self.__build_course_page(snippets, "kurse-liste", "kurse_vor_ort_liste")
|
||||
self.__build_course_page(snippets, "patenkarte", "patenkarte")
|
||||
self.__build_html_page(contents, "pruefung")
|
||||
self.__build_html_page(contents, "infos")
|
||||
+4
-2
@@ -13,8 +13,10 @@ class Config:
|
||||
with open("config/config.json") as file:
|
||||
self.config = json.load(file)
|
||||
|
||||
self.access_token = self.get_config_value("directus_api_key")
|
||||
self.base_url = self.get_config_value("directus_api_url", "https://fragenkatalog.darc.de/")
|
||||
self.question_access_token = self.get_config_value("directus_question_api_key")
|
||||
self.question_base_url = self.get_config_value("directus_question_base_url", "https://fragenkatalog.darc.de/")
|
||||
self.content_access_token = self.get_config_value("directus_content_api_key")
|
||||
self.content_base_url = self.get_config_value("directus_content_base_url", "https://redaktion.50ohm.de/")
|
||||
self.no_latex = self.get_config_value("no_latex", False)
|
||||
|
||||
def get_config_value(self, key: str, default=None):
|
||||
|
||||
+23
-1
@@ -8,9 +8,10 @@ from tqdm import tqdm
|
||||
|
||||
|
||||
class Download:
|
||||
def __init__(self, api, config):
|
||||
def __init__(self, api, content_api, config):
|
||||
self.api = api
|
||||
self.config = config
|
||||
self.content_api = content_api
|
||||
|
||||
def download_edition(self, edition):
|
||||
in_edition = self.api.get_one("items/edition", {"filter": {"ident": {"_eq": edition}}})
|
||||
@@ -131,3 +132,24 @@ class Download:
|
||||
Parallel(n_jobs=multiprocessing.cpu_count())(
|
||||
delayed(build_picture)(picture) for picture in tqdm(pictures, desc="Build All Pictures")
|
||||
)
|
||||
|
||||
def download_snippets(self):
|
||||
snippets = {}
|
||||
for snippet in tqdm(self.content_api.get("items/snippet"), desc="Downloading snippets"):
|
||||
snippets[snippet["ident"]] = snippet["content"]
|
||||
with open("data/snippets.json", "w", encoding="utf-8") as file:
|
||||
json.dump(snippets, file, ensure_ascii=False, indent=4)
|
||||
|
||||
def download_content(self):
|
||||
contents = []
|
||||
for content in self.content_api.get("items/content"): #tqdm(self.content_api.get("items/content"), desc="Downloading content"):
|
||||
contents.append(
|
||||
{
|
||||
"url_part": content["url_part"],
|
||||
"content": content["content"],
|
||||
"sidebar": content["sidebar"],
|
||||
}
|
||||
)
|
||||
|
||||
with open("data/content.json", "w", encoding="utf-8") as file:
|
||||
json.dump(contents, file, ensure_ascii=False, indent=4)
|
||||
Reference in New Issue
Block a user