From 43b9ff99c86a4ecda8927667176df7c5454dfaad Mon Sep 17 00:00:00 2001 From: Roland Meister Date: Mon, 11 Mar 2013 23:33:38 +0100 Subject: [PATCH] Add helper function footnote_spot that determins where to put new footnotes. --- sphinx/builders/epub.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index 3991ce13e..0d5ebe531 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -133,6 +133,8 @@ _link_target_template = u' [%(uri)s]' _footnote_label_template = u'#%d' +_footnotes_rubric_name = u'Footnotes' + _css_link_target_class = u'link-target' # XXX These strings should be localized according to epub_language @@ -308,12 +310,33 @@ class EpubBuilder(StandaloneHTMLBuilder): doc.note_autofootnote(footnote) return footnote + def footnote_spot(tree): + """Find or create a spot to place footnotes. + + The function returns the tuple (parent, index).""" + # The code uses the following heuristic: + # a) place them after the last existing footnote + # b) place them after an (empty) Footnotes rubric + # c) create an empty Footnotes rubric at the end of the document + fns = tree.traverse(nodes.footnote) + if fns: + fn = fns[-1] + return fn.parent, fn.parent.index(fn) + 1 + for node in tree.traverse(nodes.rubric): + if len(node.children) == 1 and \ + node.children[0].astext() == _footnotes_rubric_name: + return node.parent, node.parent.index(node) + 1 + doc = tree.traverse(nodes.document)[0] + rub = nodes.rubric() + rub.append(nodes.Text(_footnotes_rubric_name)) + doc.append(rub) + return doc, doc.index(rub) + 1 + if show_urls == 'no': return if show_urls == 'footnote': doc = tree.traverse(nodes.document)[0] - # XXX search place to put footnotes - bottom = doc + fn_spot, fn_idx = footnote_spot(tree) nr = 1 for node in tree.traverse(nodes.reference): uri = node.get('refuri', '') @@ -331,9 +354,10 @@ class EpubBuilder(StandaloneHTMLBuilder): footnote_ref = make_footnote_ref(doc, label) node.parent.insert(idx, footnote_ref) footnote = make_footnote(doc, label, uri) - bottom.append(footnote) + fn_spot.insert(fn_idx, footnote) footnote_ref['refid'] = footnote['ids'][0] footnote.add_backref(footnote_ref['ids'][0]) + fn_idx += 1 def write_doc(self, docname, doctree): """Write one document file.