Skip to content

Commit

Permalink
Merge branch 'feature_ebooklinking' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
kthoden committed Nov 16, 2021
2 parents 4c8c93a + 97602ec commit be9a755
Showing 1 changed file with 51 additions and 15 deletions.
66 changes: 51 additions & 15 deletions src/imxml2epub.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: <2021-11-15 21:09:52 (kthoden)>
# Time-stamp: <2021-11-16 08:57:52 (kthoden)>

""" Convert a customized DocBook XML file into a set of files that
constitute the contents of an EPUB file.
Expand Down Expand Up @@ -863,6 +863,7 @@ def add_css_snippet(css_snippet, css_file):
xmlFigure.clear()
xmlFigure.tag = "p"
xmlFigure.set("class", "centered_image")
xmlFigure.set("id", idFigure)
xmlFigureImage = etree.Element("img")
xmlFigureImage.set("src", "images/" + strImageFileDir + strImageFileNamewoSuffix + "." + extension_and_mime)
xmlFigureImage.set("alt", "")
Expand Down Expand Up @@ -1770,6 +1771,12 @@ class FootnoteError(Exception):

logging.info(f"{logseparator}Preparing Cross-References")

libeoaconvert.debug_xml_here(
xmlEbookTree,
"epubtree_beforecrossreferences",
DEBUG_DIR
)

# restart chapter counter
intChapterNumber = 1
for xmlChapter in xmlChapters:
Expand Down Expand Up @@ -1800,9 +1807,43 @@ class FootnoteError(Exception):

reference_type = xmlReference.get("type")

xmlReferenceLabel = xmlReference.find("Label")
xmlReferenceLabelText = xmlReferenceLabel.text

xmlReferenceRef = xmlReference.find("ref")
xmlReferenceRefTarget = xmlReferenceRef.get("target")

if reference_type == "text":
tmpTail = xmlReference.tail or ""
ref_is_text = True

logging.debug(f"{xmlReferenceRefTarget} is a text link")
ref_is_text = True
xmlReferenceRef = xmlReference.find("ref")
xmlReferenceRefTarget = xmlReferenceRef.get("target")
xmlReferenceLabel = xmlReference.find("Label")
xmlReferenceLabelText = xmlReferenceLabel.text

pararef = xmlEbookTree.xpath("//*[@id='%s']" % xmlReferenceRefTarget)
logging.info(pararef[0].get("id"))
if len(pararef) == 0:
logging.warning("There seems to be no corresponding xml:id for %s." % xmlReferenceRefTarget)
failed_ids.append(f"{xmlReferenceRefTarget} ({xmlReferenceLabelText})\n")
elif len(pararef) > 1:
logging.error("The xml:id %s is assigned more than once. This is not allowed. Exiting." % xmlReferenceLabelText)
sys.exit(2)
else:
closest_ancestor_with_id = pararef[0].xpath("ancestor::*[starts-with(name(), 'div') and @id][1]")
closest_id = closest_ancestor_with_id[0].get("id")

for xmlParent in pararef[0].iterancestors():
if xmlParent.tag == "EOAchapter":
strChapterOrder = xmlParent.get("order")
for xmlParent in pararef[0].iterancestors():
if xmlParent.tag == "EOAparagraph":
strObjectOrder = xmlParent.get("order")


all_children = list(xmlReference)
text_has_children = all_children[:-2]
if text_has_children:
Expand All @@ -1825,37 +1866,31 @@ class FootnoteError(Exception):
elif reference_type == "number":
strResult = "!!! Cross Reference !!!"

xmlReferenceLabel = xmlReference.find("Label")
xmlReferenceLabelText = xmlReferenceLabel.text

xmlReferenceRef = xmlReference.find("ref")
xmlReferenceRefTarget = xmlReferenceRef.get("target")

if xmlReferenceLabelText in dictEquations:
logging.info("Found link to array:" + xmlReferenceLabelText)
strResult = dictEquations[xmlReferenceLabelText]
if xmlReferenceRefTarget in dictEquations:
elif xmlReferenceRefTarget in dictEquations:
logging.info("Found link to equation:" + xmlReferenceRefTarget)
strResult = dictEquations[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictLists:
elif xmlReferenceRefTarget in dictLists:
logging.info("Found link to list")
strResult = dictLists[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictChapters:
elif xmlReferenceRefTarget in dictChapters:
logging.info("Found link to chapter")
strResult = dictChapters[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictSections:
elif xmlReferenceRefTarget in dictSections:
logging.info("Found link to section")
strResult = dictSections[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictFigures:
elif xmlReferenceRefTarget in dictFigures:
logging.info("Found link to figure")
strResult = dictFigures[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictFootnotes:
elif xmlReferenceRefTarget in dictFootnotes:
logging.info("Found link to footnote")
strResult = dictFootnotes[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictTheorems:
elif xmlReferenceRefTarget in dictTheorems:
logging.info("Found link to theorem")
strResult = dictTheorems[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictTables:
elif xmlReferenceRefTarget in dictTables:
logging.info("Found link to table")
strResult = dictTables[xmlReferenceRefTarget]
tmpTail = xmlReference.tail or ""
Expand All @@ -1864,6 +1899,7 @@ class FootnoteError(Exception):
logging.error("Found unknown reference type: %s. Exiting", reference_type)
sys.exit(0)
xmlReference.clear()

if args.hyperimage and hitarget_id and reference_type in ["collage", "number"]:
hyperimage_link = f"{publication_landingpage[:-11]}/{intChapterNumber}/index.html#{hitarget_id}"
xmlReference.tag = "a"
Expand Down

0 comments on commit be9a755

Please sign in to comment.