Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
888 lines (734 sloc) 35.3 KB
# -*- coding: utf-8; mode: python -*-
import re
import sys
from lxml import etree
lang_dict = {"in" : {"english" : "In:", "german" : "In:"},
"volume" : {"english" : "Vol.", "german" : "Bd."},
"edited" : {"english" : "Ed. by", "german" : "Hrsg. von"},
"editor" : {"english" : "ed.", "german" : "Hrsg."},
"editors" : {"english" : "eds.", "german" : "Hrsg."},
"and" : {"english" : "and", "german" : "und"},
"phdthesis" : {"english" : "PhD thesis", "german" : "Diss"},
# "visited" : {"english" : "last visited", "german" : "besucht am"},
# "dateformat" : {"english" : "%d/%m/%Y", "german" : "%d.%m.%Y"}
"visited" : {"english" : "visited on", "german" : "besucht am"},
"dateformat" : {"english" : "%m/%d/%Y", "german" : "%d.%m.%Y"}
}
LANGUAGE = "german"
punctuation_marks = ['?', '.', ';', '!']
class Bibitem:
# TODO: Interne Funktion, um° durch Leerzeichen zu ersetzen
# TODO: Eine Lösung finden, für Autoren die nur aus einem Namen bestehen, ie keinen Vornamen haben
def __init__(self,xmlEntry):
self.xmlEntry = xmlEntry
def sanitize(self,strString):
strString = re.sub("°", " ", strString)
return strString
def gettext(self,xmlElement):
xmlText = xmlElement.text or ""
for xmlChild in xmlElement:
xmlText += self.gettext(xmlChild)
if xmlChild.tail:
xmlText += xmlChild.tail
return xmlText
def check_text(self, element_or_tail):
"""Check for text content of element or tail."""
return_string = ""
if element_or_tail is not None:
return_string = element_or_tail
return(return_string)
# def check_text ends here
def check_for_subtags(self, xml_element):
"""Check the present XML element for subtags"""
# xml_element = etree.fromstring(xml_string)
xml_element_children = xml_element.getchildren()
return_string = self.check_text(xml_element.text)
if len(xml_element_children) != 0:
for child in xml_element_children:
if len(child.getchildren()) != 0:
print("[MESSAGE] Oi! This is too much! Exiting.")
sys.exit()
else:
if child.tag == "hi":
if child.attrib["rend"] == "it":
return_string += "<i>%s</i>%s" % (child.text, self.check_text(child.tail))
elif child.attrib["rend"] == "sup":
return_string += "<sup>%s</sup>%s" % (child.text, self.check_text(child.tail))
elif child.tag == "error":
print("[MESSAGE] Found a command not recognized by Tralics: %s.\nContinuing." % child.attrib["n"])
print('\a')
input()
else:
print("[MESSAGE] Other elements besides italics are not allowed, found %s.\nExiting." % child.tag)
sys.exit()
# else:
# print("[MESSAGE] All clear here.\n")
return(return_string)
# def check_for_subtags ends here
def get_authorname(self, xmlAuthorRow, reversed_name = True):
"""Get name of author which sometimes as no first name.
Input is a list of author/mrow/mrow, return a tuple of lastname,
firstname
"""
str_lastname = xmlAuthorRow[0].text
# if only one name given
if len(xmlAuthorRow) <= 2:
str_completename = str_lastname
elif len(xmlAuthorRow) > 2:
str_firstname = xmlAuthorRow[2].text
if reversed_name == True:
str_completename = "%s, %s" % (str_lastname, str_firstname)
else:
str_completename = "%s %s" % (str_firstname, str_lastname)
return(self.sanitize(str_completename))
# strLastName = first_author.xpath("mrow")[0].text
# try:
# strFirstName = first_author.xpath("mrow")[2].text
# strCompleteName = strLastName + ", " + strFirstName
# except IndexError:
# strCompleteName = strLastName
# strCompleteName = strLastName + ", " + strFirstName
# # Remaining Authors are to be Firstname Lastname
# for i in range(1, (intNumberOfAuthors - 1)):
# one_of_other_authors = xmlAuthors[i]
# strLastName = one_of_other_authors.xpath("mrow")[0].text
# try:
# strFirstName = one_of_other_authors.xpath("mrow")[2].text
# strCompleteName = strCompleteName + ", " + strFirstName + " " + strLastName
# except IndexError:
# strCompleteName = strCompleteName + ", " + strLastName
# try:
# strFirstName = last_author.xpath("mrow")[2].text
# str_last_author = "%s %s" % (strFirstName, strLastName)
# except IndexError:
# str_last_author = "%s" % strLastName
# def get_authorname ends here
def citekey(self):
xmlEntry = self.xmlEntry
xmlLabel = xmlEntry.find(".//label")
if xmlLabel is not None:
return xmlLabel.text
else:
return None
def entrytype(self):
strType = self.xmlEntry.find(".//type").text
return strType
def title(self):
xmlTitle = self.xmlEntry.find(".//title")
# print("xmlTitle", xmlTitle)
# input()
if xmlTitle is not None:
# strTitle = self.gettext(xmlTitle)
strTitle = self.check_for_subtags(xmlTitle)
if strTitle[-1] in punctuation_marks:
return strTitle
else:
return strTitle + "."
else:
return ""
# def title ends here
def addendum(self):
xmlAddendum = self.xmlEntry.find(".//addendum")
if xmlAddendum is not None:
return xmlAddendum.text
else:
return ""
def volume(self):
xmlVolume = self.xmlEntry.find(".//volume")
if xmlVolume is not None:
strResult = " " + lang_dict["volume"][LANGUAGE] + " " + xmlVolume.text + ". "
return strResult
else:
return ""
def volumenumeric(self):
xmlVolume = self.xmlEntry.find(".//volume")
if xmlVolume is not None:
# strResult = " " + xmlVolume.text + ". "
strResult = " " + self.check_for_subtags(xmlVolume) + ". "
return strResult
else:
return ""
# def volumenumeric ends here
def seriesnumber(self):
"""For books. Series which might be followed by a number"""
xmlSeries = self.xmlEntry.find(".//series")
series_present = False
xmlNumber = self.xmlEntry.find("number")
number_present = False
strResult = ""
if xmlSeries is not None:
series_present = True
if xmlSeries.text is not None:
xml_series_strResult = xmlSeries.text
else:
xml_series_strResult = self.check_for_subtags(xmlSeries)
if xmlNumber is not None:
number_present = True
xml_number_strResult = xmlNumber.text
if series_present == True and number_present == True:
strResult = " " + xml_series_strResult + " " + xml_number_strResult + ". "
elif series_present == True and number_present == False:
strResult = " " + xml_series_strResult + ". "
return(strResult)
# # old series code
# xmlSeries = self.xmlEntry.find(".//series")
# if xmlSeries is not None:
# if xmlSeries.text is not None:
# strResult = " " + xmlSeries.text + ". "
# else:
# strResult = " " + self.check_for_subtags(xmlSeries) + ". "
# return strResult
# else:
# return ""
# # end old series code
# # old number code
# xmlNumber = self.xmlEntry.find("number")
# strResult = ""
# if xmlNumber is not None:
# strResult = xmlNumber.text + ". "
# return strResult
# # end old number code
# def seriesnumber ends here
def series(self):
xmlSeries = self.xmlEntry.find(".//series")
if xmlSeries is not None:
if xmlSeries.text is not None:
strResult = " " + xmlSeries.text + ". "
else:
strResult = " " + self.check_for_subtags(xmlSeries) + ". "
return strResult
else:
return ""
def journaltitle(self):
xmlJournaltitle = self.xmlEntry.find(".//journaltitle")
if xmlJournaltitle is not None:
# strResult = " <i>" + str(xmlJournaltitle.text) + "</i>"
strResult = " <i>" + self.check_for_subtags(xmlJournaltitle) + "</i>"
return strResult
else:
return ""
def booktitle(self):
xmlBooktitle = self.xmlEntry.find(".//booktitle")
strResult = ""
if xmlBooktitle is not None:
if xmlBooktitle.text is not None:
# this is still problematic as there might be formatting in the entries!
strResult = " In: <i>" + str(xmlBooktitle.text) + "</i>"
else:
# this is a very unclean solution
strResult = " In: <i>" + self.check_for_subtags(xmlBooktitle) + "</i>"
return strResult
# def booktitle(self):
# xmlBooktitle = self.xmlEntry.find(".//booktitle")
# if xmlBooktitle is not None:
# strResult = " In: <i>" + str(xmlBooktitle.text) + "</i> "
# return strResult
# else:
# return ""
# def booktitle ends here
def number(self):
xmlNumber = self.xmlEntry.find("number")
strResult = ""
if xmlNumber is not None:
strResult = xmlNumber.text + ". "
return strResult
# def number ends here
def volumenumberpages(self):
xmlVolume = self.xmlEntry.find(".//volume")
xmlPages = self.xmlEntry.find(".//pages")
xmlNumber = self.xmlEntry.find("number")
strResult = ""
if xmlVolume is not None and xmlNumber is not None and xmlPages is not None and xmlPages.text is not None:
strResult = " " + xmlVolume.text + "(" + xmlNumber.text + "):" + xmlPages.text.replace("-", "")
elif xmlVolume is not None and xmlNumber is None and xmlPages is not None:
strResult = " " + xmlVolume.text + ":" + xmlPages.text.replace("-", "")
elif xmlVolume is not None and xmlNumber is not None and xmlPages is None:
strResult = " " + xmlVolume.text + "(" + xmlNumber.text + ")"
elif xmlVolume is not None and xmlNumber is None and xmlPages is None:
strResult = " " + xmlVolume.text
elif xmlVolume is None and xmlNumber is not None and xmlPages is not None:
strResult = "( " + xmlNumber.text + "):" + xmlPages.text.replace("-", "")
elif xmlVolume is None and xmlNumber is None and xmlPages is not None:
strResult = ":" + xmlPages.text.replace("-", "")
return strResult
# def volumenumberpages ends here
def institution(self):
xmlInstitution = self.xmlEntry.find(".//institution")
xmlLocation = self.xmlEntry.find(".//location")
strResult = ""
if xmlInstitution is not None and xmlLocation is not None:
strResult = " " + xmlLocation.text + ": " + xmlInstitution.text
elif xmlInstitution is None and xmlLocation is not None:
strResult = xmlLocation.text
elif xmlInstitution is not None and xmlLocation is None:
strResult = xmlInstitution.text
return strResult
# def institution ends here
def location(self):
xmlLocation = self.xmlEntry.find(".//location")
xmlPublisher = self.xmlEntry.find(".//publisher")
if xmlLocation is not None:
if xmlPublisher is not None:
strResult = " " + xmlLocation.text + ": " + self.check_for_subtags(xmlPublisher)
else:
strResult = " " + xmlLocation.text
if strResult.endswith("."):
strResult = strResult[:-1]
return strResult
elif xmlPublisher is not None:
strResult = " "+ self.check_for_subtags(xmlPublisher)
if strResult.endswith("."):
strResult = strResult[:-1]
return strResult
else:
return ""
def howpublished(self):
xmlHowpublished = self.xmlEntry.find(".//howpublished")
if xmlHowpublished is not None:
# strResult = xmlHowpublished.text
strResult = self.check_for_subtags(xmlHowpublished)
return strResult
else:
return ""
def note(self):
bib_note = self.xmlEntry.find(".//note")
if bib_note is not None:
strResult = bib_note.text
return strResult + ". "
else:
return ""
# def note ends here
def pages(self):
xmlPages = self.xmlEntry.find(".//pages")
if xmlPages is not None:
strResult = ", " + xmlPages.text
# in order to get rid of double period
if strResult.endswith("."):
strResult = strResult[:-1]
return strResult.replace("-", "")
else:
return ""
# def pages ends here
def thesistype(self):
xmlTypes = self.xmlEntry.findall(".//type")
if len(xmlTypes) > 1:
if xmlTypes[1].text == "phdthesis":
strThesisType = " " + lang_dict["phdthesis"][LANGUAGE] + ". "
else:
strThesisType = " " + xmlTypes[1].text + ". "
else:
strThesisType = ""
return strThesisType
# def edition(self):
# xmlEdition = self.xmlEntry.find(".//edition")
# if xmlEdition is not None:
# if xmlEdition.text == "2" and LANGUAGE == "english":
# strEdition = " 2nd ed. "
# # elif xmlEdition.text
# else:
# strEdition = " " + xmlEdition.text + ". "
# return strEdition
# else:
# return ""
# # def edition ends here
def edition(self):
"""Prints edition
If value is plain integer, expand it as Latex would.
"""
xmlEdition = self.xmlEntry.find(".//edition")
strEdition = ""
if xmlEdition is not None:
edition_value = xmlEdition.text
try:
numeric_edition = int(edition_value)
if LANGUAGE == "german":
strEdition = " %d. Aufl. " % numeric_edition
if LANGUAGE == "english":
if edition_value[-1] == "1":
strEdition = " %dst ed. " % numeric_edition
elif edition_value[-1] == "2":
strEdition = " %dnd ed. " % numeric_edition
elif edition_value[-1] == "3":
strEdition = " %drd ed. " % numeric_edition
else:
strEdition = " %dth ed. " % numeric_edition
except ValueError:
strEdition = " " + edition_value + ". "
return(strEdition)
# def edition ends here
def edition(self):
xmlEdition = self.xmlEntry.find(".//edition")
if xmlEdition is not None:
strEdition = " " + xmlEdition.text + ". "
return strEdition
else:
return ""
def labelyear(self):
xmlEntry = self.xmlEntry
xmlEdition = xmlEntry.find(".//labelyear")
if xmlEdition is not None:
if xmlEdition.text == "nodate":
return ""
else:
return xmlEdition.text
else:
# If no year is given, return empty string
return ""
# def labelyear ends here
def year(self):
xmlEntry = self.xmlEntry
xmlYear = xmlEntry.find(".//year")
if xmlYear is not None:
return xmlYear.text
else:
return ""
# def year ends here
def url(self):
"""Not a nice solution here, but the URL has no element in the *bib.xml file
<urlday>28</urlday>
<urlmonth>10</urlmonth>
<urlyear>2015</urlyear>
<year>o.J.</year>
url
http://www.universalis.fr/encyclopedie/college-de-coqueret/
<...
"""
from datetime import datetime
urlday = urlmonth = urlyear = ""
xmlEntry = self.xmlEntry
xml_as_string = etree.tostring(xmlEntry)
xml_URL = re.findall(r"\n(https?://.*?)\n", xml_as_string.decode(encoding="utf-8"))
xml_urlday = xmlEntry.find(".//urlday")
xml_urlmonth = xmlEntry.find(".//urlmonth")
xml_urlyear = xmlEntry.find(".//urlyear")
if xml_urlday is not None:
urlday = xml_urlday.text
if xml_urlmonth is not None:
urlmonth = xml_urlmonth.text
if xml_urlyear is not None:
urlyear = xml_urlyear.text
if len(xml_URL) != 0:
url_date = "%s-%s-%s" % (urlyear, urlmonth, urlday)
parsed_date = datetime.strptime(url_date, "%Y-%m-%d")
str_URL = "URL: <a href='" + xml_URL[0] + "'>" + xml_URL[0] + "</a> (" + lang_dict["visited"][LANGUAGE] + " " + parsed_date.strftime(lang_dict["dateformat"][LANGUAGE]) + ")."
return(str_URL)
else:
return("")
# def url ends here
def url(self):
"""Not a nice solution here, but the URL has no element in the *bib.xml file
<urlday>28</urlday>
<urlmonth>10</urlmonth>
<urlyear>2015</urlyear>
<year>o.J.</year>
url
http://www.universalis.fr/encyclopedie/college-de-coqueret/
<...
"""
from datetime import datetime
urlday = urlmonth = urlyear = ""
xmlEntry = self.xmlEntry
xml_as_string = etree.tostring(xmlEntry)
xml_URL = re.findall(r"\n(https?://.*?)\n", xml_as_string.decode(encoding="utf-8"))
xml_urlday = xmlEntry.find(".//urlday")
xml_urlmonth = xmlEntry.find(".//urlmonth")
xml_urlyear = xmlEntry.find(".//urlyear")
if xml_urlday is not None:
urlday = xml_urlday.text
if xml_urlmonth is not None:
urlmonth = xml_urlmonth.text
if xml_urlyear is not None:
urlyear = xml_urlyear.text
if len(xml_URL) != 0:
url_date = "%s-%s-%s" % (urlyear, urlmonth, urlday)
parsed_date = datetime.strptime(url_date, "%Y-%m-%d")
if LANGUAGE == "german":
# make difference between languages here, because date will be formatted differently in other languages
str_URL = "URL: <a href='" + xml_URL[0] + "'>" + xml_URL[0] + "</a> (" + lang_dict["visited"][LANGUAGE] + " " + parsed_date.strftime("%d.%m.%Y") + ")."
return(str_URL)
elif LANGUAGE == "english":
# make difference between languages here, because date will be formatted differently in other languages
str_URL = "URL: <a href='" + xml_URL[0] + "'>" + xml_URL[0] + "</a> (" + lang_dict["visited"][LANGUAGE] + " " + parsed_date.strftime("%d/%m/%Y") + ")."
return(str_URL)
else:
return("")
# def url ends here
def labelyearsuffix(self):
xmlEntry = self.xmlEntry
xmlExtrayear = xmlEntry.find(".//extrayear")
if xmlExtrayear is not None:
intExtrayear = int(xmlExtrayear.text)
listCharacters = ["","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
strLabelYearSuffix = listCharacters[intExtrayear]
return strLabelYearSuffix
else:
return ""
def fullauthorlastfirst(self):
xmlEntry = self.xmlEntry
# If no Name is given, return either the Shorthand or the Title (Shorthand prefererred)
if xmlEntry.find(".//author/number") == None:
if xmlEntry.find(".//editor") is not None:
strComplete, several_editors = self.editorasauthor()
edited_suffix = lang_dict["editor"][LANGUAGE]
if several_editors == True:
edited_suffix = lang_dict["editors"][LANGUAGE]
return(strComplete, edited_suffix)
elif xmlEntry.find(".//shorthand") is not None:
strComplete = xmlEntry.find(".//shorthand").text
return(strComplete, "")
else:
strComplete = self.title()
return(strComplete, "no_author_only_title")
intNumberOfAuthors = int(xmlEntry.find(".//author/number").text)
if intNumberOfAuthors == 1:
xmlAuthorRows = xmlEntry.findall(".//author/mrow/mrow")
result_authorname = self.get_authorname(xmlAuthorRows)
return(result_authorname, "")
elif intNumberOfAuthors == 2:
xmlAuthors = xmlEntry.findall(".//author/mrow/mrow")
# First Author here would be Lastname, Firstname
first_author = xmlAuthors[0]
strCompleteName = self.get_authorname(first_author)
second_author = self.get_authorname(xmlAuthors[1], reversed_name = False)
strCompleteName = strCompleteName + " " + lang_dict["and"][LANGUAGE] + " " + second_author
return(self.sanitize(strCompleteName), "")
elif intNumberOfAuthors > 2 and intNumberOfAuthors < 12:
xmlAuthors = xmlEntry.findall(".//author/mrow/mrow")
# First Author here would be Lastname, Firstname
first_author = xmlAuthors[0]
strCompleteName = self.get_authorname(first_author)
for i in range(1, (intNumberOfAuthors - 1)):
one_of_other_authors = xmlAuthors[i]
another_author = self.get_authorname(one_of_other_authors, reversed_name = False)
strCompleteName = "%s, %s" % (strCompleteName, another_author)
last_author = self.get_authorname(xmlAuthors[-1], reversed_name = False)
if LANGUAGE == "english":
strCompleteName = strCompleteName + ", " + lang_dict["and"][LANGUAGE] + " " + last_author
elif LANGUAGE == "german":
strCompleteName = strCompleteName + " " + lang_dict["and"][LANGUAGE] + " " + last_author
return(self.sanitize(strCompleteName), "")
# if we have a lot of authors
elif intNumberOfAuthors > 12:
xmlAuthors = xmlEntry.findall(".//author/mrow/mrow")
# First Author here would be Lastname, Firstname
first_author = xmlAuthors[0]
strCompleteName = self.get_authorname(first_author)
strCompleteName = strCompleteName + " et al."
return(self.sanitize(strCompleteName), "")
# def fullauthorlastfirst ends here
def fullauthorfirstlast(self):
xmlEntry = self.xmlEntry
# If no Name is given, return either the Shorthand or the Title (Shorthand prefererred)
if xmlEntry.find(".//author/number") == None:
if xmlEntry.find(".//shorthand") is not None:
strComplete = xmlEntry.find(".//shorthand").text
return strComplete
else:
strComplete = self.title()
return strComplete
intNumberOfAuthors = int(xmlEntry.find(".//author/number").text)
if intNumberOfAuthors == 1:
xmlAuthorRows = xmlEntry.findall(".//author/mrow/mrow")
strLastName = xmlAuthorRows[0].text
if len(xmlAuthorRows) > 3:
strFirstName = xmlAuthorRows[2].text
strCompleteName = strFirstName + " " + strLastName
return self.sanitize(strCompleteName)
else:
return self.sanitize(strLastName)
if intNumberOfAuthors > 1:
xmlAuthorRows = xmlEntry.findall(".//author/mrow/mrow/mrow")
# First Author here would be Lastname, Firstname
strLastName = xmlAuthorRows[0].text
strFirstName = xmlAuthorRows[2].text
strCompleteName = strFirstName + " " + strLastName
# Remaining Authors are to be Firstname Lastname
for i in range(0, (intNumberOfAuthors - 1)):
a = i + 4
strLastName = ""
strFirstName = ""
if len(xmlAuthorRows) > a:
strLastName = xmlAuthorRows[a].text
a = i + 6
if len(xmlAuthorRows) > a:
strFirstName = xmlAuthorRows[a].text
strCompleteName = strCompleteName + ", " + strFirstName + " " + strLastName
return self.sanitize(strCompleteName)
def shortauthor(self):
xmlEntry = self.xmlEntry
# If no Name is given, return either the Editor, the Shorthand or the Title (Shorthand prefererred)
if xmlEntry.find(".//author/number") == None:
if xmlEntry.find(".//editor") is not None:
strComplete = self.editorasauthor(whole_name=False)[0]
return(strComplete)
elif xmlEntry.find(".//shorthand") is not None:
strComplete = xmlEntry.find(".//shorthand").text
return strComplete
else:
strComplete = xmlEntry.find(".//title").text
return strComplete
intNumberOfAuthors = int(xmlEntry.find(".//author/number").text)
if intNumberOfAuthors == 1:
xmlAuthorRows = xmlEntry.findall(".//author/mrow/mrow")
strLastName = xmlAuthorRows[0].text
return self.sanitize(strLastName)
if intNumberOfAuthors == 2:
xmlAuthorRows = xmlEntry.findall(".//author/mrow/mrow/mrow")
strLastName1 = xmlAuthorRows[0].text
if len(xmlAuthorRows) > 5:
strLastName2 = xmlAuthorRows[4].text
else:
strLastName2 = ""
strCompleteName = strLastName1 + " " + lang_dict["and"][LANGUAGE] + " " + strLastName2
return self.sanitize(strCompleteName)
if intNumberOfAuthors > 2:
xmlAuthorRows = xmlEntry.findall(".//author/mrow/mrow/mrow")
strLastName1 = xmlAuthorRows[0].text
strCompleteName = strLastName1 + " et.al."
return self.sanitize(strCompleteName)
def editorasauthor(self, whole_name=True):
more_than_one = False
xmlEntry = self.xmlEntry
if xmlEntry.find(".//editor/number") is None:
return("")
intNumberOfEditors = int(xmlEntry.find(".//editor/number").text)
if intNumberOfEditors == 1:
xmlEditorRows = xmlEntry.findall(".//editor/mrow/mrow")
strLastName = xmlEditorRows[0].text
if len(xmlEditorRows) > 3:
try:
strFirstName = xmlEditorRows[2].text
strCompleteName = strLastName + ", " + strFirstName
except IndexError:
strCompleteName = strCompleteName + ", " + strLastName
if whole_name is True:
return(self.sanitize(strCompleteName), more_than_one)
else:
return(self.sanitize(strLastName), more_than_one)
else:
return(self.sanitize(strLastName), more_than_one)
elif intNumberOfEditors == 2:
more_than_one = True
xmlEditors = xmlEntry.findall(".//editor/mrow/mrow")
# First Author here would be Lastname, Firstname
strCompleteName = self.get_authorname(xmlEditors[0])
second_editor = self.get_authorname(xmlEditors[1], reversed_name = False)
strCompleteName = strCompleteName + " " + lang_dict["and"][LANGUAGE] + " " + second_editor
return(self.sanitize(strCompleteName), more_than_one)
# elif intNumberOfEditors == 2:
# more_than_one = True
# xmlEditors = xmlEntry.findall(".//editor/mrow/mrow")
# # First Author here would be Lastname, Firstname
# first_editor = xmlEditors[0]
# strCompleteName = self.get_authorname(first_editor)
# # strLastName = first_editor.xpath("mrow")[0].text
# # strFirstName = first_editor.xpath("mrow")[2].text
# # strCompleteName = strLastName + ", " + strFirstName
# # Remaining Authors are to be Firstname Lastname
# for i in range(1, (intNumberOfEditors - 1)):
# one_of_other_editors = xmlEditors[i]
# another_editor = self.get_authorname(one_of_other_editors, reversed_name = False)
# strCompleteName = strCompleteName + another_editor
# # strLastName = one_of_other_editors.xpath("mrow")[0].text
# # strFirstName = one_of_other_editors.xpath("mrow")[2].text
# # strCompleteName = strCompleteName + ", " + strFirstName + " " + strLastName
# last_editor = self.get_authorname(xmlEditors[-1], reversed_name = False)
# # last_editor = xmlEditors[-1]
# # strLastName = last_editor.xpath("mrow")[0].text
# # strFirstName = last_editor.xpath("mrow")[2].text
# # strCompleteName = strCompleteName + " " + lang_dict["and"][LANGUAGE] + " " + strFirstName + " " + strLastName
# if LANGUAGE == "english":
# strCompleteName = strCompleteName + ", " + lang_dict["and"][LANGUAGE] + " " + last_editor
# elif LANGUAGE == "german":
# strCompleteName = strCompleteName + " " + lang_dict["and"][LANGUAGE] + " " + last_editor
# return(self.sanitize(strCompleteName), more_than_one)
elif intNumberOfEditors > 2:
more_than_one = True
xmlEditors = xmlEntry.findall(".//editor/mrow/mrow")
# First Author here would be Lastname, Firstname
first_editor = xmlEditors[0]
strLastName = first_editor.xpath("mrow")[0].text
strFirstName = first_editor.xpath("mrow")[2].text
strCompleteName = strLastName + ", " + strFirstName
# Remaining Authors are to be Firstname Lastname
for i in range(1, (intNumberOfEditors - 1)):
one_of_other_editors = xmlEditors[i]
strLastName = one_of_other_editors.xpath("mrow")[0].text
strFirstName = one_of_other_editors.xpath("mrow")[2].text
strCompleteName = strCompleteName + ", " + strFirstName + " " + strLastName
last_editor = xmlEditors[-1]
strLastName = last_editor.xpath("mrow")[0].text
strFirstName = last_editor.xpath("mrow")[2].text
if LANGUAGE == "english":
strCompleteName = strCompleteName + ", " + lang_dict["and"][LANGUAGE] + " " + strFirstName + " " + strLastName
elif LANGUAGE == "german":
strCompleteName = strCompleteName + " " + lang_dict["and"][LANGUAGE] + " " + strFirstName + " " + strLastName
return(self.sanitize(strCompleteName), more_than_one)
# def editorasauthor ends here
def editor(self):
xmlEntry = self.xmlEntry
# should be language specific
# If no Name is given, return either the Shorthand or the Title (Shorthand prefererred)
if xmlEntry.find(".//editor/number") is None:
return ""
intNumberOfEditors = int(xmlEntry.find(".//editor/number").text)
if intNumberOfEditors == 1:
xmlEditorRows = xmlEntry.findall(".//editor/mrow/mrow")
strLastName = xmlEditorRows[0].text
if len(xmlEditorRows) > 3:
strFirstName = xmlEditorRows[2].text
strCompleteName = lang_dict["edited"][LANGUAGE] + " " + strFirstName + " " + strLastName + "."
return self.sanitize(strCompleteName)
else:
strLastName = xmlEditorRows[0].text
strCompleteName = lang_dict["edited"][LANGUAGE] + " " + strLastName + "."
return self.sanitize(strCompleteName)
elif intNumberOfEditors == 2:
xmlEditors = xmlEntry.findall(".//editor/mrow/mrow")
# First Author here would be Lastname, Firstname
first_editor = xmlEditors[0]
strLastName = first_editor.xpath("mrow")[0].text
strFirstName = first_editor.xpath("mrow")[2].text
strCompleteName = lang_dict["edited"][LANGUAGE] + " " + strFirstName + " " + strLastName
# Remaining Authors are to be Firstname Lastname
for i in range(1, (intNumberOfEditors - 1)):
one_of_other_editors = xmlEditors[i]
if len(one_of_other_editors.xpath("mrow")) == 2:
strCompleteName = strCompleteName + ", " + one_of_other_editors.xpath("mrow")[0].text + "."
else:
strLastName = one_of_other_editors.xpath("mrow")[0].text
strFirstName = one_of_other_editors.xpath("mrow")[2].text
strCompleteName = strCompleteName + ", " + strFirstName + " " + strLastName + "."
last_editor = xmlEditors[-1]
if len(last_editor.xpath("mrow")) == 2:
strCompleteName = strCompleteName + " " + lang_dict["and"][LANGUAGE] + " " + last_editor.xpath("mrow")[0].text + "."
else:
strLastName = last_editor.xpath("mrow")[0].text
strFirstName = last_editor.xpath("mrow")[2].text
strCompleteName = strCompleteName + " " + lang_dict["and"][LANGUAGE] + " " + strFirstName + " " + strLastName + "."
elif intNumberOfEditors > 2:
xmlEditors = xmlEntry.findall(".//editor/mrow/mrow")
first_editor = xmlEditors[0]
strLastName = first_editor.xpath("mrow")[0].text
strFirstName = first_editor.xpath("mrow")[2].text
strCompleteName = lang_dict["edited"][LANGUAGE] + " " + strFirstName + " " + strLastName
# Remaining Authors are to be Firstname Lastname
for i in range(1, (intNumberOfEditors - 1)):
one_of_other_editors = xmlEditors[i]
if len(one_of_other_editors.xpath("mrow")) == 2:
strCompleteName = strCompleteName + ", " + one_of_other_editors.xpath("mrow")[0].text
else:
strLastName = one_of_other_editors.xpath("mrow")[0].text
strFirstName = one_of_other_editors.xpath("mrow")[2].text
strCompleteName = strCompleteName + ", " + strFirstName + " " + strLastName
last_editor = xmlEditors[-1]
if len(last_editor.xpath("mrow")) == 2:
strCompleteName = strCompleteName + " " + lang_dict["and"][LANGUAGE] + " " + last_editor.xpath("mrow")[0].text + "."
else:
strLastName = last_editor.xpath("mrow")[0].text
strFirstName = last_editor.xpath("mrow")[2].text
if LANGUAGE == "english":
strCompleteName = strCompleteName + ", " + lang_dict["and"][LANGUAGE] + " " + strFirstName + " " + strLastName + "."
elif LANGUAGE == "german":
strCompleteName = strCompleteName + " " + lang_dict["and"][LANGUAGE] + " " + strFirstName + " " + strLastName + "."
return self.sanitize(strCompleteName)