From d51df3f3f8da08ca40e04bc2ca31ff139fe032af Mon Sep 17 00:00:00 2001 From: kthoden Date: Fri, 29 Mar 2019 15:47:00 +0100 Subject: [PATCH] Removing old solution --- eoatex2imxml.py | 235 +++++++++++++----------------------------------- 1 file changed, 62 insertions(+), 173 deletions(-) diff --git a/eoatex2imxml.py b/eoatex2imxml.py index ce61873..4319a0f 100755 --- a/eoatex2imxml.py +++ b/eoatex2imxml.py @@ -1098,153 +1098,81 @@ def cleanup(): with open(interim_bib_json_file, 'w') as ibjf: json.dump(citeproc_json.decode('utf-8'), ibjf) - - -#################### -# the old solution # -#################### -# # Copy interim .bbl-File to interim bib.tex file -# interim_bibtex_file = (args.filename) + "bib.tex" -# try: -# shutil.copy(BIBERFILE, interim_bibtex_file) -# except FileNotFoundError: -# print("%s has not been created yet. Switch TeX distribution to TeXLive2016, run biber_2.1 -O biber2-1.bbl %s to obtain this file" % (BIBERFILE, args.filename)) -# # Read all lines of Bibliographic TeX -# tmpFile = open(interim_bibtex_file, "r") -# tmpLines = tmpFile.readlines() -# tmpFile.close() - -# # First line should link to Bibliographic Praeambel -# tmpLines[0] = "\\include{%spre_bib}\n" % TEMPLATE_PATH -# # Remove unwanted lines -# for i in range(18,0,-1): -# del tmpLines[i] -# # Save changes -# tmpFile = open(interim_bibtex_file, "w") -# tmpFile.writelines(tmpLines) -# tmpFile.close() -# # TeX has been sanitized, now tralics to make it intermediate XML -# print("TeX has been sanitized, now tralics to make it intermediate XML") -# Kommando = "%s -log_file %s -confdir %s/tralics_conf -config %s/tralics.tcf -utf8 -utf8output -entnames=false %sbib.tex" % (TRALICS_PATH_EXEC, args.filename + "-bib-tralics.log", TRALICS_PATH_LIB, TRALICS_PATH_LIB, args.filename) -# Argumente = shlex.split(Kommando) -# Prozess = subprocess.call(Argumente) -# # Sanitize XML to make it useable -# tmpFile = open((args.filename) + "bib.xml", "r") -# tmpContent = tmpFile.read() -# tmpFile.close() -# listReplace = [ r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"", -# r"uniquename=(.*?),hash=(.*?)", -# r"hash=(.*?)", -# ] -# for strReplace in listReplace: -# tmpContent = re.sub(strReplace, "", tmpContent) - -# # Put Back Underscore _ -# tmpContent = re.sub(r"", "_", tmpContent) - -# # Remove empty Lines -# tmpContent = re.sub(r"\n\n", "\n", tmpContent) - -# # Put back Ampersand -# tmpContent = re.sub(r"&", "&", tmpContent) -# tmpFile = open((args.filename) + "bib.xml", "w") -# tmpFile.write(tmpContent) -# tmpFile.close() - -# # TeXML has been sanitized, now load xml-Tree -# xmlParser2 = etree.XMLParser(no_network=False,load_dtd=False) -# xmlBibTree = etree.parse((args.filename + "bib.xml"), xmlParser2) -# xml_bib_entries = xmlBibTree.findall(".//entry") - -########################### -# end of the old solution # -########################### - make_bibchecker(bib_database, set_citations) # If Bibliography-Type is monograph search for EOAbibliography and make it all if bib_type == "monograph": if xmlTree.find(".//EOAprintbibliography") is not None: - # to insert here: with keywords we can have multiple bibliographies - xmlBibliography = xmlTree.find(".//EOAprintbibliography") - bibliography_keyword = xmlBibliography.get("keyword") - if bibliography_keyword: - logging.debug(f"Found bibliography keyword {bibliography_keyword}") + xmlBibliographies = xmlTree.findall(".//EOAprintbibliography") + logging.debug(f"Found {libeoaconvert.plural(len(xmlBibliographies), 'bibliography', plural='bibliographies')}.") + if len(xmlBibliographies) > 1: + for xmlBibliography in xmlBibliographies: + bibliography_keyword = xmlBibliography.get("keyword") + if bibliography_keyword: + logging.debug(f"Found bibliography keyword {bibliography_keyword}") + else: + logging.error(f"No bibliography keyword found. Since there is more than one bibliography, all bibliographies are required to have a keyword. Exiting ") + sys.exit() else: - logging.debug(f"No bibliography keyword found") + if xmlBibliographies[0].get("keyword"): + logging.warning("Found a keyword in bibliography although there is only one.") + + # find all citekeys in publication + citekeys = xmlTree.xpath(".//citekey/text()") + nocite_elements = xmlTree.xpath(".//nocite") + + if nocite_elements: + logging.debug(f"Found {libeoaconvert.plural(len(nocite_elements), 'nocite command')}.") + nocitekeys = [] + for nocite in nocite_elements: + keys = [x.strip() for x in nocite.text.split(",")] + nocitekeys += keys + nocite.tag = "elementtobestripped" + logging.debug(f"Found {libeoaconvert.plural(len(set(nocitekeys)), 'nocite key')}.") + logging.debug(f"Adding nocite keys to the other cite keys.") + citekeys += nocitekeys + + # create a formatted file for all references + tmp_citation_filename = f"used_citations-monograph" + citations_to_format = set(citekeys) + formatted_references = libeoaconvert.format_citations(citations_to_format, bib_database + ".bib", strLanguage, tmp_citation_filename)[0] - xmlBibliography.clear() - xmlBibliography.tag = "div" + # printbibliographydict = {} - bib_parent = xmlBibliography.getparent() - bib_parent.tag = "div" + for xmlBibliography in xmlBibliographies: + bibliography_keyword = xmlBibliography.get("keyword") + xmlBibliography.clear() + xmlBibliography.tag = "div" + if bibliography_keyword: + xmlBibliography.set("keyword", bibliography_keyword) - xmlBibliographyDiv = etree.Element("div") - xmlBibliography.addnext(xmlBibliographyDiv) - ############### - # new version # - ############### - tmp_citation_filename = "used_citations-monograph" - citations_to_format = [x["id"] for x in citations_json if x["keyword"] == bibliography_keyword] - formatted_references = libeoaconvert.format_citations(citations_to_format, bib_database + ".bib", strLanguage, tmp_citation_filename)[0] - fixed_entries = libeoaconvert.fix_bib_entries(formatted_references) - for entry in fixed_entries: - xmlBibliographyDiv.append(entry) - ###################### - # end of new version # - ###################### + bib_parent = xmlBibliography.getparent() + bib_parent.tag = "div" - ############### - # old version # - ############### - # xml_bib_entries = xmlBibTree.findall(".//entry") - # intNumberOfEntry = 0 - # for xmlEntry in xml_bib_entries: - # if intNumberOfEntry == 0: - # # Don't check for previous author if first entry of the Bibliography - # bibEntry = Bibitem(xmlEntry) - # strNewentry = "

" + createBibEntryAuthorYear(bibEntry, boolSameAuthor=False) + "

" - # else: - # bibEntry = Bibitem(xmlEntry) - # # Check if author of previous Entry is the same - # bibEntryPrevious = Bibitem(xml_bib_entries[intNumberOfEntry - 1]) - # if bibEntry.fullauthorlastfirst()[0] == bibEntryPrevious.fullauthorlastfirst()[0]: - # strNewentry = "

" + createBibEntryAuthorYear(bibEntry, boolSameAuthor=True) + "

" - # elif bibEntryPrevious.fullauthorlastfirst()[0] == bibEntry.fullauthorlastfirst()[0]: - # strNewentry = "

" + createBibEntryAuthorYear(bibEntry, boolSameAuthor=True) + "

" - # elif bibEntry.fullauthorlastfirst()[0] == bibEntryPrevious.fullauthorlastfirst()[0]: - # strNewentry = "

" + createBibEntryAuthorYear(bibEntry, boolSameAuthor=True) + "

" - # else: - # strNewentry = "

" + createBibEntryAuthorYear(bibEntry, boolSameAuthor=False) + "

" - - # xmlNew = etree.fromstring(strNewentry) - # next one writes the bibliography into the document - # xmlBibliography.append(xmlNew) - - # intNumberOfEntry += 1 - ###################### - # end of old version # - ###################### + # xmlBibliographyDiv = etree.Element("divxbd") + # if bibliography_keyword: + # xmlBibliographyDiv.set("keyword", bibliography_keyword) + # xmlBibliography.addnext(xmlBibliographyDiv) + # create bibliographies per keyword + if bibliography_keyword: + tmp_citation_filename = f"used_citations-monograph_{bibliography_keyword}" + logging.info("We want to collect the entries matching the keywords from the database.") + + citations_with_keyword = [x["id"] for x in citations_json if bibliography_keyword in x["keyword"]] + logging.debug(f"Found {libeoaconvert.plural(len(citations_with_keyword), 'citation')} with keyword {bibliography_keyword} in database.") + + citations_to_format = [x for x in citations_with_keyword if x in citekeys] + logging.debug(f"Found {libeoaconvert.plural(len(citations_to_format), 'citation')} with keyword {bibliography_keyword} that are actually cited.") + + formatted_references = libeoaconvert.format_citations(citations_to_format, bib_database + ".bib", strLanguage, tmp_citation_filename)[0] + + fixed_entries = libeoaconvert.fix_bib_entries(formatted_references) + for entry in fixed_entries: + xmlBibliography.append(entry) + # printbibliographydict[bibliography_keyword] = xmlBibliographyDiv # If Bibliography-Type is anthology search for EOAbibliography and make one per chapter if bib_type == "anthology": @@ -1400,45 +1328,6 @@ def cleanup(): ################### # If Bibliography-Type is monograph find all entries, forget about refsection - ############### - # old version # - ############### - """ - if bib_type == "monograph": - xml_bib_entries = xmlBibTree.findall(".//entry") - for xmlEntry in xml_bib_entries: - bibEntry = Bibitem(xmlEntry) - if bibEntry.citekey() == xmlCitation.find("./citekey").text: - if xmlCitation.tag == "EOAciteauthoryear": - strCitation = bibEntry.shortauthor() + " " + bibEntry.labelyear() - if bibEntry.labelyearsuffix() is not None: - strCitation = strCitation + bibEntry.labelyearsuffix() - strTitle = bibEntry.title() - if xmlCitation.tag == "EOAciteyear": - strCitation = bibEntry.labelyear() - if bibEntry.labelyearsuffix() is not None: - strCitation = strCitation + bibEntry.labelyearsuffix() - strTitle = bibEntry.title() - if xmlCitation.tag == "EOAcitemanual": - cite_text = xmlCitation.find("citetext") - if cite_text.getchildren(): - tmp_string = xmlCitation.find("citetext") - tmp_string = cite_text.getchildren()[0] - strCitation = etree.tostring(tmp_string) - # BAUSTELLE!!!!! - # tmp_string2 = etree.tostring(tmp_string) - # tmp_string3 = tmp_string2.decode() - # strCitation = tmp_string3.replace("<", "<") - else: - strCitation = xmlCitation.find("citetext").text - strTitle = bibEntry.title() - if xmlCitation.find("./page") is not None and xmlCitation.find("./page").text is not None: - strCitation = strCitation + ", " + xmlCitation.find("./page").text - """ - ###################### - # end of old version # - ###################### - ############### # new version # ###############