Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Figures in epub
  • Loading branch information
Klaus Thoden committed May 29, 2018
1 parent a0ccbe2 commit 6ee0a6e
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions imxml2epub.py
Expand Up @@ -483,7 +483,7 @@ def addToTocncx(tocncx, Label, intTechnicalChapterNumber):

print("-----------------------------------------------------")
print("Preparing Figures")
xmlFigures = xmlEbookTree.xpath(".//EOAfigure | .//EOAlsfigure")
xmlFigures = xmlEbookTree.xpath(".//EOAfigure[not(@type='hionly')] | .//EOAlsfigure[not(@type='hionly')]")
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 All @@ -493,11 +493,23 @@ def addToTocncx(tocncx, Label, intTechnicalChapterNumber):
# Remove / from path
strImageFileDir = re.sub("/", "", strImageFileDir)
strImageFileName = os.path.basename(strImageFileString)
strImageFileNamewoSuffix = os.path.splitext(strImageFileName)[0]
strImageFileNamewoSuffix, strImageFileName_Suffix = os.path.splitext(strImageFileName)
shutil.copy(os.getcwd() + "/" + strImageFileString, os.getcwd() + "/CONVERT/epub/OEBPS/images/" + strImageFileDir + strImageFileName)
if strImageFileName_Suffix.lower() == ".jpg":
extension_and_mime = "jpg"
elif strImageFileName_Suffix.lower() in [".png", ".pdf"]:
extension_and_mime = "png"
else:
print("Found an unrecognized image suffix: %s" % strImageFileName_Suffix)
sys.exit()

strImageFilepath = libeoaconvert.sanitizeImage(os.getcwd() + "/CONVERT/epub/OEBPS/images/" + strImageFileDir + strImageFileName, GM_PATH, TL_PATH)
# Add copied file to contentopf
contentopf = addToContentopf(contentopf, "images/" + strImageFileDir + strImageFileNamewoSuffix + ".jpg", strImageFileDir + strImageFileNamewoSuffix + "jpg", "jpg")
content_opf_filename = "images" + os.path.sep + "{}{}.{}".format(strImageFileDir, strImageFileNamewoSuffix, extension_and_mime)
content_opf_fileid = "{}{}{}".format(strImageFileDir, strImageFileNamewoSuffix, extension_and_mime)

contentopf = addToContentopf(contentopf, content_opf_filename, content_opf_fileid, extension_and_mime)

xmlFigureCaption = xmlFigure.find(".//caption")
idFigure = xmlFigure.find(".//anchor").get("id")
intFigureNumber = dictFigures[idFigure]
Expand All @@ -509,7 +521,7 @@ def addToTocncx(tocncx, Label, intTechnicalChapterNumber):
xmlFigure.clear()
xmlFigure.tag = "p"
xmlFigureImage = etree.Element("img")
xmlFigureImage.set("src", "images/" + strImageFileDir + strImageFileNamewoSuffix + ".jpg")
xmlFigureImage.set("src", "images/" + strImageFileDir + strImageFileNamewoSuffix + "." + extension_and_mime)
xmlFigureImage.set("alt", "")
xmlFigureImage.set("style", "width: " + strImageWidth + "%")
xmlFigure.append(xmlFigureImage)
Expand All @@ -521,6 +533,11 @@ def addToTocncx(tocncx, Label, intTechnicalChapterNumber):
# 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))
for fig in xml_figures_hyperimage:
fig.tag = "EOAhifigure"

print("-----------------------------------------------------")
print("Preparing not numbered Figures")
xmlFigures = xmlEbookTree.findall(".//EOAfigurenonumber")
Expand All @@ -536,7 +553,8 @@ def addToTocncx(tocncx, Label, intTechnicalChapterNumber):
shutil.copy(os.getcwd() + "/" + strImageFileString, os.getcwd() + "/CONVERT/epub/OEBPS/images/" + strImageFileDir + strImageFileName)
strImageFilepath = libeoaconvert.sanitizeImage(os.getcwd() + "/CONVERT/epub/OEBPS/images/" + strImageFileDir + strImageFileName, GM_PATH, TL_PATH)
# Add copied file to contentopf
contentopf = addToContentopf(contentopf, "images/" + strImageFileDir + strImageFileNamewoSuffix + ".jpg", strImageFileDir + strImageFileNamewoSuffix + "jpg", "jpg")
contentopf = addToContentopf(contentopf, "images/" + strImageFileDir + strImageFileNamewoSuffix + ".jpg", strImageFileDir + strImageFileNamewoSuffix + "-nonumber-jpg", "jpg")
logging.debug("Added a nonumber figure")
strImageWidth = xmlFigure.find(".//width").text
strImageWidth = strImageWidth.rstrip("\n")
xmlFigure.clear()
Expand Down Expand Up @@ -626,17 +644,14 @@ def bring_footnote_down_epub(footnote, footnote_name, destination):
kids = list(footnote.getchildren())
prefix = "[%s]" % footnote_name

print("hier 0" + prefix)
# we would like to prepend this footnote identifier to the footnote element
if footnote.text is not None:
print("hier 1" + prefix)
# if the element starts with some text anyway, prepend it there
# footnote.text = "%s %s" % (prefix, footnote.text)

pass
else:
# if, however, the element begins with a child, prepend the text at the beginning of the first child instead
if len(kids):
print("hier 2" + prefix)
first_child = kids[0]
# child_text = prefix
child_text = ""
Expand All @@ -649,7 +664,6 @@ def bring_footnote_down_epub(footnote, footnote_name, destination):
child_text += child_suffix
first_child.text = child_text
else:
print("hier 3" + prefix)
# a totally empty footnote is weird, but who am I to judge?
footnote.text = prefix

Expand All @@ -666,7 +680,6 @@ def bring_footnote_down_epub(footnote, footnote_name, destination):
# append any text the footnote used to have to the destination
destkids = list(destination.getchildren())
if len(destkids):
print("hier 4")
# if the destination has children, append after the last one's tail
last_kid = destkids[-1]
prefix = last_kid.tail
Expand All @@ -676,7 +689,6 @@ def bring_footnote_down_epub(footnote, footnote_name, destination):
prefix += " "
last_kid.tail = prefix + footnote_text
else:
print("hier 5")
# if the destination has no children, append to its text
prefix = destination.text
if prefix is None:
Expand Down Expand Up @@ -981,8 +993,8 @@ class FootnoteError(Exception):

strInlineElementFileName = os.path.basename(strInlineElementFilePath)
strInlineElementDirName = os.path.dirname(strInlineElementFilePath)
strNewImagePath = os.getcwd() + "/CONVERT/epub/OEBPS/images/" + strInlineElementDirName + strInlineElementFileName

strNewImagePath = os.getcwd() + "/CONVERT/epub/OEBPS/images/" + strInlineElementDirName + strInlineElementFileName
# trouble when there are subdirectories in Image path!
# some thing goes wrong here: <EOAinline>Images/png_300dpi/A.png</EOAinline>

Expand Down

0 comments on commit 6ee0a6e

Please sign in to comment.