From 587db53ffa7cffe8471babd2cbc3dfd9904db1fb Mon Sep 17 00:00:00 2001 From: kthoden Date: Mon, 6 Apr 2020 15:42:45 +0200 Subject: [PATCH] Support for images in footnotes --- src/imxml2django.py | 17 ++++++++++++++++- src/tei2imxml.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/imxml2django.py b/src/imxml2django.py index 795c526..6be5b2a 100755 --- a/src/imxml2django.py +++ b/src/imxml2django.py @@ -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("""""") + 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 diff --git a/src/tei2imxml.py b/src/tei2imxml.py index 8146af5..7688d46 100755 --- a/src/tei2imxml.py +++ b/src/tei2imxml.py @@ -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 # ##############