Merge branch 'main' into feature/slides
This commit is contained in:
@@ -16,6 +16,7 @@ dl.download_edition("N")
|
|||||||
dl.download_question_metadata()
|
dl.download_question_metadata()
|
||||||
dl.download_pictures()
|
dl.download_pictures()
|
||||||
dl.download_photos()
|
dl.download_photos()
|
||||||
|
dl.download_includes()
|
||||||
dl.download_snippets()
|
dl.download_snippets()
|
||||||
dl.download_content()
|
dl.download_content()
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from mistletoe import Document, HtmlRenderer
|
|||||||
from .comment import BlockComment
|
from .comment import BlockComment
|
||||||
from .dash import Dash
|
from .dash import Dash
|
||||||
from .halfwidth_spaces import HalfwidthSpaces
|
from .halfwidth_spaces import HalfwidthSpaces
|
||||||
|
from .include import Include
|
||||||
from .morse import Morse
|
from .morse import Morse
|
||||||
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
from .nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
||||||
from .photo import Photo
|
from .photo import Photo
|
||||||
@@ -30,7 +31,14 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
section_url = "section.html"
|
section_url = "section.html"
|
||||||
ref_id = 0
|
ref_id = 0
|
||||||
|
|
||||||
def __init__(self, *extras, question_renderer=None, picture_handler=None, photo_handler=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
*extras,
|
||||||
|
question_renderer=None,
|
||||||
|
picture_handler=None,
|
||||||
|
photo_handler=None,
|
||||||
|
include_handler=None
|
||||||
|
):
|
||||||
|
|
||||||
final_extras = chain(extras,(
|
final_extras = chain(extras,(
|
||||||
Dash,
|
Dash,
|
||||||
@@ -49,6 +57,7 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
Photo,
|
Photo,
|
||||||
Table,
|
Table,
|
||||||
Qso,
|
Qso,
|
||||||
|
Include,
|
||||||
))
|
))
|
||||||
|
|
||||||
super().__init__(*final_extras)
|
super().__init__(*final_extras)
|
||||||
@@ -56,6 +65,7 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
self.question_renderer = question_renderer
|
self.question_renderer = question_renderer
|
||||||
self.picture_handler = picture_handler
|
self.picture_handler = picture_handler
|
||||||
self.photo_handler = photo_handler
|
self.photo_handler = photo_handler
|
||||||
|
self.include_handler = include_handler
|
||||||
|
|
||||||
def render_dash(self, token):
|
def render_dash(self, token):
|
||||||
return " – "
|
return " – "
|
||||||
@@ -233,3 +243,6 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
table += "</table>"
|
table += "</table>"
|
||||||
|
|
||||||
return table
|
return table
|
||||||
|
|
||||||
|
def render_include(self, token):
|
||||||
|
return self.include_handler(token.ident)
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from mistletoe.block_token import BlockToken
|
||||||
|
|
||||||
|
|
||||||
|
class Include(BlockToken):
|
||||||
|
"""
|
||||||
|
Block token ("[include:ident]").
|
||||||
|
Includes raw html or javascript code from a file with the given identifier.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def start(line):
|
||||||
|
return re.match(r"^\s*\[include:\s*[^\]]+\]", line)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def read(cls, lines):
|
||||||
|
re.compile(r'')
|
||||||
|
line = next(lines) # Consume the line with the include directive
|
||||||
|
ident = re.match(r"^\s*\[include:\s*([^\]]+)\]", line).group(1)
|
||||||
|
return ident
|
||||||
|
|
||||||
|
def __init__(self, match):
|
||||||
|
self.ident = match
|
||||||
+11
-30
@@ -138,6 +138,11 @@ class Build:
|
|||||||
result = self.__build_page(result, course_wrapper=True)
|
result = self.__build_page(result, course_wrapper=True)
|
||||||
file.write(result)
|
file.write(result)
|
||||||
|
|
||||||
|
def __include_handler(self, include):
|
||||||
|
with open("data/includes.json") as file:
|
||||||
|
includes = json.load(file)
|
||||||
|
return includes.get(include)
|
||||||
|
|
||||||
# cached
|
# cached
|
||||||
def __build_section(self, edition, edition_name, section, section_id, chapter, next_section=None, next_chapter=None):
|
def __build_section(self, edition, edition_name, section, section_id, chapter, next_section=None, next_chapter=None):
|
||||||
section_template = self.env.get_template("html/section.html")
|
section_template = self.env.get_template("html/section.html")
|
||||||
@@ -148,7 +153,8 @@ class Build:
|
|||||||
with FiftyOhmHtmlRenderer(
|
with FiftyOhmHtmlRenderer(
|
||||||
question_renderer=self.__build_question,
|
question_renderer=self.__build_question,
|
||||||
picture_handler=self.__picture_handler,
|
picture_handler=self.__picture_handler,
|
||||||
photo_handler=self.__photo_handler
|
photo_handler=self.__photo_handler,
|
||||||
|
include_handler=self.__include_handler
|
||||||
) as renderer:
|
) as renderer:
|
||||||
section["content"] = renderer.render(Document(section["content"]))
|
section["content"] = renderer.render(Document(section["content"]))
|
||||||
|
|
||||||
@@ -182,34 +188,8 @@ class Build:
|
|||||||
with FiftyOhmHtmlSlideRenderer(
|
with FiftyOhmHtmlSlideRenderer(
|
||||||
question_renderer=self.__build_question_slide,
|
question_renderer=self.__build_question_slide,
|
||||||
picture_handler=self.__picture_handler,
|
picture_handler=self.__picture_handler,
|
||||||
photo_handler=self.__photo_handler
|
photo_handler=self.__photo_handler,
|
||||||
) as renderer:
|
include_handler=self.__include_handler
|
||||||
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):
|
|
||||||
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:
|
) as renderer:
|
||||||
result = "<section>\n"
|
result = "<section>\n"
|
||||||
result += f'<section data-background="#DAEEFA">\n<h1>{chapter["title"]}</h1>\n</section>\n'
|
result += f'<section data-background="#DAEEFA">\n<h1>{chapter["title"]}</h1>\n</section>\n'
|
||||||
@@ -286,7 +266,8 @@ class Build:
|
|||||||
with FiftyOhmHtmlRenderer(
|
with FiftyOhmHtmlRenderer(
|
||||||
question_renderer=self.__build_question,
|
question_renderer=self.__build_question,
|
||||||
picture_handler=self.__picture_handler,
|
picture_handler=self.__picture_handler,
|
||||||
photo_handler=self.__photo_handler
|
photo_handler=self.__photo_handler,
|
||||||
|
include_handler=self.__include_handler
|
||||||
) as renderer:
|
) as renderer:
|
||||||
for key, value in snippets.items():
|
for key, value in snippets.items():
|
||||||
snippets[key] = renderer.render_inner(Document(value))
|
snippets[key] = renderer.render_inner(Document(value))
|
||||||
|
|||||||
@@ -133,6 +133,16 @@ class Download:
|
|||||||
delayed(build_picture)(picture) for picture in tqdm(pictures, desc="Build All Pictures")
|
delayed(build_picture)(picture) for picture in tqdm(pictures, desc="Build All Pictures")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def download_includes(self):
|
||||||
|
includes = self.api.get("items/include", params={"limit": -1})
|
||||||
|
|
||||||
|
result = {}
|
||||||
|
for include in tqdm(includes, desc="Downloading includes"):
|
||||||
|
result[include["ident"]] = include["content"]
|
||||||
|
|
||||||
|
with open("data/includes.json", "w") as f:
|
||||||
|
json.dump(result, f, indent=4)
|
||||||
|
|
||||||
def download_snippets(self):
|
def download_snippets(self):
|
||||||
snippets = {}
|
snippets = {}
|
||||||
for snippet in tqdm(self.content_api.get("items/snippet"), desc="Downloading snippets"):
|
for snippet in tqdm(self.content_api.get("items/snippet"), desc="Downloading snippets"):
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
% Kommentar: DARC-DOWN-TEST
|
% Kommentar: DARC-DOWN-TEST
|
||||||
|
|
||||||
|
[include:test]
|
||||||
|
|
||||||
Das ist eine Item-Liste:
|
Das ist eine Item-Liste:
|
||||||
|
|
||||||
* Erster Eintrag
|
* Erster Eintrag
|
||||||
|
|||||||
+8
-1
@@ -69,12 +69,19 @@ def question_stub(input):
|
|||||||
answer_pictures=answer_pictures,
|
answer_pictures=answer_pictures,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def include_stub(include):
|
||||||
|
with open("data/includes.json") as file:
|
||||||
|
includes = json.load(file)
|
||||||
|
return includes.get(include)
|
||||||
|
|
||||||
def test_html(capsys):
|
def test_html(capsys):
|
||||||
with capsys.disabled():
|
with capsys.disabled():
|
||||||
with open("test/acceptanceTest.md") as file:
|
with open("test/acceptanceTest.md") as file:
|
||||||
content = file.read()
|
content = file.read()
|
||||||
with FiftyOhmHtmlRenderer(question_renderer=question_stub) as renderer:
|
with FiftyOhmHtmlRenderer(
|
||||||
|
question_renderer=question_stub,
|
||||||
|
include_handler=include_stub,
|
||||||
|
) as renderer:
|
||||||
output = renderer.render(Document(content))
|
output = renderer.render(Document(content))
|
||||||
with open("test/acceptanceTest.html", "w") as output_file:
|
with open("test/acceptanceTest.html", "w") as output_file:
|
||||||
pretty = BeautifulSoup(output, "html.parser").prettify()
|
pretty = BeautifulSoup(output, "html.parser").prettify()
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import pytest
|
||||||
|
from mistletoe import Document
|
||||||
|
|
||||||
|
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.html
|
||||||
|
def test_include_html():
|
||||||
|
assertions = {
|
||||||
|
'[include:ident]': "ident\n",
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_function(input):
|
||||||
|
return f"{input}"
|
||||||
|
|
||||||
|
with FiftyOhmHtmlRenderer(include_handler=test_function) as renderer:
|
||||||
|
for assertion in assertions:
|
||||||
|
assert renderer.render(Document(assertion)) == assertions[assertion]
|
||||||
Reference in New Issue
Block a user