-
Liliana Sanfilippo authoredLiliana Sanfilippo authored
cit.py 9.42 KiB
# Voraussetzungen
#- Python 3.x
#- bibtexparser (Installieren Sie es mit `pip install bibtexparser`)
problemlist = []
def main():
print("Starting program...")
try:
import bibtexparser
except ImportError:
print("The package 'bibtexparser' is not installed. Install with: pip install bibtexparser")
exit(1)
try:
import argparse
except ImportError:
print("The package 'argparse' is not installed.")
exit(1)
try:
import re
except ImportError:
print("The package 're' is not installed.")
exit(1)
try:
#reading command line input and parsing it
parser = argparse.ArgumentParser(
prog='HTML Citations',
description='create acessible HTML Citations from bib files')
parser.add_argument('-i','--input', required=True, help="path to source bib")
parser.add_argument('-s','--startnumber', required=True, help="number from which to start numbering")
args = parser.parse_args()
print("Source: " + args.input)
except argparse.ArgumentError as e:
print(f"Argument parsing error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
print("Reading file...")
#reading and parsing the file
try:
with open(args.input, 'r') as file:
file_content = file.read()
print("Parsing file...")
try:
library = bibtexparser.loads(file_content)
#opening output file
try:
with open('output.txt', 'w') as out:
length = len(library.entries)
print("found " + str(length) + " entries")
ran = range(length)
startnum = int(args.startnumber);
count = 0;
#processing every entry and writing the dictionary for it
for x in ran:
print("\n Initializing empty dictionary for entry "+ str(startnum+count) + "...")
dictio = {}
en_x = library.entries[x]
print("Filling dictionary for entry "+ str(startnum+count) + "")
# Direkt auf die Einträge zugreifen, da es sich um ein Dictionary handelt
for key, value in en_x.items():
key_low = key.lower()
dictio[key_low] = value
# Überprüfung auf den Typ des Eintrags über 'ENTRYTYPE'
if en_x['ENTRYTYPE'] == "article":
articleHTML(dictio, (startnum+count), out)
elif en_x['ENTRYTYPE'] == "misc":
miscHTML(dictio, (startnum+count), out)
elif en_x['ENTRYTYPE'] == "book":
bookHTML(dictio, (startnum+count), out)
count += 1;
except Exception as e:
print(f"An unexpected error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
except FileNotFoundError:
print(f"Error: The file '{args.input}' was not found.")
if len(problemlist)>0:
print("- - - - - - - - - - - - - - - - - ")
print("REMAINING ERRORS:")
for p in problemlist:
print(p)
else:
print("DONE")
def articleHTML(dictio, x, out):
print("Writing html code for entry "+ str(x) + "...")
out.write("{/*<!-- Citation num " + str(x) + "--> */}" + "\n")
out.write("<li typeof=\"schema:ScolarlyArticle\" role=\"doc-biblioentry\" property=\"schema:citation\" id=\"desc-" + str(x) + "\">"+ "\n")
out.write("\t" + "<span property=\"schema:author\" typeof=\"schema:Person\">"+ "\n")
print("Just a sec, seperating authors...")
authors = dictio['author']
authors = authors.replace(" and ", "|")
liste = authors.split("|")
for a in liste:
try:
#print("processing " + a)
first = None
last = None
name = None
if ',' in a:
s = a.split(", ")
first = s[1]
first_sh = first[0]
last = s[0]
name = last + ", " + first_sh + "."
else:
s = a.split()
if len(s) == 2:
first = s[0]
first_sh = first[0]
last = s[1]
name = last + ", " + first_sh + "."
else:
leng = len(s)
last = s[leng-1]
first = ''
for n in s:
if n != s[-1]:
first = first + n[0] + '.'
name = last + ", " + first
if a == liste[-1]:
out.write("\t" + "\t" +"<span property=\"schema:Name\"> " +name + "</span>"+ "\n")
else:
out.write("\t" +"\t" +"<span property=\"schema:Name\"> " +name + "</span>;"+ "\n")
except Exception as e:
print(f"An unexpected error occurred: {e} see " + a)
out.write("\t" +"</span>"+ "\n")
out.write("\t" + "<span property=\"schema:name\"> "+dictio['title']+ ". </span>"+ "\n")
out.write("\t" +"<i property=\"schema:publisher\" typeof=\"schema:Organization\"> "+ dictio['journal'] +"</i>"+ "\n")
out.write("\t" +"<b property=\"issueNumber\" typeof=\"PublicationIssue\"> "+dictio['volume']+"</b>, "+ "\n")
print("Getting pages...")
try:
pages = dictio['pages']
if pages is not None:
if len(pages) > 0:
if '--' in pages:
pag = pages.split("--")
begin = pag[0].strip()
end = pag[1].strip()
out.write("\t" + "<span property=\"schema:pageBegin\"> "+ begin +"</span>-<span property=\"schema:pageEnd\">"+ end + "</span>"+ "\n")
elif '-' in pages:
pag = pages.split("-")
begin = pag[0].strip()
end = pag[1].strip()
out.write("\t" + "<span property=\"schema:pageBegin\"> "+ begin +"</span>-<span property=\"schema:pageEnd\">"+ end + "</span>"+ "\n")
elif len(pages) > 0:
out.write("\t" + "<span property=\"schema:pageBegin\">"+ pages +"</span>"+ "\n")
else:
print("Sorry, no readable page information")
problemlist.append("Check for missing page info at " + str (x))
else:
print("Sorry, no page information")
problemlist.append("Check for missing page info at " + str (x))
else:
print("Sorry, no page information")
problemlist.append("Check for missing page info at " + str (x))
except KeyError as e:
print("Sorry, no page information")
problemlist.append("Check for missing page info at " + str (x))
year = dictio['year']
out.write("\t" +"(<time property=\"schema:datePublished\" datatype=\"xsd:gYear\" dateTime=\" " + year + "\">"+year+"</time>)."+ "\n")
try:
doi = dictio['doi']
out.write("\t" +"<a className=\"doi\" href=\"https://doi.org/"+doi+"\"> doi: "+doi+"</a>"+ "\n")
except KeyError as e:
print("Sorry, no doi information")
problemlist.append("Check for missing doi info at " + str (x))
out.write("</li>" + "\n"+ "\n")
def miscHTML(dictio, x, out):
print("Writing html code for entry "+ str(x) + "...")
out.write("#<!-- Citation num " + str(x) + "-->" + "\n")
out.write("<li typeof=\"schema:WebPage\" role=\"doc-biblioentry\" property=\"schema:citation\" id=\"desc-" + str(x) + "\">"+ "\n")
out.write("\t" + "<span property=\"schema:author\" typeof=\"schema:Organisation\">"+ "\n")
aut = dictio['author']
out.write("\t" + "\t" +"<span property=\"schema:Name\">" +aut + "</span>."+ "\n")
out.write("\t" +"</span>"+ "\n")
out.write("\t" + "<span property=\"schema:name\">"+dictio['title']+ ".</span>"+ "\n")
out.write("\t" +"<i property=\"schema:publisher\" typeof=\"schema:Organization\">"+ dictio['howpublished'] +"</i>"+ "\n")
year = dictio['year']
out.write("\t" +"(<time property=\"schema:datePublished\" datatype=\"xsd:gYear\" datetime=\"" + year + "\">"+year+"</time>)."+ "\n")
out.write("</li>" + "\n"+ "\n")
def bookHTML(dictio, x, out):
print("Writing html code for entry "+ str(x) + "...")
out.write("#<!-- Citation num " + str(x) + "-->" + "\n")
out.write("<li typeof=\"schema:WebPage\" role=\"doc-biblioentry\" property=\"schema:citation\" id=\"desc-" + str(x) + "\">"+ "\n")
out.write("\t" + "<span property=\"schema:author\" typeof=\"schema:Organisation\">"+ "\n")
aut = dictio['author']
out.write("\t" + "\t" +"<span property=\"schema:Name\">" +aut + "</span>."+ "\n")
out.write("\t" +"</span>"+ "\n")
out.write("\t" + "<span property=\"schema:name\">"+dictio['title']+ ".</span>"+ "\n")
out.write("\t" +"<i property=\"schema:publisher\" typeof=\"schema:Organization\">"+ dictio['publisher'] +"</i>"+ "\n")
year = dictio['year']
out.write("\t" +"(<time property=\"schema:datePublished\" datatype=\"xsd:gYear\" datetime=\"" + year + "\">"+year+"</time>)."+ "\n")
out.write("</li>" + "\n"+ "\n")
main()