From a897d797d43861126f8695df6c4a01c656bd6f2a Mon Sep 17 00:00:00 2001 From: kthoden Date: Fri, 4 Sep 2020 19:15:18 +0200 Subject: [PATCH] Implement EOAbox (grey infoboxes) for Django --- src/eoatex2imxml.py | 34 ++++++++++++++++++++++++++++++++++ src/imxml2django.py | 33 +++++++++------------------------ 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/eoatex2imxml.py b/src/eoatex2imxml.py index 702f2c7..23376a0 100755 --- a/src/eoatex2imxml.py +++ b/src/eoatex2imxml.py @@ -1071,6 +1071,36 @@ def process_pararefs(xmlTree): pararef.tag = "elementtobestripped" # def process_pararefs ends here + +def process_eoabox(xmlChapters): + """Move contents of EOAbox into regular document structure + + The EOAbox element is the child of a p tag and contains itself p + tags + """ + + all_boxes = xmlTree.findall(".//EOAbox") + if len(all_boxes) == 0: + logging.info("Document contains no info boxes.") + else: + logging.info(f"Found {libeoaconvert.plural(len(all_boxes), 'box', plural='boxes')}.") + + for box in all_boxes: + box_parent = box.getparent() + box_parent.tag = "elementtobestripped" + box_contents = list(box) + box_head = box_contents[0].find("head") + box_head.set("style", "boxhead") + box_head.tag = "p" + libeoaconvert.remove_wrapping_element(box_head.getparent()) + for contents in box_contents[1:]: + contents.set("style", "box") + + libeoaconvert.remove_wrapping_element(box) + libeoaconvert.remove_wrapping_element(box_parent) + return +# def process_eoabox ends here + def make_indices_child_of_div0(): """Move index commands to a higher location""" @@ -1374,6 +1404,10 @@ def add_bibliography_to_xml( logging.info("Converting EOAchem") process_eoachem( xmlChapters ) +logging.info("-----------------------------------------------------") +logging.info("Converting EOAbox") +process_eoabox( xmlChapters ) + logging.info("-----------------------------------------------------") logging.info("EOAFigure Numbering per Chapter") dictFigures = process_figures( xmlChapters ) diff --git a/src/imxml2django.py b/src/imxml2django.py index b04eb20..f859a90 100755 --- a/src/imxml2django.py +++ b/src/imxml2django.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8; mode: python -*- -# Time-stamp: <2020-09-02 13:33:37 (kthoden)> +# Time-stamp: <2020-09-04 19:15:08 (kthoden)> """ Create an XML file that can be inserted into the Django database @@ -806,30 +806,15 @@ def djangoParseObject(xmlElement, indent=False, listtype=None, listnumber=0, uid xmlResult.append(copied_line) etree.strip_tags(xmlResult, "p") - elif xmlElement.tag == "EOAbox": - logging.debug("Found a box") - xmlResult = etree.Element("temp") - xmlResult.set("style", "box") - - box_header = xmlElement.find("head") - box_header.tag = "EOAparagraph" - box_header.set("style", "box") - box_header.set("order", str(intObjectNumber)) - head_contents = box_header.find("p") - head_contents.tag = "b" - # etree.strip_tags(box_header, "p") - xmlResult.append(box_header) + elif xmlElement.tag == "head" and xmlElement.get("style") == "boxhead": + xmlElement.tag = "b" + del xmlElement.attrib["style"] + wrapping_paragraph = etree.Element("EOAparagraph") + wrapping_paragraph.set("style", "box") + libeoaconvert.wrap_into_element(wrapping_paragraph, xmlElement) + wrapping_paragraph.set("order", str(intObjectNumber)) intObjectNumber += 1 - # question: what to do about paragraph equivalent objects? - box_elements = xmlElement.getchildren() - logging.debug(len(box_elements)) - for box_element in box_elements: - if box_element.tag == "p": - box_element.tag = "EOAparagraph" - box_element.set("style", "box") - box_element.set("order", str(intObjectNumber)) - xmlResult.append(box_element) - intObjectNumber += 1 + xmlResult = wrapping_paragraph elif xmlElement.tag == "p" and xmlElement.get("class") == "divider": xmlElement.tag = "EOAparagraph" xmlElement.set("order", str(intObjectNumber))