diff --git a/fix_tei.py b/fix_tei.py index 060d019..f0337ad 100644 --- a/fix_tei.py +++ b/fix_tei.py @@ -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""" @@ -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.") @@ -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 = {} @@ -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