From c02135d027dba92bc6dca9f3dff743345b49ecea Mon Sep 17 00:00:00 2001 From: kthoden Date: Wed, 6 Nov 2019 15:28:59 +0100 Subject: [PATCH] Insert id, irrespective of nonumber status --- idassigner.py | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/idassigner.py b/idassigner.py index 9dec660..2dea957 100644 --- a/idassigner.py +++ b/idassigner.py @@ -46,29 +46,21 @@ def assign_ids(chapter_tree, elements): sections = chapter_tree.xpath("//t:div[@type='section']", namespaces=NS_MAP) section_id_counter = 1 for section in sections: - if section.get("n") == "nonumber": - logging.info("Leaving out unnumbered section.") - pass - else: - section_id = "{}_sec{:02d}".format(chapter_id, section_id_counter) - libeoaconvert.assign_xml_id(section, section_id) - section_id_counter += 1 + section_id = "{}_sec{:02d}".format(chapter_id, section_id_counter) + libeoaconvert.assign_xml_id(section, section_id) + section_id_counter += 1 if "sections" in elements: subsections = chapter_tree.xpath("//t:div[@type='subsection']", namespaces=NS_MAP) subsection_id_counter = 1 for subsection in subsections: - if subsection.get("n") == "nonumber": - logging.info("Leaving out unnumbered subsection.") - pass - else: - section_element = subsection.getparent() - section_id = section_element.attrib["{http://www.w3.org/XML/1998/namespace}id"] - logging.debug("Found a subsection in section %s", section_id) - rest, section_number = section_id.split("_sec") - subsection_id = "{}_subsec{}-{:02d}".format(chapter_id, section_number, subsection_id_counter) - libeoaconvert.assign_xml_id(subsection, subsection_id) - subsection_id_counter += 1 + section_element = subsection.getparent() + section_id = section_element.attrib["{http://www.w3.org/XML/1998/namespace}id"] + logging.debug("Found a subsection in section %s", section_id) + rest, section_number = section_id.split("_sec") + subsection_id = "{}_subsec{}-{:02d}".format(chapter_id, section_number, subsection_id_counter) + libeoaconvert.assign_xml_id(subsection, subsection_id) + subsection_id_counter += 1 if "figures" in elements: figures = chapter_tree.xpath("//t:figure", namespaces=NS_MAP) @@ -119,13 +111,13 @@ def main(): print(selected_chapters) chapters = [] for xml_chapter in selected_chapters: - chapter = xml_tree.xpath(f"//t:div[@xml:id='{xml_chapter}' and not(@n='nonumber')]", namespaces=NS_MAP)[0] + chapter = xml_tree.xpath(f"//t:div[@xml:id='{xml_chapter}'", namespaces=NS_MAP)[0] copied_chapter = deepcopy(chapter) assign_ids(copied_chapter, elements=list_of_elements) chapter.addprevious(copied_chapter) chapter.tag = "elementtobestripped" else: - chapters = xml_tree.xpath("//t:div[@type='chapter' and not(@n='nonumber')]", namespaces=NS_MAP) + chapters = xml_tree.xpath("//t:div[@type='chapter']", namespaces=NS_MAP) logging.debug("Found %s chapters.", len(chapters)) # in this iteration, a copy is made of each chapter and fitted # with ids, the original chapter is being discarded