Merge branch 'main' into feature-alt-texts

This commit is contained in:
Tim Grelka
2026-01-09 20:54:20 +01:00
committed by GitHub
62 changed files with 22074 additions and 154 deletions
+19
View File
@@ -0,0 +1,19 @@
import pytest
from mistletoe import Document
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
@pytest.mark.html
def test_formula_html():
assertions = {
r"$a \cdot b$": "\n" + r"$$a \cdot b$$" + "\n\n",
r" $a \cdot b$ ": "\n" + r"$$a \cdot b$$" + "\n\n",
r"$ a \cdot b $": "\n" + r"$$ a \cdot b $$" + "\n\n",
r"$a \cdot b$" + "\n" + r"$a \cdot b$": "\n" + r"$$a \cdot b$$" + "\n\n\n" + r"$$a \cdot b$$" + "\n\n",
r"Lorem Ipsum" + "\n" + r"$a \cdot b$": "<p>Lorem Ipsum</p>\n\n" + r"$$a \cdot b$$" + "\n\n",
}
with FiftyOhmHtmlRenderer() as renderer:
for assertion in assertions:
assert renderer.render(Document(assertion)) == assertions[assertion]
+222
View File
@@ -0,0 +1,222 @@
import mistletoe
import pytest
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
from renderer.fifty_ohm_html_slide_renderer import FiftyOhmHtmlSlideRenderer
@pytest.mark.html
def test_hierarchical_picture_numbering():
"""Test that pictures get hierarchical numbers in format edition-chapter.section.counter"""
markdown_text = """
[picture:123:pic1:First picture]
Some text here.
[picture:456:pic2:Second picture]
Reference to first: [ref:pic1]
Reference to second: [ref:pic2]
"""
renderer = FiftyOhmHtmlRenderer(edition="E", chapter="2", section="3")
doc = mistletoe.Document(markdown_text)
# First pass: collect figures
renderer.collect_figures(doc)
# Verify figure map
assert renderer.figure_map["pic1"] == "E-2.3.1"
assert renderer.figure_map["pic2"] == "E-2.3.2"
# Second pass: render
result = renderer.render(doc)
# Verify hierarchical numbers in output
assert "Abbildung E-2.3.1:" in result
assert "Abbildung E-2.3.2:" in result
# Verify references are resolved
assert ">E-2.3.1</a>" in result
assert ">E-2.3.2</a>" in result
@pytest.mark.html
def test_hierarchical_photo_numbering():
"""Test that photos get hierarchical numbers"""
markdown_text = """
[photo:789:photo1:First photo]
[photo:101:photo2:Second photo]
Reference: [ref:photo1]
"""
renderer = FiftyOhmHtmlRenderer(edition="N", chapter="1", section="2")
doc = mistletoe.Document(markdown_text)
renderer.collect_figures(doc)
assert renderer.figure_map["photo1"] == "N-1.2.1"
assert renderer.figure_map["photo2"] == "N-1.2.2"
result = renderer.render(doc)
assert "Abbildung N-1.2.1:" in result
assert "Abbildung N-1.2.2:" in result
assert ">N-1.2.1</a>" in result
@pytest.mark.html
def test_hierarchical_table_numbering():
"""Test that tables with names get hierarchical numbers"""
markdown_text = """
| Header 1 | Header 2 |
| Data 1 | Data 2 |
[table:table1:First table]
| Col A | Col B |
| Val 1 | Val 2 |
[table:table2:Second table]
Reference: [ref:table1]
"""
renderer = FiftyOhmHtmlRenderer(edition="A", chapter="3", section="1")
doc = mistletoe.Document(markdown_text)
renderer.collect_figures(doc)
assert renderer.figure_map["table1"] == "A-3.1.1"
assert renderer.figure_map["table2"] == "A-3.1.2"
result = renderer.render(doc)
assert "Tabelle A-3.1.1:" in result
assert "Tabelle A-3.1.2:" in result
assert ">A-3.1.1</a>" in result
@pytest.mark.html
def test_mixed_figure_types_share_counter():
"""Test that pictures, photos, and tables share a single unified counter"""
markdown_text = """
[picture:1:pic1:Picture one]
[photo:2:photo1:Photo one]
| A | B |
| 1 | 2 |
[table:tab1:Table one]
[picture:3:pic2:Picture two]
References: [ref:pic1], [ref:photo1], [ref:tab1], [ref:pic2]
"""
renderer = FiftyOhmHtmlRenderer(edition="E", chapter="5", section="2")
doc = mistletoe.Document(markdown_text)
renderer.collect_figures(doc)
# All types share the same counter
assert renderer.figure_map["pic1"] == "E-5.2.1"
assert renderer.figure_map["photo1"] == "E-5.2.2"
assert renderer.figure_map["tab1"] == "E-5.2.3"
assert renderer.figure_map["pic2"] == "E-5.2.4"
result = renderer.render(doc)
# Verify all references are resolved with correct numbers
assert "Abbildung E-5.2.1:" in result # pic1
assert "Abbildung E-5.2.2:" in result # photo1
assert "Tabelle E-5.2.3:" in result # tab1
assert "Abbildung E-5.2.4:" in result # pic2
@pytest.mark.slide
def test_slide_renderer_adds_s_suffix():
"""Test that slide renderer adds 'S' suffix to edition"""
markdown_text = """
[picture:1:pic1:Slide picture]
Reference: [ref:pic1]
"""
renderer = FiftyOhmHtmlSlideRenderer(edition="E", chapter="2", section="1")
doc = mistletoe.Document(markdown_text)
# Verify edition has S suffix
assert renderer.edition == "ES"
renderer.collect_figures(doc)
# Verify hierarchical number includes S
assert renderer.figure_map["pic1"] == "ES-2.1.1"
result = renderer.render(doc)
assert "Abbildung ES-2.1.1:" in result
assert ">ES-2.1.1</a>" in result
@pytest.mark.html
def test_fallback_numbering_without_context():
"""Test that numbering falls back to simple counter when context is missing"""
markdown_text = """
[picture:1:pic1:Picture]
Reference: [ref:pic1]
"""
# No edition/chapter/section provided
renderer = FiftyOhmHtmlRenderer()
doc = mistletoe.Document(markdown_text)
renderer.collect_figures(doc)
# Should fall back to simple counter
assert renderer.figure_map["pic1"] == "1"
result = renderer.render(doc)
assert "Abbildung 1:" in result
assert ">1</a>" in result
@pytest.mark.html
def test_unresolved_reference():
"""Test that references to non-existent figures show '?'"""
markdown_text = """
Reference to non-existent: [ref:nonexistent]
"""
renderer = FiftyOhmHtmlRenderer(edition="E", chapter="1", section="1")
doc = mistletoe.Document(markdown_text)
renderer.collect_figures(doc)
result = renderer.render(doc)
# Should show ? for unresolved reference
assert ">?</a>" in result
@pytest.mark.html
def test_table_without_name_not_numbered():
"""Test that tables without names are not numbered"""
markdown_text = """
| Header 1 | Header 2 |
| Data 1 | Data 2 |
[picture:1:pic1:Picture one]
"""
renderer = FiftyOhmHtmlRenderer(edition="E", chapter="1", section="1")
doc = mistletoe.Document(markdown_text)
renderer.collect_figures(doc)
# Only the picture should be numbered
assert renderer.figure_map["pic1"] == "E-1.1.1"
assert renderer.figure_counter == 1
+1 -1
View File
@@ -33,7 +33,7 @@ questions = parse_katalog()
def question_stub(input):
"""Combines the original question dataset from BNetzA with our internal metadata"""
with open("data/metadata.json") as metadata_file:
with open("data/metadata3b.json") as metadata_file:
metadata = json.load(metadata_file)
number = metadata[f"{input}"]["number"] # Fragennummer z.B. AB123
+1 -1
View File
@@ -45,7 +45,7 @@ def filter_shuffle_answers(seq):
def question_stub(input):
"""Combines the original question dataset from BNetzA with our internal metadata"""
with open("data/metadata.json") as metadata_file:
with open("data/metadata3b.json") as metadata_file:
metadata = json.load(metadata_file)
number = metadata[f"{input}"]["number"] # Fragennummer z.B. AB123
+1 -1
View File
@@ -36,7 +36,7 @@ def test_question_translation_html():
}
def test_function(input):
with open("data/metadata.json") as file:
with open("data/metadata3b.json") as file:
metadata = json.load(file)
return metadata[f"{input}"].get("number")
+4 -5
View File
@@ -7,12 +7,11 @@ from test.util import paragraph
@pytest.mark.html
def test_references_html():
# Right now this tests only the default values.
# The correct behavior can be tested when the information is added to the renderer class
# References without figures in the map should show "?"
assertions = {
"[ref:e_oszilloskop_bildschirmfoto_sinus]": '<a href="section.html#ref_e_oszilloskop_bildschirmfoto_sinus" onclick="highlightRef(\'e_oszilloskop_bildschirmfoto_sinus\');">0</a>', # noqa: E501
"[ref:n_rst_r]": '<a href="section.html#ref_n_rst_r" onclick="highlightRef(\'n_rst_r\');">0</a>',
"[ref:Uuuuuh]": '<a href="section.html#ref_Uuuuuh" onclick="highlightRef(\'Uuuuuh\');">0</a>', # Test the default parameters # noqa: E501
"[ref:e_oszilloskop_bildschirmfoto_sinus]": '<a href="section.html#ref_e_oszilloskop_bildschirmfoto_sinus" onclick="highlightRef(\'e_oszilloskop_bildschirmfoto_sinus\');">?</a>', # noqa: E501
"[ref:n_rst_r]": '<a href="section.html#ref_n_rst_r" onclick="highlightRef(\'n_rst_r\');">?</a>',
"[ref:Uuuuuh]": '<a href="section.html#ref_Uuuuuh" onclick="highlightRef(\'Uuuuuh\');">?</a>',
}
for key, value in assertions.items():
+11 -11
View File
@@ -8,17 +8,17 @@ from renderer.fifty_ohm_latex_renderer import FiftyOhmLaTeXRenderer
@pytest.mark.html
def test_table_html():
assertions = {
"| l: a |": '<table>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n</table>\n',
"| r: a |": '<table>\n<tr>\n<th style="text-align: right;">a</th>\n</tr>\n</table>\n',
"| c: a |": '<table>\n<tr>\n<th style="text-align: center;">a</th>\n</tr>\n</table>\n',
"| l: a |\n| b |": '<table>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n<tr>\n<td style="text-align: left;">b</td>\n</tr>\n</table>\n', # noqa: E501
"| l: a |\n| b |\n| c |": '<table>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n<tr>\n<td style="text-align: left;">b</td>\n</tr>\n<tr>\n<td style="text-align: left;">c</td>\n</tr>\n</table>\n', # noqa: E501
"| a |\n| b |\n| c |": "<table>\n<tr>\n<th>a</th>\n</tr>\n<tr>\n<td>b</td>\n</tr>\n<tr>\n<td>c</td>\n</tr>\n</table>\n", # noqa: E501
"| l: a |\n| *b* |": '<table>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n<tr>\n<td style="text-align: left;"><em>b</em></td>\n</tr>\n</table>\n', # noqa: E501
"| l: a |\n| b |\n[table:n_ab:A und B]": '<table>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n<tr>\n<td style="text-align: left;">b</td>\n</tr>\n<caption>A und B</caption></table>\n', # noqa: E501
"| l: *a* |": '<table>\n<tr>\n<th style="text-align: left;"><em>a</em></th>\n</tr>\n</table>\n',
"| l: a |\n| [morse:a] |": '<table>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n<tr>\n<td style="text-align: left;"><span class="morse"><span class="morse_char">\n<span class="morse_char">\n▄</span>\n<span class="morse_char">\n▄▄▄</span>\n</span>\n</span></td>\n</tr>\n</table>\n', # noqa: E501
"| l: abc | r: abc | X: abc |\n| 123 | 456 | 789 | \n | 987 | 654 | 321 | ": '<table>\n<tr>\n<th style="text-align: left;">abc</th>\n<th style="text-align: right;">abc</th>\n<th>abc</th>\n</tr>\n<tr>\n<td style="text-align: left;">123</td>\n<td style="text-align: right;">456</td>\n<td>789</td>\n</tr>\n<tr>\n<td style="text-align: left;">987</td>\n<td style="text-align: right;">654</td>\n<td>321</td>\n</tr>\n</table>\n', # noqa: E501
"| l: a |": '<table class="table table-hover">\n<thead>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n</thead>\n</table>\n', # noqa: E501
"| r: a |": '<table class="table table-hover">\n<thead>\n<tr>\n<th style="text-align: right;">a</th>\n</tr>\n</thead>\n</table>\n', # noqa: E501
"| c: a |": '<table class="table table-hover">\n<thead>\n<tr>\n<th style="text-align: center;">a</th>\n</tr>\n</thead>\n</table>\n', # noqa: E501
"| l: a |\n| b |": '<table class="table table-hover">\n<thead>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style="text-align: left;">b</td>\n</tr>\n</tbody>\n</table>\n', # noqa: E501
"| l: a |\n| b |\n| c |": '<table class="table table-hover">\n<thead>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style="text-align: left;">b</td>\n</tr>\n<tr>\n<td style="text-align: left;">c</td>\n</tr>\n</tbody>\n</table>\n', # noqa: E501
"| a |\n| b |\n| c |": '<table class="table table-hover">\n<thead>\n<tr>\n<th>a</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>b</td>\n</tr>\n<tr>\n<td>c</td>\n</tr>\n</tbody>\n</table>\n', # noqa: E501
"| l: a |\n| *b* |": '<table class="table table-hover">\n<thead>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style="text-align: left;"><em>b</em></td>\n</tr>\n</tbody>\n</table>\n', # noqa: E501
"| l: a |\n| b |\n[table:n_ab:A und B]": '<table class="table table-hover" id="ref_n_ab" name="n_ab">\n<thead>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style="text-align: left;">b</td>\n</tr>\n</tbody>\n<caption>A und B</caption>\n</table>\n', # noqa: E501
"| l: *a* |": '<table class="table table-hover">\n<thead>\n<tr>\n<th style="text-align: left;"><em>a</em></th>\n</tr>\n</thead>\n</table>\n', # noqa: E501
"| l: a |\n| [morse:a] |": '<table class="table table-hover">\n<thead>\n<tr>\n<th style="text-align: left;">a</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style="text-align: left;"><span class="morse"><span class="morse_char">\n<span class="morse_symbol">\n▄</span>\n<span class="morse_symbol">\n▄▄▄</span>\n</span>\n</span></td>\n</tr>\n</tbody>\n</table>\n', # noqa: E501
"| l: abc | r: abc | X: abc |\n| 123 | 456 | 789 | \n | 987 | 654 | 321 | ": '<table class="table table-hover">\n<thead>\n<tr>\n<th style="text-align: left;">abc</th>\n<th style="text-align: right;">abc</th>\n<th>abc</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style="text-align: left;">123</td>\n<td style="text-align: right;">456</td>\n<td>789</td>\n</tr>\n<tr>\n<td style="text-align: left;">987</td>\n<td style="text-align: right;">654</td>\n<td>321</td>\n</tr>\n</tbody>\n</table>\n', # noqa: E501
}
with FiftyOhmHtmlRenderer() as renderer: