Skip to content

Commit

Permalink
Support for images in footnotes
Browse files Browse the repository at this point in the history
  • Loading branch information
kthoden committed Apr 6, 2020
1 parent bc43342 commit 587db53
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/imxml2django.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,9 +1419,24 @@ def bring_footnote_down_django(footnote, fragment, footnote_number, object_numbe
surrounding_p.append(xmlElement)
elif xmlElement.tag == "span":
surrounding_p.append(xmlElement)

elif xmlElement.tag == "EOAfigurenonumber":
surrounding_p = etree.fromstring("""<img/>""")
strImageFileString = xmlElement.find(".//file").text
strImageFileString = strImageFileString.rstrip("\n")
strImageFileDir = os.path.dirname(strImageFileString)
strImageFileDir = re.sub("/", "", strImageFileDir)
strImageFileName = os.path.basename(strImageFileString)
strImageFileNamewoSuffix = os.path.splitext(strImageFileName)[0]
shutil.copy(
PUBLICATION_DIR / strImageFileString,
OUTPUT_DIR / "images" / "embedded" / (strImageFileDir + strImageFileName)
)
surrounding_p.set("src", strImageFileDir + strImageFileName)
surrounding_p.set("width", xmlElement.find(".//width").text + "%;")
xmlElement = surrounding_p
else:
pass
logging.debug("Footnote paragraph")
xmlEOAfootnote.append(xmlElement)
xmlResult.append(xmlEOAfootnote)
intFootnoteNumber += 1
Expand Down
31 changes: 31 additions & 0 deletions src/tei2imxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,37 @@ def handle_refs_default(ref):

etree.strip_elements(figure, "{%s}graphic" % ns_tei)

###################
# Inline graphics #
###################

# if no scale or height is given, treat it as an EOAinline graphic, else consider it a small image,
# for example in a footnote
eoa_graphics = xml_tree.xpath("//t:graphic[not(ancestor::t:figure)]", namespaces=NS_MAP)
for graphic in eoa_graphics:
graphic_attributes = dict(graphic.attrib).copy()
graphic_tail = graphic.tail
graphic.clear()
# only url attribute
if len(graphic_attributes) == 1:
graphic.tag = "EOAinline"
graphic.text = graphic_attributes["url"]
elif len(graphic_attributes) == 2:
graphic_parent = graphic.getparent()
graphic_siblings = graphic_parent.getchildren()
if graphic_parent.tag == "p" and len(graphic_siblings) == 1:
graphic_parent.tag = "EOAfigurenonumber"
graphic.tag = "p"
graphic_url = etree.SubElement(graphic, "file").text = graphic_attributes["url"]
try:
size_parameter = graphic_attributes["scale"]
except:
size_parameter = graphic_attributes["height"]
graphic_size = etree.SubElement(graphic, "width").text = size_parameter
else:
logging.error("Illegal circumstances for a non-numbered figure. It should be the only element in a paragraph. Exiting.")
sys.exit(1)

##############
# Hi-Element #
##############
Expand Down

0 comments on commit 587db53

Please sign in to comment.