From a620abf2888a1a1a7cd90a0d6e12bdb98273f480 Mon Sep 17 00:00:00 2001 From: Klaus Thoden Date: Fri, 9 Feb 2018 16:00:42 +0100 Subject: [PATCH] Double footnotes --- eoaconvert.py | 11 +++++++++-- tralics2django.py | 42 +++++++++++++++++++++++++++++++++++++++++- tralics2epub.py | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 80 insertions(+), 7 deletions(-) diff --git a/eoaconvert.py b/eoaconvert.py index 89cf1f2..87d54ad 100755 --- a/eoaconvert.py +++ b/eoaconvert.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8; mode: python -*- -# Time-stamp: <2018-02-09 10:52:21 (kthoden)> +# Time-stamp: <2018-02-09 13:22:07 (kthoden)> # license? __version__= "1.0" @@ -966,8 +966,15 @@ def cleanup(): dictFootnotes[strUID] = str(intNoteNumber) intNoteNumber += 1 - # here was OU's footnote code, now in libeoaconvert +# def get_bigfoot_data(chapter) + +# bigfoot needs to be integrated into +# 'fndict': {'uid11': '2', 'uid12': '3', 'uid9': '1'}, + +# the new-style footnotes that use LaTeX bigfoot show up in the following order: +footnote_groups = ["decimal", "lower-latin"] + print("-----------------------------------------------------") print("Numbering of Lists per Chapter") diff --git a/tralics2django.py b/tralics2django.py index ee2bb81..6113fd4 100755 --- a/tralics2django.py +++ b/tralics2django.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8; mode: python -*- -# Time-stamp: <2018-02-09 11:16:22 (kthoden)> +# Time-stamp: <2018-02-09 14:21:09 (kthoden)> import pickle import os @@ -97,6 +97,44 @@ # Find all Chapters from the original tralics XML xmlChapters = xmlTree.findall("//div1") +def replace_footnote_with_sup(note): + """ + captures reusable behavior from the existing code + potentially, some of the old code could be replaced by calls to this helper + + this behavior showed up in a few places + I thought I would be able to extract a little more, but this was all that was actually common + """ + tail = note.tail + note.clear() + note.tail = tail + note.tag = "sup" +# def replace_footnote_with_sup ends here + +def alph_footnote_index(fndex): + """ + lowercase Latin footnotes need to support more than 26 values + These are zero-indexed. + + >>> alph_footnote_index(0) + 'a' + >>> alph_footnote_index(1) + 'b' + >>> alph_footnote_index(24) + 'y' + >>> alph_footnote_index(25) + 'z' + >>> alph_footnote_index(26) + 'aa' + >>> alph_footnote_index(27) + 'ab' + """ + alphabet = "abcdefghijklmnopqrstuvwxyz" + quotient, remainder = divmod(fndex, len(alphabet)) + if not quotient: return alphabet[fndex] + return alph_footnote_index(quotient - 1) + alph_footnote_index(remainder) +# def alph_footnote_index ends here + def debug_chapters(xmlEOAchapters): """Write individual chapters to files""" @@ -978,6 +1016,8 @@ def bring_footnote_down_django(footnote, fragment, footnote_number, object_numbe for xmlEOAchapter in xmlEOAchapters: groupings = libeoaconvert.get_bigfoot_data(xmlEOAchapter) + print(groupings) + input("the groupings") has_old = 0 != len(xmlEOAchapter.findall(".//note")) has_new = 0 != len( [ # flatten diff --git a/tralics2epub.py b/tralics2epub.py index 517e4fa..17046a9 100755 --- a/tralics2epub.py +++ b/tralics2epub.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8; mode: python -*- -# Time-stamp: <2018-02-09 11:19:54 (kthoden)> +# Time-stamp: <2018-02-09 16:00:16 (kthoden)> import os import sys @@ -492,6 +492,8 @@ def replace_footnote_equations(footnote): shutil.copy("%s/items/%s" % (cwd, filename), "%s/CONVERT/epub/DEBPS/images/%s" % (cwd, filename)) result = addToContentopf(result, "images/" + filename, filename, "png") + print("einmal durch replace_footnote_equations") + return result # def replace_footnote_equations ends here @@ -503,6 +505,7 @@ def replace_footnote_with_sup(note): this behavior showed up in a few places I thought I would be able to extract a little more, but this was all that was actually common """ + tail = note.tail note.clear() note.tail = tail @@ -522,15 +525,20 @@ def bring_footnote_down_epub(footnote, footnote_name, destination): kids = list(footnote.getchildren()) prefix = "[%s]" % footnote_name + print("hier 0" + prefix) # we would like to prepend this footnote identifier to the footnote element if footnote.text is not None: + print("hier 1" + prefix) # if the element starts with some text anyway, prepend it there - footnote.text = "%s %s" % (prefix, footnote.text) + # footnote.text = "%s %s" % (prefix, footnote.text) + else: # if, however, the element begins with a child, prepend the text at the beginning of the first child instead if len(kids): + print("hier 2" + prefix) first_child = kids[0] - child_text = prefix + # child_text = prefix + child_text = "" # separate them with a space, unless the child had no text to begin with child_suffix = first_child.text if child_suffix is None: @@ -540,14 +548,24 @@ def bring_footnote_down_epub(footnote, footnote_name, destination): child_text += child_suffix first_child.text = child_text else: + print("hier 3" + prefix) # a totally empty footnote is weird, but who am I to judge? footnote.text = prefix + footnote_text = footnote.text or "" + replace_footnote_with_sup(footnote) - footnote.text = "[%s] " % footnote_name + footnote.text = "[" + note_link = etree.SubElement(footnote, "a") + note_link.set("href", "#fn" + footnote_name) + note_link.set("id", "body_fn-ref" + footnote_name) + note_link.text = "%s" % footnote_name + note_link.tail = "]" + # append any text the footnote used to have to the destination destkids = list(destination.getchildren()) if len(destkids): + print("hier 4") # if the destination has children, append after the last one's tail last_kid = destkids[-1] prefix = last_kid.tail @@ -557,6 +575,7 @@ def bring_footnote_down_epub(footnote, footnote_name, destination): prefix += " " last_kid.tail = prefix + footnote_text else: + print("hier 5") # if the destination has no children, append to its text prefix = destination.text if prefix is None: @@ -609,6 +628,13 @@ class FootnoteError(Exception): if "lower-latin" == grouping: footnote_name = alph_footnote_index(index) para = etree.Element("p") + para.text = "[" + note_link = etree.SubElement(para, "a") + note_link.set("id", "fn" + footnote_name) + note_link.set("href", "#body_fn-ref" + footnote_name) + note_link.text = "%s" % footnote_name + note_link.tail = "]" + contentopf = bring_footnote_down_epub(note, footnote_name, para) xmlNewFootnotes.append(para)