19 def read(filenames, commentPrefix):
22 for filename
in filenames:
23 with open(filename)
as inFile:
27 line = re.sub(re.compile(
"[ \t]"+commentPrefix+
".*?$"),
"", line)
28 line = re.sub(re.compile(
"^"+commentPrefix+
".*?$"),
"", line)
29 content = content+line+
"\n"
31 bibDB = bibtexparser.loads(content)
40 if 'month' in entry
and entry[
'month']:
41 date = date+
","+entry[
'month']
43 if 'day' in entry
and entry[
'day']:
44 date = date+
","+entry[
'day'].split(
'-', 1)[0]
45 timeFormat =
"%Y,%B,%d"
46 return datetime.datetime.strptime(date, timeFormat)
54 if 'address' in entry
and entry[
'address']:
55 addressAndDate += prefix + entry[
'address']
57 if 'month' in entry
and entry[
'month']:
58 addressAndDate += prefix + datetime.datetime.strptime(entry[
'month'],
"%B").strftime(
"%b")
59 prefix =
" " if 'day' in entry
and entry[
'day']
else ", "
60 if 'day' in entry
and entry[
'day']:
61 addressAndDate += prefix + entry[
'day'].replace(
"--",
"-")
63 if 'year' in entry
and entry[
'year']:
64 addressAndDate += prefix + entry[
'year']
71 authorArray = author.split(
'and')
72 for i, oneAuthor
in enumerate(authorArray):
74 nameArray = oneAuthor.split(
',')
75 assert len(nameArray) == 2,
"len(nameArray) = %d" % len(nameArray)
76 authorArray[i] = nameArray[1].strip() +
' ' + nameArray[0].strip()
78 author = authorArray[i]
79 elif i+1 < len(authorArray):
80 author +=
", " + authorArray[i]
82 author +=
" and " + authorArray[i]
94 conferenceEntries = []
96 for entry
in bibDB.entries:
97 if 'journal' in entry:
98 journalEntries.append(entry)
100 conferenceEntries.append(entry)
102 journalEntries.sort(key=
lambda entry:
getDatetime(entry), reverse=
True)
103 conferenceEntries.sort(key=
lambda entry:
getDatetime(entry), reverse=
True)
104 stringMap = dict(bibDB.strings)
107 if suffix.lower() ==
'web':
108 print """# jemdoc: menu{MENU}{publications.html}
109 # jemdoc: addcss{yibolin_homepage/jemdoc.css}
110 # jemdoc: title{Yibo Lin's Homepage}
115 printWeb(bibDB, stringMap, highlightAuthors, journalEntries,
'journal',
'journal')
116 printWeb(bibDB, stringMap, highlightAuthors, conferenceEntries,
'conference',
'booktitle')
117 elif suffix.lower() ==
'cv':
118 print """\\begin{rSection}{Publications}
121 printCV(bibDB, stringMap, highlightAuthors, journalEntries,
'journal',
'journal')
122 printCV(bibDB, stringMap, highlightAuthors, conferenceEntries,
'conference',
'booktitle')
128 assert 0,
"unknown suffix = %s" % suffix
137 def printWeb(bibDB, stringMap, highlightAuthors, entries, publishType, booktitleKey):
139 if publishType ==
'journal':
140 print "=== Journal Papers\n"
143 print "=== Conference Papers\n"
148 for i, entry
in enumerate(entries):
149 if not currentYear
or currentYear.lower() != entry[
'year'].lower():
150 currentYear = entry[
'year']
151 print "==== %s\n" % (currentYear)
155 for highlightAuthor
in highlightAuthors:
156 author = author.replace(highlightAuthor,
"*"+highlightAuthor+
"*")
157 title = entry[
'title'].replace(
"{",
"").replace(
"}",
"")
158 booktitle = stringMap[entry[booktitleKey]]
if entry[booktitleKey]
in stringMap
else entry[booktitleKey]
159 address = entry[
'address']
if 'address' in entry
else ""
160 publishlink = entry[
'publishlink']
if 'publishlink' in entry
else ""
161 annotate = entry[
'annotateweb']
if 'annotateweb' in entry
else ""
163 title =
"[" + publishlink +
" " + title +
"]"
170 """ % (prefix, count, author, title, booktitle, addressAndDate, annotate)
180 def printCV(bibDB, stringMap, highlightAuthors, entries, publishType, booktitleKey):
182 if publishType ==
'journal':
184 \\textbf{Journal Papers}
189 \\textbf{Conference Papers}
193 \\begin{description}[font=\\normalfont]
200 for i, entry
in enumerate(entries):
201 if not currentYear
or currentYear.lower() != entry[
'year'].lower():
202 currentYear = entry[
'year']
206 for highlightAuthor
in highlightAuthors:
207 author = author.replace(highlightAuthor,
"\\textbf{"+highlightAuthor+
"}")
208 title = entry[
'title']
209 booktitle = stringMap[entry[booktitleKey]]
if entry[booktitleKey]
in stringMap
else entry[booktitleKey]
210 publishlink = entry[
'publishlink']
if 'publishlink' in entry
else ""
211 annotate = entry[
'annotatecv']
if 'annotatecv' in entry
else ""
213 title =
"\\href{" + publishlink +
"}{" + title +
"}"
222 """ % (prefix, count, author, title, booktitle, addressAndDate, annotate)
233 usage: python bibconvert.py --suffix suffix --highlight author1 [--highlight author2] --input 1.bib [--input 2.bib]
234 suffix can be 'web' or 'cv'
235 'web': jemdoc format for personal webpage
236 'cv': latex format for resume
239 if __name__ ==
"__main__":
243 highlightAuthors = []
247 if len(sys.argv) < 3
or sys.argv[1]
in (
'--help',
'-h'):
250 for i
in range(1, len(sys.argv), 2):
251 if sys.argv[i] ==
'--suffix':
253 raise RuntimeError(
"only one suffix can be accepted")
254 suffix = sys.argv[i+1]
255 elif sys.argv[i] ==
'--highlight':
256 highlightAuthors.append(sys.argv[i+1])
257 elif sys.argv[i] ==
'--input':
258 filenames.append(sys.argv[i+1])
def printWeb
print in Jemdoc format
def switchToFirstLastNameStyle
switch from [last name, first name] to [first name last name]
def printCV
print in Latex format
def printHelp
print help message
def getAddressAndDate
extract address and date time from entry
def read
read bibtex files with bibtexparser
def printBibDB
print bibtex database with target format
def getDatetime
extract date time from entry