From 694b8a84c7b4ddda27a672de2e5db3ebb3430661 Mon Sep 17 00:00:00 2001 From: Logan Cox Date: Thu, 17 Mar 2016 11:14:28 -0500 Subject: [PATCH 01/11] initial version of alpha and arabic footnote sets --- TeX/pre_eoa.tex | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/TeX/pre_eoa.tex b/TeX/pre_eoa.tex index 9171305..1a621d9 100755 --- a/TeX/pre_eoa.tex +++ b/TeX/pre_eoa.tex @@ -611,3 +611,19 @@ \setlength\emergencystretch{3em} \usepackage[german=quotes]{csquotes} + + + + +%%%%%%%%%%%% Footnote Sets +% +\usepackage{etex} +\usepackage{bigfoot} +\reserveinserts{50} +\DeclareNewFootnote{Alph}[alph] +\DeclareNewFootnote{Arabic}[arabic] + +\newcommand{\EOAfnarabic}[1]{\protect\footnoteArabic{#1}} +\newcommand{\EOAfnalph}[1]{\protect\footnoteAlph{#1}} + +%%%%%%%%%%%% Footnote Sets --end From 1f937cf1fe14a2b75b7655cd9573ac3c4fe118e8 Mon Sep 17 00:00:00 2001 From: Logan Cox Date: Fri, 18 Mar 2016 14:48:38 -0500 Subject: [PATCH 02/11] Refine footenote numbering and order. * add footnote counter reset to EOAchapter command * re-order footnote command DeclareNewFootnote calls to reorder footnote sets in output --- TeX/pre_eoa.tex | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/TeX/pre_eoa.tex b/TeX/pre_eoa.tex index 1a621d9..16faa6c 100755 --- a/TeX/pre_eoa.tex +++ b/TeX/pre_eoa.tex @@ -372,6 +372,8 @@ } % New command for numbered chapters \newcommand{\EOAchapter}[2]{ + \setcounter{footnotealph}{0} + \setcounter{footnotearabic}{0} \chapter[#2]{#2} \chaptermark{#1} \ifthenelse{\boolean{anthology}}{ @@ -616,14 +618,22 @@ %%%%%%%%%%%% Footnote Sets -% + \usepackage{etex} \usepackage{bigfoot} \reserveinserts{50} -\DeclareNewFootnote{Alph}[alph] -\DeclareNewFootnote{Arabic}[arabic] +\DeclareNewFootnote{arabic}[arabic] +\DeclareNewFootnote{alph}[alph] + + + +\usepackage{alphalph} +\renewcommand{\thefootnotealph}{\emph{\alphalph{\value{footnotealph}}}} + +\newcommand{\EOAfnalph}[1]{\protect\footnotealph{#1}} +\newcommand{\EOAfnarabic}[1]{\protect\footnotearabic{#1}} + + -\newcommand{\EOAfnarabic}[1]{\protect\footnoteArabic{#1}} -\newcommand{\EOAfnalph}[1]{\protect\footnoteAlph{#1}} %%%%%%%%%%%% Footnote Sets --end From 6ca5dc089fd9950317662dd53b8533c5f543d783 Mon Sep 17 00:00:00 2001 From: Montana Rowe Date: Mon, 21 Mar 2016 09:30:43 -0500 Subject: [PATCH 03/11] Django support for two kinds of footnotes --- Skripten/EOAconvert.py | 140 +++++++++++++++++++++++++++++++++++++++-- TeX/pre_xml.tex | 3 + 2 files changed, 138 insertions(+), 5 deletions(-) diff --git a/Skripten/EOAconvert.py b/Skripten/EOAconvert.py index 422d2cc..92d92a5 100755 --- a/Skripten/EOAconvert.py +++ b/Skripten/EOAconvert.py @@ -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: @@ -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") @@ -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

korrigieren # Dann pro FN die Kindelemente abarbeiten und an die neue FN dran hängen @@ -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() diff --git a/TeX/pre_xml.tex b/TeX/pre_xml.tex index 50de11c..3c85caa 100644 --- a/TeX/pre_xml.tex +++ b/TeX/pre_xml.tex @@ -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}} From aea681d298c4c6baa10bf915e19077f455163ee3 Mon Sep 17 00:00:00 2001 From: Montana Rowe Date: Mon, 21 Mar 2016 13:22:07 -0500 Subject: [PATCH 04/11] correct some typos in variable names --- Skripten/EOAconvert.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Skripten/EOAconvert.py b/Skripten/EOAconvert.py index 92d92a5..aadee0e 100755 --- a/Skripten/EOAconvert.py +++ b/Skripten/EOAconvert.py @@ -1518,7 +1518,7 @@ 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) + return alph_fndex(quotient - 1) + alph_fndex(remainder) def replace_footnote_equations(footnote): @@ -2804,7 +2804,7 @@ def bring_footnote_down_django(footnote, fragment, footnote_number, object_numbe ) ) ) - foot.set("anchor", anchorNumber) + foot.set("anchor", anchor_number) foot.set("id", unique_id) foot.text = footnote_text for kid in kids: From 7e7968ecd7bc48e299986090653027efbf1f3025 Mon Sep 17 00:00:00 2001 From: Montana Rowe Date: Mon, 21 Mar 2016 15:28:02 -0500 Subject: [PATCH 05/11] Show custom footnotes in epub output. --- Skripten/EOAconvert.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Skripten/EOAconvert.py b/Skripten/EOAconvert.py index aadee0e..43deda6 100755 --- a/Skripten/EOAconvert.py +++ b/Skripten/EOAconvert.py @@ -1548,15 +1548,16 @@ def bring_footnote_down_epub(footnote, footnote_name, destination): kids = list(footnote.getchildren()) if len(kids): first_child = kids[0] - first_child.text = "[%s]%s" % (footnote_name, (first_child.text or "")) + first_child.text = "[%s] %s" % (footnote_name, (first_child.text or "")) replace_footnote_with_sup(footnote) - footnote.text = "[%s]" % footnote_name + footnote.text = "[%s] " % footnote_name for kid in kids: destination.append(kid) return contentopf -for xmlChapter, groupings in zip(xmlChapters, bigfoot_data): +for xmlChapter in xmlChapters: + groupings = get_bigfoot_data(xmlChapter) xmlFootnotes = xmlChapter.findall(".//note") if len(xmlFootnotes) == 0: if not len([note for grouping, notes in groupings for note in notes]): @@ -1571,7 +1572,10 @@ def bring_footnote_down_epub(footnote, footnote_name, destination): footnote_name = str(index + 1) if "lower-latin" == grouping: footnote_name = alph_fndex(index) - contentopf = bring_footnote_down_epub(note, footnote_name, xmlNewFootnotes) + para = etree.Element("p") + para.text = "[%s] %s" % (footnote_name, note.text) + contentopf = bring_footnote_down_epub(note, footnote_name, para) + xmlNewFootnotes.append(para) notes_so_far += len(notes) intFootnoteNumber = notes_so_far + 1 for xmlFootnote in xmlFootnotes: From c2248a8def0ed29b75ef5ed4d347b29bfb2ebd20 Mon Sep 17 00:00:00 2001 From: Montana Rowe Date: Tue, 22 Mar 2016 09:32:27 -0500 Subject: [PATCH 06/11] Refuse to mix old-style and new-style footnotes within a chapter, and use old-style footnotes' fragments for backwards compatibility of footnote URLs. --- Skripten/EOAconvert.py | 54 +++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/Skripten/EOAconvert.py b/Skripten/EOAconvert.py index 43deda6..70d8027 100755 --- a/Skripten/EOAconvert.py +++ b/Skripten/EOAconvert.py @@ -823,12 +823,6 @@ def get_bigfoot_data(chapter): ] -bigfoot_data = [ - get_bigfoot_data(xmlChapter) - for xmlChapter - in xmlChapters -] - print ("-----------------------------------------------------") print ("Numbering of Lists per Chapter") for xmlChapter in xmlChapters: @@ -1556,17 +1550,30 @@ def bring_footnote_down_epub(footnote, footnote_name, destination): return contentopf +class FootnoteError(Exception): + pass + for xmlChapter in xmlChapters: groupings = get_bigfoot_data(xmlChapter) - xmlFootnotes = xmlChapter.findall(".//note") - if len(xmlFootnotes) == 0: - if not len([note for grouping, notes in groupings for note in notes]): + xmlFootnotes = list(xmlChapter.findall(".//note")) + has_old = 0 != len(xmlFootnotes) + has_new = 0 != len( + [ + note + for grouping, notes in groupings + for note in notes + ] + ) + if has_old: + if has_new: + raise FootnoteError("This chapter contains both old-style footnotes and new-style footnotes") + else: + if not has_new: continue xmlNewFootnotes = etree.Element("div") xmlNewFootnotesHeader = etree.Element("h3") xmlNewFootnotesHeader.text = dictLangFootnotes[xmlChapter.get("language")] xmlNewFootnotes.append(xmlNewFootnotesHeader) - notes_so_far = 0 for grouping, notes in groupings: for index, note in enumerate(notes): footnote_name = str(index + 1) @@ -1576,8 +1583,7 @@ def bring_footnote_down_epub(footnote, footnote_name, destination): para.text = "[%s] %s" % (footnote_name, note.text) contentopf = bring_footnote_down_epub(note, footnote_name, para) xmlNewFootnotes.append(para) - notes_so_far += len(notes) - intFootnoteNumber = notes_so_far + 1 + intFootnoteNumber = 1 for xmlFootnote in xmlFootnotes: # Not numbered Equations may appear in a footnote, need to be treated differently xmlEquationsnonumber = xmlFootnote.findall(".//EOAequationnonumber") @@ -2824,9 +2830,21 @@ def bring_footnote_down_django(footnote, fragment, footnote_number, object_numbe xmlEOAchapters = xmlEOAdocument.findall(".//EOAchapter") -for chapter_index, (xmlEOAchapter, groupings) in enumerate(zip(xmlEOAchapters, bigfoot_data)): - if len(xmlEOAchapter.findall(".//note")) == 0: - if not len([note for grouping, notes in groupings for note in notes]): +for xmlEOAchapter in xmlEOAchapters: + groupings = get_bigfoot_data(xmlEOAchapter) + has_old = 0 != len(xmlEOAchapter.findall(".//note")) + has_new = 0 != len( + [ + note + for grouping, notes in groupings + for note in notes + ] + ) + if has_old: + if has_new: + raise FootnoteError("This chapter contains both old-style footnotes and new-style footnotes") + else: + if not has_new: continue # Find out running order of last item the chapter # Hier pro FN zunächst die EOAequationnonumber in

korrigieren @@ -2845,17 +2863,15 @@ def bring_footnote_down_django(footnote, fragment, footnote_number, object_numbe 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) + unique_id = "fn%s" % fntext intObjectNumber = bring_footnote_down_django(note, unique_id, fntext, intObjectNumber, unique_id, xmlResult) - notes_so_far += len(notes) - intFootnoteNumber = notes_so_far + 1 + intFootnoteNumber = 1 xmlFootnotes = xmlEOAchapter.findall(".//note") for xmlFootnote in xmlFootnotes: xmlFootnoteContent = xmlFootnote.getchildren() From dcdf460350c3a2bc225d048435b5c50dba15e6b5 Mon Sep 17 00:00:00 2001 From: Logan Cox Date: Tue, 22 Mar 2016 15:46:32 -0500 Subject: [PATCH 07/11] Add comments and clean up whitespace --- TeX/pre_eoa.tex | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/TeX/pre_eoa.tex b/TeX/pre_eoa.tex index 16faa6c..51a2f76 100755 --- a/TeX/pre_eoa.tex +++ b/TeX/pre_eoa.tex @@ -372,8 +372,8 @@ } % New command for numbered chapters \newcommand{\EOAchapter}[2]{ - \setcounter{footnotealph}{0} - \setcounter{footnotearabic}{0} +\setcounter{footnotealph}{0} +\setcounter{footnotearabic}{0} \chapter[#2]{#2} \chaptermark{#1} \ifthenelse{\boolean{anthology}}{ @@ -618,22 +618,19 @@ %%%%%%%%%%%% Footnote Sets - \usepackage{etex} \usepackage{bigfoot} -\reserveinserts{50} +\usepackage{alphalph} +% Allocate some extra registers so that we don't run out when +% declaring footnotes +\reserveinserts{4} + \DeclareNewFootnote{arabic}[arabic] \DeclareNewFootnote{alph}[alph] - - - -\usepackage{alphalph} +%allow for more than 26 alpha-numbered footnotes per chater +%(aa,ab,ac,...) \renewcommand{\thefootnotealph}{\emph{\alphalph{\value{footnotealph}}}} \newcommand{\EOAfnalph}[1]{\protect\footnotealph{#1}} \newcommand{\EOAfnarabic}[1]{\protect\footnotearabic{#1}} - - - - %%%%%%%%%%%% Footnote Sets --end From 6dc31b9380c6d16f0e513b6eb355f322c9452be7 Mon Sep 17 00:00:00 2001 From: Montana Rowe Date: Tue, 22 Mar 2016 16:08:31 -0500 Subject: [PATCH 08/11] Handle more cases of epub footnotes. --- Skripten/EOAconvert.py | 131 +++++++++++++++++++++++++++++++++++------ 1 file changed, 113 insertions(+), 18 deletions(-) diff --git a/Skripten/EOAconvert.py b/Skripten/EOAconvert.py index 70d8027..2b29051 100755 --- a/Skripten/EOAconvert.py +++ b/Skripten/EOAconvert.py @@ -803,15 +803,22 @@ def cleanup(): intNoteNumber += 1 +# the new-style footnotes that use LaTeX bigfoot show up in the following order: footnote_groups = ["decimal", "lower-latin"] def get_bigfoot_data(chapter): - # footnotes are per-chapter, with numbers resetting + """ + footnotes are per-chapter + footnote numbers reset for each chapter + this helper takes a chapter and returns a collection containing its new-style footnotes that use LaTeX bigfoot + the result is an association list: a list of key-value pairs + the values are, for each type of footnote, a list of the footnotes of that type, in the order in which they appear in the chapter + """ xmlBigfootNotes = list(chapter.findall(".//EOAbigfoot")) - return [ - ( - grouping, - [ + return [ # a list + ( # of tuples + grouping, # the key + [ # the value: a filter of the above list note for note in xmlBigfootNotes @@ -819,7 +826,7 @@ def get_bigfoot_data(chapter): ], ) for grouping - in footnote_groups + in footnote_groups # the types we support ] @@ -1508,15 +1515,38 @@ def get_bigfoot_data(chapter): print ("-----------------------------------------------------") print ("Preparing Footnotes") -def alph_fndex(fndex): +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_fndex(quotient - 1) + alph_fndex(remainder) + return alph_footnote_index(quotient - 1) + alph_footnote_index(remainder) def replace_footnote_equations(footnote): - # usage: contentopf = replace_footnote_equations(my_footnote) + """ + captures reusable behavior from the existing code + potentially, some of the old code could be replaced by calls to this helper + + usage: contentopf = replace_footnote_equations(my_footnote) + unfortunately, returning the result seemed like a better idea than mutating the global variable + """ result = contentopf for equation in footnote.findall(".//EOAequationnonumber"): filename = equation.get("filename") @@ -1531,6 +1561,13 @@ def replace_footnote_equations(footnote): 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 @@ -1538,35 +1575,85 @@ def replace_footnote_with_sup(note): def bring_footnote_down_epub(footnote, footnote_name, destination): - contentopf = replace_footnote_equations(footnote) + """ + captures reusable behavior from the existing code + potentially, some of the old code could be replaced by calls to this helper + """ + + contentopf = replace_footnote_equations(footnote) # see usage note kids = list(footnote.getchildren()) - if len(kids): - first_child = kids[0] - first_child.text = "[%s] %s" % (footnote_name, (first_child.text or "")) + prefix = "[%s]" % footnote_name + + # we would like to prepend this footnote identifier to the footnote element + if footnote.text is not None: + # if the element starts with some text anyway, prepend it there + 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): + first_child = kids[0] + child_text = prefix + # separate them with a space, unless the child had no text to begin with + child_suffix = first_child.text + if child_suffix is None: + child_suffix = "" + else: + child_prefix += " " + first_child.text = child_prefix + child_suffix + else: + # 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 + # append any text the footnote used to have to the destination + destkids = list(destination.getchildren()) + if len(destkids): + # if the destination has children, append after the last one's tail + last_kid = destkids[-1] + prefix = last_kid.tail + if prefix is None: + prefix = "" + else: + prefix += " " + last_kid.tail = prefix + footnote_text + else: + # if the destination has no children, append to its text + prefix = destination.text + if prefix is None: + prefix = "" + else: + prefix += " " + destination.text = prefix + footnote_text for kid in kids: destination.append(kid) return contentopf class FootnoteError(Exception): + """ + we only support one type of footnote per chapter + don't try to mix-and-match + """ pass + for xmlChapter in xmlChapters: groupings = get_bigfoot_data(xmlChapter) xmlFootnotes = list(xmlChapter.findall(".//note")) has_old = 0 != len(xmlFootnotes) has_new = 0 != len( - [ + [ # flatten the association list whose values are lists, so we can take the length note for grouping, notes in groupings for note in notes ] ) + + # the XOR case falls through, the AND is an error, and the NOR skips to the next chapter if has_old: if has_new: - raise FootnoteError("This chapter contains both old-style footnotes and new-style footnotes") + raise FootnoteError("Chapter %s contains both \\EOAfn and footnotes in the style of \\EOAfnalph" % xmlChapter.get("id-text")) else: if not has_new: continue @@ -1575,10 +1662,11 @@ class FootnoteError(Exception): xmlNewFootnotesHeader.text = dictLangFootnotes[xmlChapter.get("language")] xmlNewFootnotes.append(xmlNewFootnotesHeader) for grouping, notes in groupings: + # do for the new-style footnotes what was being done for the old for index, note in enumerate(notes): footnote_name = str(index + 1) if "lower-latin" == grouping: - footnote_name = alph_fndex(index) + footnote_name = alph_footnote_index(index) para = etree.Element("p") para.text = "[%s] %s" % (footnote_name, note.text) contentopf = bring_footnote_down_epub(note, footnote_name, para) @@ -2792,6 +2880,11 @@ def djangoParseHeadline(xmlElement): print ("Processing and linking Footnotes for django") def bring_footnote_down_django(footnote, fragment, footnote_number, object_number, unique_id, destination): + """ + captures reusable behavior from the existing code + potentially, some of the old code could be replaced by calls to this helper + """ + kids = list(footnote.getchildren()) footnote_text = footnote.text or "" replace_footnote_with_sup(footnote) @@ -2834,12 +2927,13 @@ def bring_footnote_down_django(footnote, fragment, footnote_number, object_numbe groupings = get_bigfoot_data(xmlEOAchapter) has_old = 0 != len(xmlEOAchapter.findall(".//note")) has_new = 0 != len( - [ + [ # flatten note for grouping, notes in groupings for note in notes ] ) + # XOR falls through, AND is an error (that should have already been thrown during the epub phase), and NOR skips to the next chapter if has_old: if has_new: raise FootnoteError("This chapter contains both old-style footnotes and new-style footnotes") @@ -2865,9 +2959,10 @@ def bring_footnote_down_django(footnote, fragment, footnote_number, object_numbe for grouping, notes in groupings: for index, note in enumerate(notes): + # do for the new-style notes what the old code did for the other footnotes fntext = str(index+1) if "lower-latin" == grouping: - fntext = alph_fndex(index) + fntext = alph_footnote_index(index) unique_id = "fn%s" % fntext intObjectNumber = bring_footnote_down_django(note, unique_id, fntext, intObjectNumber, unique_id, xmlResult) From f31fad9b9d052d63b88e5e442d008882b8a6df00 Mon Sep 17 00:00:00 2001 From: Montana Rowe Date: Tue, 22 Mar 2016 16:27:54 -0500 Subject: [PATCH 09/11] don't double-denote footnotes in epub --- Skripten/EOAconvert.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Skripten/EOAconvert.py b/Skripten/EOAconvert.py index 2b29051..4fa1689 100755 --- a/Skripten/EOAconvert.py +++ b/Skripten/EOAconvert.py @@ -1578,6 +1578,9 @@ def bring_footnote_down_epub(footnote, footnote_name, destination): """ captures reusable behavior from the existing code potentially, some of the old code could be replaced by calls to this helper + + usage: contentopf = bring_footnote_down_epub(my_footnote, "1", xmlNewFootnotes) + unfortunately, returning the result seemed like a better idea than mutating the global variable """ contentopf = replace_footnote_equations(footnote) # see usage note @@ -1587,7 +1590,7 @@ def bring_footnote_down_epub(footnote, footnote_name, destination): # we would like to prepend this footnote identifier to the footnote element if footnote.text is not None: # 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): @@ -1668,7 +1671,6 @@ class FootnoteError(Exception): if "lower-latin" == grouping: footnote_name = alph_footnote_index(index) para = etree.Element("p") - para.text = "[%s] %s" % (footnote_name, note.text) contentopf = bring_footnote_down_epub(note, footnote_name, para) xmlNewFootnotes.append(para) intFootnoteNumber = 1 @@ -2883,6 +2885,9 @@ def bring_footnote_down_django(footnote, fragment, footnote_number, object_numbe """ captures reusable behavior from the existing code potentially, some of the old code could be replaced by calls to this helper + + usage: intObjectNumber = bring_footnote_down_django(xmlFootnote, "fn"+str(intFootnoteNumber), str(intFootnoteNumber), intObjectNumber, tmpStrUID, xmlResult) + unfortunately, returning the result seemed like a better idea than mutating the global variable """ kids = list(footnote.getchildren()) From 21469543097a76afb0d2338a28aebf5e1d083b12 Mon Sep 17 00:00:00 2001 From: Montana Rowe Date: Tue, 22 Mar 2016 16:54:22 -0500 Subject: [PATCH 10/11] use the right variable names in epub helper --- Skripten/EOAconvert.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Skripten/EOAconvert.py b/Skripten/EOAconvert.py index 4fa1689..f86a6bd 100755 --- a/Skripten/EOAconvert.py +++ b/Skripten/EOAconvert.py @@ -1601,8 +1601,9 @@ def bring_footnote_down_epub(footnote, footnote_name, destination): if child_suffix is None: child_suffix = "" else: - child_prefix += " " - first_child.text = child_prefix + child_suffix + child_text += " " + child_text += child_suffix + first_child.text = child_text else: # a totally empty footnote is weird, but who am I to judge? footnote.text = prefix From e36145644ac2e9cba1ae5fccd41d11b21cf3d54d Mon Sep 17 00:00:00 2001 From: Montana Rowe Date: Wed, 23 Mar 2016 09:58:14 -0500 Subject: [PATCH 11/11] Make last-modification metadata less specific and more accurate. --- TeX/pre_eoa.tex | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/TeX/pre_eoa.tex b/TeX/pre_eoa.tex index 51a2f76..7b893a9 100755 --- a/TeX/pre_eoa.tex +++ b/TeX/pre_eoa.tex @@ -1,6 +1,5 @@ -% Version: 1.5 -% 14/07/2015 -% Last modifier: Georg +% Version: 1.5+ +% Last modified: March 2016 \documentclass[10pt,openright,twoside]{scrbook}