Skip to content

Commit

Permalink
Insert id, irrespective of nonumber status
Browse files Browse the repository at this point in the history
  • Loading branch information
kthoden committed Nov 6, 2019
1 parent fb6af94 commit c02135d
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions idassigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c02135d

Please sign in to comment.