aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mammoth/conversion.py27
-rw-r--r--tests/conversion_tests.py23
2 files changed, 48 insertions, 2 deletions
diff --git a/mammoth/conversion.py b/mammoth/conversion.py
index e5526d5..c1299f6 100644
--- a/mammoth/conversion.py
+++ b/mammoth/conversion.py
@@ -1,3 +1,5 @@
+# coding=utf-8
+
import base64
import random
@@ -45,7 +47,8 @@ class DocumentConverter(object):
documents.TableCell: self._convert_table_cell,
documents.LineBreak: self._line_break,
documents.Image: convert_image or images.inline(self._convert_image),
- documents.FootnoteReference: self._footnote_reference,
+ documents.FootnoteReference: self._convert_footnote_reference,
+ documents.Footnote: self._convert_footnote,
}
self._convert_underline = convert_underline
@@ -56,6 +59,14 @@ class DocumentConverter(object):
def _convert_document(self, document, html_generator):
self._convert_elements_to_html(document.children, html_generator)
+ html_generator.end_all()
+ html_generator.start("ol")
+ footnotes = [
+ document.footnotes.find_footnote_by_id(footnote_id)
+ for footnote_id in self._footnote_ids
+ ]
+ self._convert_elements_to_html(footnotes, html_generator)
+ html_generator.end()
def _convert_paragraph(self, paragraph, html_generator):
@@ -130,7 +141,7 @@ class DocumentConverter(object):
"src": "data:{0};base64,{1}".format(image.content_type, encoded_src)
}
- def _footnote_reference(self, footnote_reference, html_generator):
+ def _convert_footnote_reference(self, footnote_reference, html_generator):
uid = self._footnote_uid(footnote_reference.footnote_id)
html_generator.start("sup")
html_generator.start("a", {
@@ -142,6 +153,18 @@ class DocumentConverter(object):
html_generator.text("[{0}]".format(footnote_number))
html_generator.end()
html_generator.end()
+
+ def _convert_footnote(self, footnote, html_generator):
+ uid = self._footnote_uid(footnote.id)
+ html_generator.start("li", {"id": "footnote-{0}".format(uid)})
+ footnote_generator = HtmlGenerator()
+ self._convert_elements_to_html(footnote.body, footnote_generator)
+ footnote_generator.text(" ")
+ footnote_generator.start("a", {"href": "#footnote-ref-{0}".format(uid)})
+ footnote_generator.text(u"↑")
+ footnote_generator.end_all()
+ html_generator.append(footnote_generator)
+ html_generator.end()
def _convert_elements_to_html(self, elements, html_generator):
diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py
index e920551..508d457 100644
--- a/tests/conversion_tests.py
+++ b/tests/conversion_tests.py
@@ -1,3 +1,5 @@
+# coding=utf-8
+
import io
from nose.tools import istest, assert_equal
@@ -268,6 +270,27 @@ def footnote_reference_is_converted_to_superscript_intra_page_link():
assert_equal('<sup><a href="#footnote-42-4" id="footnote-ref-42-4">[1]</a></sup>', result.value)
+@istest
+def footnotes_are_included_after_the_main_body():
+ footnote_reference = documents.footnote_reference("4")
+ document = documents.document(
+ [documents.paragraph([
+ _run_with_text("Knock knock"),
+ documents.run([footnote_reference])
+ ])],
+ footnotes=documents.Footnotes({
+ "4": documents.Footnote("4", [_paragraph_with_text("Who's there?")])
+ })
+ )
+ result = convert_document_element_to_html(
+ document,
+ generate_uniquifier=lambda: 42
+ )
+ expected_html = (u'<p>Knock knock<sup><a href="#footnote-42-4" id="footnote-ref-42-4">[1]</a></sup></p>' +
+ u'<ol><li id="footnote-42-4"><p>Who\'s there? <a href="#footnote-ref-42-4">↑</a></p></li></ol>')
+ assert_equal(expected_html, result.value)
+
+
def _paragraph_with_text(text):
return documents.paragraph(children=[_run_with_text(text)])