feature: first build process

This commit is contained in:
Prof. Dr. Matthias Jung DL9MJ
2025-06-24 22:04:10 +02:00
committed by Tim Grelka
parent 9ed1866d15
commit b8edfa2364
4 changed files with 176 additions and 2 deletions
+8 -2
View File
@@ -1,11 +1,13 @@
import api.config as config
import api.directus as directus
import api.download as download
import src.build as build
import src.config as config
import src.download as download
conf = config.Config()
api = directus.DirectusAPI(conf)
dl = download.Download(api)
bd = build.Build()
# Download the text files:
dl.download_edition("N")
@@ -14,3 +16,7 @@ dl.download_edition("N")
dl.download_question_metadata()
dl.download_photos()
dl.download_pictures()
# Build edition:
bd.build_edition("N")
+121
View File
@@ -0,0 +1,121 @@
import json
from bs4 import BeautifulSoup
from jinja2 import Environment, FileSystemLoader
from joblib import Memory
from mistletoe import Document
from tqdm import tqdm
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
class Build:
def __init__(self):
memory = Memory("./cache", verbose=0)
self.env = Environment(loader=FileSystemLoader("templates/html"))
self.questions = self.__parse_katalog()
# Decorate the method with memory.cache
self.__build_question = memory.cache(self.__build_question)
self.__build_chapter = memory.cache(self.__build_chapter)
self.__build_section = memory.cache(self.__build_section)
def __parse_katalog(self):
with open("data/fragenkatalog3b.json") as fragenkatalog_file:
fragenkatalog = json.load(fragenkatalog_file)
questions = {}
for exampart in fragenkatalog["sections"]:
for chapter in exampart["sections"]:
if "questions" in chapter:
for question in chapter["questions"]:
questions[question["number"]] = question
if "sections" in chapter:
for section in chapter["sections"]:
for question in section["questions"]:
questions[question["number"]] = question
return questions
# cached
def __build_question(self, input):
"""Combines the original question dataset from BNetzA with our internal metadata"""
question_template = self.env.get_template("question.html")
with open("data/metadata.json") as metadata_file:
metadata = json.load(metadata_file)
number = metadata[f"{input}"]["number"] # Fragennummer z.B. AB123
question = self.questions[number]
metadata = metadata[f"{input}"]
if "answer_a" in question:
answers = [question["answer_a"], question["answer_b"], question["answer_c"], question["answer_d"]]
else:
answers = []
if metadata["picture_a"] != "":
answer_pictures = [
metadata["picture_a"],
metadata["picture_b"],
metadata["picture_c"],
metadata["picture_d"],
]
else:
answer_pictures = []
if "picture_question" in question:
picture_question = metadata["picture_question"]
else:
picture_question = ""
return question_template.render(
question=question["question"],
number=number,
layout=metadata["layout"],
picture_question=picture_question,
answers=answers,
answer_pictures=answer_pictures,
)
# cached
def __build_chapter(self, edition, number, chapter):
chapter_template = self.env.get_template("chapter.html")
with open(f'build/{edition}_chapter_{chapter["ident"]}.html', 'w') as file:
result = chapter_template.render(
edition=edition,
number=number,
chapter=chapter,
)
file.write(result)
# cached
def __build_section(self, edition, section, chapter):
section_template = self.env.get_template("section.html")
with open(f'build/{edition}_{section["ident"]}.html', 'w') as file:
with FiftyOhmHtmlRenderer(self.__build_question) as renderer:
section["content"] = renderer.render(Document(section["content"]))
result = section_template.render(
edition=edition,
section=section,
chapter=chapter,
)
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):
self.__build_chapter(edition, number, chapter)
for section in chapter["sections"] :
self.__build_section(edition, section, chapter)
+26
View File
@@ -0,0 +1,26 @@
<div style="float: right;">
<div class="btn-group" role="group">
{% if chapter.video_url %}
<a class="btn btn-secondary" href="{{ chapter["video_url"] }}" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"></path><path d="M21 3H3c-1.11 0-2 .89-2 2v12c0 1.1.89 2 2 2h5v2h8v-2h5c1.1 0 1.99-.9 1.99-2L23 5c0-1.11-.9-2-2-2zm0 14H3V5h18v12zm-5-6l-7 4V7z"></path></svg></a>
{% endif %}
<a class="btn btn-secondary" href="{{ edition }}_slide_{{ chapter["ident"] }}.html"><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M840-120v-640H120v320H40v-320q0-33 23.5-56.5T120-840h720q33 0 56.5 23.5T920-760v560q0 33-23.5 56.5T840-120ZM360-400q-66 0-113-47t-47-113q0-66 47-113t113-47q66 0 113 47t47 113q0 66-47 113t-113 47Zm0-80q33 0 56.5-23.5T440-560q0-33-23.5-56.5T360-640q-33 0-56.5 23.5T280-560q0 33 23.5 56.5T360-480ZM40-80v-112q0-34 17.5-62.5T104-298q62-31 126-46.5T360-360q66 0 130 15.5T616-298q29 15 46.5 43.5T680-192v112H40Zm80-80h480v-32q0-11-5.5-20T580-226q-54-27-109-40.5T360-280q-56 0-111 13.5T140-226q-9 5-14.5 14t-5.5 20v32Zm240-400Zm0 400Z"/></svg></a>
</div>
</div>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.html">Home</a></li>
<li class="breadcrumb-item"><a href="{{ edition }}_course_index.html">Online-Kurs</a></li>
<li class="breadcrumb-item active" aria-current="page">{{ chapter["title"] }}</li>
</ol>
</nav>
<h1>{{ chapter["title"] }}</h1>
<p>{{ chapter["abstract"] }}</p>
<div class="chapter-toc d-grid gap-2">
{% for section in chapter["sections"] %}
<a class="btn btn-outline-primary btn-labeled" href="{{ edition }}_{{ section["ident"] }}.html">
<span class="btn-label">{{ number }}.{{loop.index}}</span>{{ section["title"]}}
</a>
{% endfor %}
</div>
+21
View File
@@ -0,0 +1,21 @@
<div style="float: right;">
<div class="btn-group" role="group">
{% if section.video_url %}
<a class="btn btn-secondary" href="{{ section["video_url"] }}" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"></path><path d="M21 3H3c-1.11 0-2 .89-2 2v12c0 1.1.89 2 2 2h5v2h8v-2h5c1.1 0 1.99-.9 1.99-2L23 5c0-1.11-.9-2-2-2zm0 14H3V5h18v12zm-5-6l-7 4V7z"></path></svg></a>
{% endif %}
<a class="btn btn-secondary" href="{{ edition }}_slide_{{ chapter["ident"] }}.html#{{ section_id }}"><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M840-120v-640H120v320H40v-320q0-33 23.5-56.5T120-840h720q33 0 56.5 23.5T920-760v560q0 33-23.5 56.5T840-120ZM360-400q-66 0-113-47t-47-113q0-66 47-113t113-47q66 0 113 47t47 113q0 66-47 113t-113 47Zm0-80q33 0 56.5-23.5T440-560q0-33-23.5-56.5T360-640q-33 0-56.5 23.5T280-560q0 33 23.5 56.5T360-480ZM40-80v-112q0-34 17.5-62.5T104-298q62-31 126-46.5T360-360q66 0 130 15.5T616-298q29 15 46.5 43.5T680-192v112H40Zm80-80h480v-32q0-11-5.5-20T580-226q-54-27-109-40.5T360-280q-56 0-111 13.5T140-226q-9 5-14.5 14t-5.5 20v32Zm240-400Zm0 400Z"/></svg></a>
</div>
</div>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.html">Home</a></li>
<li class="breadcrumb-item"><a href="{{ edition }}_course_index.html">Online-Kurs</a></li>
<li class="breadcrumb-item"><a href="{{ edition }}_chapter_{{ chapter["ident"] }}.html">{{ chapter["title"] }}</a></li>
<li class="breadcrumb-item active" aria-current="page">{{ section["title"] }}</li>
</ol>
</nav>
<h1>{{ section["title"] }} (Klasse {{ section["class"] }})</h1>
{{ section["content"] }}