Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New functions for just handling chapters
  • Loading branch information
Klaus Thoden committed Nov 22, 2018
1 parent 254a81b commit ce29a6d
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions fix_tei.py
Expand Up @@ -217,6 +217,21 @@ def validate_citations(used_citekeys, bibdata):
return no_citekey
# def validate_citations ends here

def insert_bib_section(xml_tree):
"""Insert a section containing a PI for bibliography"""

bib_pi = etree.ProcessingInstruction("eoa", "printbibliography")
bib_div = etree.Element("div", type="section", n="nonumber")
bib_head = etree.SubElement(bib_div, "head").text = "References"
bib_div.append(bib_pi)

all_children = xml_tree.getchildren()
last_child = all_children[-1]
last_child.addnext(bib_div)

return
# def insert_bib_section ends here

def convert_figures(string):
"""Find figures shorthands"""

Expand Down Expand Up @@ -530,8 +545,11 @@ def main():

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dochighestorder", default='chapter', help="Specify which divider is at the highest level, possible values: part, chapter. Default is chapter.")
parser.add_argument("-p", "--bibtexparserlog", help="Display logging output of bibtexparser", action="store_true")
parser.add_argument("-f", "--finalize", help="Finalize a publication.", action="store_true")
parser.add_argument("-b", "--bibtype", help="Specify the type of bibliography, possible values: anthology, monograph.", default="monograph")
parser.add_argument("-c", "--chapter", help="Treat the TEI as one chapter, discards header.", action="store_true")
parser.add_argument("-a", "--addbibliography", help="Add a section with bibliography PI.", action="store_true")
parser.add_argument("teifile", help="Output from oxgarage/metypeset, an TEI XML file.")
parser.add_argument("bibfile", help="The bibliography database of the publication.")
parser.add_argument("figdir", help="The directory that contains the figures belonging to the publication.")
Expand Down Expand Up @@ -607,14 +625,35 @@ def main():
bad_pageref = parse_cited_range(all_references)
report["bad_pageref"] = bad_pageref

tei_header = xml_tree2.xpath("//t:teiHeader", namespaces=NS_MAP)
fix_tei_header(tei_header[0], str(args.bibfile), str(args.bibtype))
if args.chapter:
tei_text = xml_tree2.xpath("//t:body", namespaces=NS_MAP)[0]
body_element = tei_text.xpath("//t:body", namespaces=NS_MAP)[0]
body_element.tag = "div"
body_element.set("type", "chapter")

etree.strip_tags(xml_tree2, "tagtobestripped")

if args.addbibliography:
logging.debug("Adding bib")
insert_bib_section(body_element)
else:
logging.debug("Not adding bib")
pass

tei_text = xml_tree2.xpath("/t:TEI/t:text", namespaces=NS_MAP)[0]
tei_front_part = add_tei_frontpart()
tei_text.insert(0, tei_front_part)
output = args.teifile.replace(".xml", "-out.xml")
tree = etree.ElementTree(body_element)
tree.write(output, pretty_print=True, xml_declaration=True,encoding="utf-8")
logging.info("Wrote %s." % output)

else:
tei_header = xml_tree2.xpath("//t:teiHeader", namespaces=NS_MAP)
fix_tei_header(tei_header[0], str(args.bibfile), str(args.bibtype))

etree.strip_tags(xml_tree2, "tagtobestripped")
tei_text = xml_tree2.xpath("/t:TEI/t:text", namespaces=NS_MAP)[0]
tei_front_part = add_tei_frontpart()
tei_text.insert(0, tei_front_part)

etree.strip_tags(xml_tree2, "tagtobestripped")

dictChapters = {}
dictEquations = {}
Expand All @@ -639,15 +678,17 @@ def main():
'pagelabeldict' : dictPagelabels}

with open('tmp_files/data.pickle', 'wb') as f:
# Pickle the 'data' dictionary using the highest protocol available.
pickle.dump(data_to_pickle, f, pickle.HIGHEST_PROTOCOL)

fix_document_structure(xml_tree2, highest_level)
# output
output = args.teifile.replace(".xml", "-out.xml")
tree = etree.ElementTree(xml_tree2)
tree.write(output, pretty_print=True, xml_declaration=True,encoding="utf-8")
logging.info("Wrote %s." % output)
if args.chapter:
pass
else:
output = args.teifile.replace(".xml", "-out.xml")
tree = etree.ElementTree(xml_tree2)
tree.write(output, pretty_print=True, xml_declaration=True,encoding="utf-8")
logging.info("Wrote %s." % output)

if args.finalize:
pass
Expand Down

0 comments on commit ce29a6d

Please sign in to comment.