Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Django support for two kinds of footnotes
  • Loading branch information
calcnerd256 committed Mar 21, 2016
1 parent 694b8a8 commit 6ca5dc0
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 5 deletions.
140 changes: 135 additions & 5 deletions Skripten/EOAconvert.py
Expand Up @@ -802,6 +802,33 @@ def cleanup():
dictFootnotes[strUID] = str(intNoteNumber)
intNoteNumber += 1


footnote_groups = ["decimal", "lower-latin"]

def get_bigfoot_data(chapter):
# footnotes are per-chapter, with numbers resetting
xmlBigfootNotes = list(chapter.findall(".//EOAbigfoot"))
return [
(
grouping,
[
note
for note
in xmlBigfootNotes
if grouping == note.get("list-style-type")
],
)
for grouping
in footnote_groups
]


bigfoot_data = [
get_bigfoot_data(xmlChapter)
for xmlChapter
in xmlChapters
]

print ("-----------------------------------------------------")
print ("Numbering of Lists per Chapter")
for xmlChapter in xmlChapters:
Expand Down Expand Up @@ -1486,15 +1513,67 @@ def cleanup():

print ("-----------------------------------------------------")
print ("Preparing Footnotes")
for xmlChapter in xmlChapters:

def alph_fndex(fndex):
alphabet = "abcdefghijklmnopqrstuvwxyz"
quotient, remainder = divmod(fndex, len(alphabet))
if not quotient: return alphabet[fndex]
return alph_index(quotient - 1) + alph_index(remainder)


def replace_footnote_equations(footnote):
# usage: contentopf = replace_footnote_equations(my_footnote)
result = contentopf
for equation in footnote.findall(".//EOAequationnonumber"):
filename = equation.get("filename")
equation.clear()
equation.tag = "p"
img = etree.Element("img", src="images/%s" % filename, alt="")
equation.append(img)
cwd = os.getcwd()
shutil.copy("%s/items/%s" % (cwd, filename), "%s/CONVERT/epub/DEBPS/images/%s" % (cwd, filename))
result = addToContentopf(result, "images/" + filename, filename, "png")
return result


def replace_footnote_with_sup(note):
tail = note.tail
note.clear()
note.tail = tail
note.tag = "sup"


def bring_footnote_down_epub(footnote, footnote_name, destination):
contentopf = replace_footnote_equations(footnote)
kids = list(footnote.getchildren())
if len(kids):
first_child = kids[0]
first_child.text = "[%s]%s" % (footnote_name, (first_child.text or ""))
replace_footnote_with_sup(footnote)
footnote.text = "[%s]" % footnote_name
for kid in kids:
destination.append(kid)
return contentopf


for xmlChapter, groupings in zip(xmlChapters, bigfoot_data):
xmlFootnotes = xmlChapter.findall(".//note")
if len(xmlFootnotes) == 0:
continue
if not len([note for grouping, notes in groupings for note in notes]):
continue
xmlNewFootnotes = etree.Element("div")
xmlNewFootnotesHeader = etree.Element("h3")
xmlNewFootnotesHeader.text = dictLangFootnotes[xmlChapter.get("language")]
xmlNewFootnotes.append(xmlNewFootnotesHeader)
intFootnoteNumber = 1
notes_so_far = 0
for grouping, notes in groupings:
for index, note in enumerate(notes):
footnote_name = str(index + 1)
if "lower-latin" == grouping:
footnote_name = alph_fndex(index)
contentopf = bring_footnote_down_epub(note, footnote_name, xmlNewFootnotes)
notes_so_far += len(notes)
intFootnoteNumber = notes_so_far + 1
for xmlFootnote in xmlFootnotes:
# Not numbered Equations may appear in a footnote, need to be treated differently
xmlEquationsnonumber = xmlFootnote.findall(".//EOAequationnonumber")
Expand Down Expand Up @@ -2702,10 +2781,49 @@ def djangoParseHeadline(xmlElement):
print ("----------------------------------------------")
print ("Processing and linking Footnotes for django")

def bring_footnote_down_django(footnote, fragment, footnote_number, object_number, unique_id, destination):
kids = list(footnote.getchildren())
footnote_text = footnote.text or ""
replace_footnote_with_sup(footnote)
footnote.set("class", "footnote")
anchor = etree.Element("a")
anchor.set("href", "#" + fragment) # "fn" + str(intFootnoteNumber)
anchor.text = footnote_number # str(intFootnoteNumber)
footnote.append(anchor)
foot = etree.Element("EOAfootnote")
foot.set("order", str(object_number))
object_number += 1
foot.set("number", footnote_number)
anchor_number = next(
iter(
(
parent.get("order")
for parent
in footnote.iterancestors()
if parent.get("order") is not None
)
)
)
foot.set("anchor", anchorNumber)
foot.set("id", unique_id)
foot.text = footnote_text
for kid in kids:
if "EOAequationnonumber" == kid.tag:
cwd = os.getcwd()
shutil.copy(
"%s/items/%s" % (cwd, kid.get("filename")),
"%s/CONVERT/django/images/" % cwd,
)
foot.append(kid)
destination.append(foot)
return object_number


xmlEOAchapters = xmlEOAdocument.findall(".//EOAchapter")
for xmlEOAchapter in xmlEOAchapters:
for chapter_index, (xmlEOAchapter, groupings) in enumerate(zip(xmlEOAchapters, bigfoot_data)):
if len(xmlEOAchapter.findall(".//note")) == 0:
continue
if not len([note for grouping, notes in groupings for note in notes]):
continue
# Find out running order of last item the chapter
# Hier pro FN zunächst die EOAequationnonumber in <p> korrigieren
# Dann pro FN die Kindelemente abarbeiten und an die neue FN dran hängen
Expand All @@ -2722,6 +2840,18 @@ def djangoParseHeadline(xmlElement):
xmlHead.text = dictLangFootnotes[xmlEOAchapter.get("language")]
xmlEOAsection.append(xmlHead)
xmlResult.append(xmlEOAsection)

notes_so_far = 0
for grouping, notes in groupings:
for index, note in enumerate(notes):
unique_id = "bfn_%s_%s_%s" % (chapter_index, grouping, index)
fntext = str(index+1)
if "lower-latin" == grouping:
fntext = alph_fndex(index)
intObjectNumber = bring_footnote_down_django(note, unique_id, fntext, intObjectNumber, unique_id, xmlResult)
notes_so_far += len(notes)

intFootnoteNumber = notes_so_far + 1
xmlFootnotes = xmlEOAchapter.findall(".//note")
for xmlFootnote in xmlFootnotes:
xmlFootnoteContent = xmlFootnote.getchildren()
Expand Down
3 changes: 3 additions & 0 deletions TeX/pre_xml.tex
Expand Up @@ -268,3 +268,6 @@
%EOAtablehead
\newcommand*{\EOAtablehead}[2][1]{\begin{xmlelement}{tableheader}TRUE\end{xmlelement}#2\\}
\newcommand*{\EOAfnarabic}[1]{\begin{xmlelement}{EOAbigfoot}\AddAttToCurrent{list-style-type}{decimal}#1\end{xmlelement}}
\newcommand*{\EOAfnalph}[1]{\begin{xmlelement}{EOAbigfoot}\AddAttToCurrent{list-style-type}{lower-latin}#1\end{xmlelement}}

0 comments on commit 6ca5dc0

Please sign in to comment.