Skip to content

Commit

Permalink
Enable hyperimage support for epub
Browse files Browse the repository at this point in the history
  • Loading branch information
kthoden committed Jan 14, 2020
1 parent 9552d24 commit 1c507db
Showing 1 changed file with 93 additions and 46 deletions.
139 changes: 93 additions & 46 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: <2019-12-18 10:13:44 (kthoden)>
# Time-stamp: <2020-01-14 14:56:34 (kthoden)>

""" Convert a customized DocBook XML file into a set of files that
constitute the contents of an EPUB file.
Expand Down Expand Up @@ -31,6 +31,7 @@
SCRIPT_PATH = Path( __file__ )
SCRIPT_NAME = SCRIPT_PATH.stem


DEFAULT_INPUT_DIR = \
Path(os.environ['INPUT_DIR'] if 'INPUT_DIR' in os.environ else './input')

Expand Down Expand Up @@ -97,6 +98,13 @@
help="Specify the directory with files of the font (the font itself, License)",
)

parser.add_argument(
"-him", "--hyperimage",
help="Link hyperlink references to online version.",
action="store_true"
)


args = parser.parse_args()

config_file = args.CONFIG_FILE
Expand Down Expand Up @@ -179,6 +187,12 @@
dictPagelabels = data["pagelabeldict"]


if args.hyperimage:
logging.info("Enabled Hyperimage support")
else:
pass


def get_mimetype(filename_suffix):
"""Return mimetype of image"""
if filename_suffix.lower() == ".jpg":
Expand Down Expand Up @@ -526,6 +540,7 @@ def add_css_snippet(css_snippet, css_file):
publication_series = cfgPublication.get("Technical", "Serie")
publication_number = cfgPublication.get("Technical", "Number")
publication_license = cfgPublication.get("Technical", "License")
publication_landingpage = cfgPublication.get("Technical", "LandingPage")
try:
publication_isbn = cfgPublication.get("Technical", "ISBN-epub")
except:
Expand Down Expand Up @@ -738,7 +753,9 @@ def add_css_snippet(css_snippet, css_file):
xmlParagraph.find("head").tag = "h5"

logging.info(f"{logseparator}Preparing Figures")
xmlFigures = xmlEbookTree.xpath(".//EOAfigure[not(@type='hionly')] | .//EOAlsfigure[not(@type='hionly')]")
xmlFigures = xmlEbookTree.xpath(".//EOAfigure[not(contains(@type,'hionly'))]")
libeoaconvert.debug_xml_here(xmlEbookTree, "find_eoafigures", DEBUG_DIR)
logging.info("Found %s figures", len(xmlFigures))
for xmlFigure in xmlFigures:
# Copy File of the Image
# If it's in a subfolder, name of folder and name of image will be merged
Expand Down Expand Up @@ -808,9 +825,10 @@ def add_css_snippet(css_snippet, css_file):
# Change the tag of the parent <p>-Tag to <div> so that it may be removed
#xmlFigure.getparent().tag = "div"

xml_figures_hyperimage = xmlEbookTree.xpath(".//EOAfigure[@type='hionly'] | .//EOAlsfigure[@type='hionly']")
logging.debug("found %s hyperimage figures" % len(xml_figures_hyperimage))
xml_figures_hyperimage = xmlEbookTree.xpath(".//EOAfigure[contains(@type,'hionly')]")
logging.info("Found %s hyperimage figures", len(xml_figures_hyperimage))
for fig in xml_figures_hyperimage:
fig.clear()
fig.tag = "EOAhifigure"

logging.info(f"{logseparator}Preparing not numbered Figures")
Expand Down Expand Up @@ -1659,10 +1677,9 @@ class FootnoteError(Exception):
logging.info(f"{logseparator}Preparing Cross-References")

for xmlChapter in xmlChapters:
xmlReferences = xmlChapter.findall(".//EOAref")
xmlReferences = xmlChapter.xpath(".//EOAref[not(parent::EOAref)]")
for xmlReference in xmlReferences:


# the new stuff
# label_text = xmlReference.find("Label").text[1:]
# logging.debug("label text is %s" % label_text)
Expand All @@ -1678,47 +1695,77 @@ class FootnoteError(Exception):
# eoa_id = eoa_id_element.get("id")
# end of the new stuff

hitarget_id_list = xmlReference.xpath("./ref/@hitarget")

logging.info("XXXXXXXX")
strResult = "!!! Cross Reference !!!"

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

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

if xmlReferenceLabelText in dictEquations:
logging.info("Verweis auf Array gefunden:" + xmlReferenceLabelText)
strResult = dictEquations[xmlReferenceLabelText]
if xmlReferenceRefTarget in dictEquations:
logging.info("Verweis auf Equation gefunden:" + xmlReferenceRefTarget)
strResult = dictEquations[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictLists:
logging.info("Verweis auf Liste gefunden")
strResult = dictLists[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictChapters:
logging.info("Verweis auf Kapitel gefunden")
strResult = dictChapters[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictSections:
logging.info("Verweis auf Section gefunden")
strResult = dictSections[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictFigures:
logging.info("Verweis auf Abbildung gefunden")
strResult = dictFigures[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictFootnotes:
logging.info("Verweis auf Fussnote gefunden")
strResult = dictFootnotes[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictTheorems:
logging.info("Verweis auf Theorem gefunden")
strResult = dictTheorems[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictTables:
logging.info("Verweis auf Tabelle gefunden")
strResult = dictTables[xmlReferenceRefTarget]
tmpTail = xmlReference.tail or ""
#tmpTail = tmpTail.strip()
if len(hitarget_id_list) == 1:
hitarget_id = hitarget_id_list[0]
else:
hitarget_id = None

reference_type = xmlReference.get("type")
if reference_type == "text":
tmpTail = xmlReference.tail or ""
strResult = xmlReference.text
elif reference_type == "collage":
tmpTail = xmlReference.tail or ""
logging.debug("Found reference to a Hyperimage collage.")
subreferences = xmlReference.xpath("./EOAref[@type='number']")
strResult = ""
for subref in subreferences:
subref_tail = subref.tail or ""
subref_target = subref.xpath("./ref/@target")[0]
target_string = dictFigures[subref_target]
strResult += f"{target_string}{subref_tail}"
elif reference_type == "number":
logging.info("XXXXXXXX")
strResult = "!!! Cross Reference !!!"

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

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

if xmlReferenceLabelText in dictEquations:
logging.info("Verweis auf Array gefunden:" + xmlReferenceLabelText)
strResult = dictEquations[xmlReferenceLabelText]
if xmlReferenceRefTarget in dictEquations:
logging.info("Verweis auf Equation gefunden:" + xmlReferenceRefTarget)
strResult = dictEquations[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictLists:
logging.info("Verweis auf Liste gefunden")
strResult = dictLists[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictChapters:
logging.info("Verweis auf Kapitel gefunden")
strResult = dictChapters[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictSections:
logging.info("Verweis auf Section gefunden")
strResult = dictSections[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictFigures:
logging.info("Verweis auf Abbildung gefunden")
strResult = dictFigures[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictFootnotes:
logging.info("Verweis auf Fussnote gefunden")
strResult = dictFootnotes[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictTheorems:
logging.info("Verweis auf Theorem gefunden")
strResult = dictTheorems[xmlReferenceRefTarget]
if xmlReferenceRefTarget in dictTables:
logging.info("Verweis auf Tabelle gefunden")
strResult = dictTables[xmlReferenceRefTarget]
tmpTail = xmlReference.tail or ""
#tmpTail = tmpTail.strip()
else:
logging.error("Found unknown reference type: %s. Exiting", reference_type)
sys.exit(0)
logging.info("XXXXXXXX")
xmlReference.clear()
if args.hyperimage and hitarget_id and reference_type in ["collage", "number"]:
hyperimage_link = f"{publication_landingpage[:-11]}/{intChapterNumber - 1}/index.html#{hitarget_id}"
xmlReference.tag = "a"
xmlReference.set("href", hyperimage_link)
else:
pass
xmlReference.text = strResult
xmlReference.tail = tmpTail

Expand Down Expand Up @@ -1760,8 +1807,8 @@ class FootnoteError(Exception):
xmlIndexentry.clear()
xmlIndexentry.tail = tmpTail
etree.strip_tags(xmlEbookTree, "EOAlabel", "EOAindex", "EOApageref", "EOAcitenumeric", "EOAtable", "EOAref", "note", "div", "div2", "div3", "div4", "div5", "citetext", "newpage", "EOAciteyear", "EOAtablelabel" , "hi", "pagebreak", "page", "pagestyle", "EOAcitation", "EOAciteauthoryear", "EOAcitemanual", "EOAprintbibliography", "EOAindexperson", "EOAprintindex", "EOAindexlocation", "EOAprintpersonindex", "EOAprintlocationindex","anchor", "temp", "EOAletterhead", "EOAhifigure", "EOAtocentry")
etree.strip_attributes(xmlEbookTree, "id-text", "noindent", "type", "label", "spacebefore", "rend") # also contained "id"
etree.strip_elements(xmlEbookTree, "citekey", with_tail=False)
etree.strip_attributes(xmlEbookTree, "id-text", "noindent", "type", "label", "spacebefore", "rend", "hielement") # also contained "id"
etree.strip_elements(xmlEbookTree, "citekey", "originalcontents", with_tail=False)

logging.info("Write every Part and Chapter into one file")
xmlChapters = xmlEbookTree.findall("//div1")
Expand Down

0 comments on commit 1c507db

Please sign in to comment.