Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Removing old solution
  • Loading branch information
kthoden committed Mar 29, 2019
1 parent d5cc216 commit d51df3f
Showing 1 changed file with 62 additions and 173 deletions.
235 changes: 62 additions & 173 deletions eoatex2imxml.py
Expand Up @@ -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"<math mode='display' xmlns='http://www.w3.org/1998/Math/MathML'>",
# r"<formula textype='displaymath' type='display'>",
# r"<mi>",
# r"</mi>",
# r"<mn>",
# r"<mn>",
# r"<mo>",
# r"</mo>",
# r"<mn>",
# r"</mn>",
# r"<mrow/>",
# r"<msup>",
# r"</msup>",
# r"</math>",
# r"</formula>",
# r"<formula type='inline'>",
# r"<math xmlns='http://www.w3.org/1998/Math/MathML'>",
# r"<formula textype='math' type='inline'>",
# r"<mrow>uniquename=(.*?),hash=(.*?)</mrow>",
# r"<mrow>hash=(.*?)</mrow>",
# ]
# for strReplace in listReplace:
# tmpContent = re.sub(strReplace, "", tmpContent)

# # Put Back Underscore _
# tmpContent = re.sub(r"<error n='_' l='(.*?)' c='Missing dollar'/>", "_", tmpContent)

# # Remove empty Lines
# tmpContent = re.sub(r"\n\n", "\n", tmpContent)

# # Put back Ampersand
# tmpContent = re.sub(r"&", "&amp;", 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 = "<p class=\"bibliography\">" + createBibEntryAuthorYear(bibEntry, boolSameAuthor=False) + "</p>"
# 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 = "<p class=\"bibliography\">" + createBibEntryAuthorYear(bibEntry, boolSameAuthor=True) + "</p>"
# elif bibEntryPrevious.fullauthorlastfirst()[0] == bibEntry.fullauthorlastfirst()[0]:
# strNewentry = "<p class=\"bibliography\">" + createBibEntryAuthorYear(bibEntry, boolSameAuthor=True) + "</p>"
# elif bibEntry.fullauthorlastfirst()[0] == bibEntryPrevious.fullauthorlastfirst()[0]:
# strNewentry = "<p class=\"bibliography\">" + createBibEntryAuthorYear(bibEntry, boolSameAuthor=True) + "</p>"
# else:
# strNewentry = "<p class=\"bibliography\">" + createBibEntryAuthorYear(bibEntry, boolSameAuthor=False) + "</p>"

# 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":
Expand Down Expand Up @@ -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("&lt;", "<")
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 #
###############
Expand Down

0 comments on commit d51df3f

Please sign in to comment.