diff --git a/src/imxml2epub.py b/src/imxml2epub.py index 9822707..42a1595 100755 --- a/src/imxml2epub.py +++ b/src/imxml2epub.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8; mode: python -*- -# Time-stamp: <2021-07-02 12:15:51 (kthoden)> +# Time-stamp: <2021-07-09 15:26:19 (kthoden)> """ Convert a customized DocBook XML file into a set of files that constitute the contents of an EPUB file. @@ -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", "") @@ -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: @@ -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: @@ -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 "" @@ -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"