Skip to content

Commit

Permalink
Double footnotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaus Thoden committed Feb 9, 2018
1 parent 8c1a254 commit a620abf
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
11 changes: 9 additions & 2 deletions eoaconvert.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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")
Expand Down
42 changes: 41 additions & 1 deletion tralics2django.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"""

Expand Down Expand Up @@ -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
Expand Down
34 changes: 30 additions & 4 deletions tralics2epub.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit a620abf

Please sign in to comment.