epub2md/tests/test_footnotes.py

93 lines
3.9 KiB
Python
Raw Normal View History

import re
from conftest import Book, Doc
CH1 = (
"<h1>Kapitel</h1>"
'<p>Erste Aussage<a epub:type="noteref" href="#fn1" id="r1">1</a> und nochmal<a epub:type="noteref" href="#fn1" id="r1b">1</a>.</p>'
'<p>Zweite<sup><a role="doc-noteref" href="notes.xhtml#en1" id="r2">2</a></sup>.</p>'
'<aside epub:type="footnote" id="fn1"><p><a href="#r1">1</a> Erster Absatz mit <em>Betonung</em> und '
'<a href="https://example.org">Link</a>.</p><p>Zweiter Absatz mit <code>code()</code>.</p></aside>'
)
NOTES = (
"<h1>Anmerkungen</h1>"
'<aside epub:type="endnote" id="en1"><p><a href="ch.xhtml#r2">2</a> Endnote mit Formel '
'<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>e</mi><mi>x</mi></msup></math>.</p></aside>'
)
def book() -> Book:
return Book(docs=[Doc("ch.xhtml", CH1), Doc("notes.xhtml", NOTES)])
def test_end_of_chapter(convert):
run = convert(book())
text = run.chapter("001")
body, _, defs = text.partition("[^fn-001]:")
assert "Erste Aussage[^fn-001] und nochmal[^fn-001]." in body
assert "Zweite[^fn-002]." in body
assert "Erster Absatz" not in body # not duplicated as running text
assert "Erster Absatz mit *Betonung* und [Link](https://example.org).\n\n Zweiter Absatz mit `code()`." in defs
assert "[^fn-002]: Endnote mit Formel $e^{x}$." in text
assert text.count("[^fn-001]:") == 1
assert run.report["footnotes"]["rendered"] == 2
def test_end_of_book(convert):
run = convert(book(), "--footnotes", "end-of-book")
text = run.chapter("001")
assert '<a id="fnref-001"></a><sup>[1](999-anmerkungen.md#fn-001)</sup>' in text
assert '<a id="fnref-001-2"></a>' in text
notes = run.chapter("999")
assert '<a id="fn-001"></a>**1.** Erster Absatz' in notes
assert "[↩](001-kapitel.md#fnref-001)" in notes and "[↩](001-kapitel.md#fnref-001-2)" in notes
assert run.report["output_link_check"]["broken_count"] == 0
def test_inline(convert):
text = convert(book(), "--footnotes", "inline").chapter("001")
assert "Zweite (Anmerkung: Endnote mit Formel $e^{x}$.)." in text
assert "Erste Aussage (Anmerkung: Erster Absatz mit *Betonung* und [Link](https://example.org). Zweiter Absatz" in text
assert "[^fn" not in text
def test_inline_falls_back_for_block_content(convert):
body = ('<h1>K</h1><p>Text<a epub:type="noteref" href="#n">1</a>.</p>'
'<aside epub:type="footnote" id="n"><pre>code\n eingerückt</pre></aside>')
text = convert(Book(docs=[Doc("c.xhtml", body)]), "--footnotes", "inline").chapter("001")
assert "Text[^fn-001]." in text
assert "[^fn-001]: ```" in text
def test_keep_links(convert):
run = convert(book(), "--footnotes", "keep-links")
text = run.chapter("001")
assert "[1](#fn1)" in text
assert '<a id="fn1"></a>' in text
assert "Erster Absatz" in text
assert run.report["output_link_check"]["broken_count"] == 0
def test_class_based_footnotes_with_backlinks(convert):
body = ('<h1>K</h1><p>Text<sup><a href="c.xhtml#n1"><span id="r1">1</span></a></sup>.</p><hr/>'
'<p class="Footnote"><a href="c.xhtml#r1"><span id="n1">1</span></a> Die Fußnote.</p>')
text = convert(Book(docs=[Doc("c.xhtml", body)])).chapter("001")
assert "Text[^fn-001]." in text
assert re.search(r"^\[\^fn-001\]: Die Fußnote\.$", text, re.M)
def test_unresolvable_noteref_is_warned(convert):
body = '<h1>K</h1><p>Text<a epub:type="noteref" href="#fehlt">9</a></p>'
run = convert(Book(docs=[Doc("c.xhtml", body)]))
codes = [w["code"] for w in run.report["warnings"]]
assert "noteref-unresolved" in codes
def test_commonmark_footnotes_use_anchor_links(convert):
run = convert(book(), "--format", "commonmark")
text = run.chapter("001")
assert "[^fn" not in text
assert '<sup><a href="#fn-001">1</a></sup>' in text
assert '<a id="fn-001"></a>**1:** Erster Absatz' in text
assert "[↩](#fnref-001)" in text
assert run.report["output_link_check"]["broken_count"] == 0