-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpybib2html.py
More file actions
executable file
·497 lines (386 loc) · 11.2 KB
/
pybib2html.py
File metadata and controls
executable file
·497 lines (386 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/python
import sys
import re
import os.path
publication_counter=1
output_file=sys.stdout
author_data = dict()
def sort_by_year(y, x):
xyear = int(x[1].fields['year'])
yyear = int(y[1].fields['year'])
if xyear != yyear:
return xyear - yyear
#same year, sort by month if possible
if 'month' in x[1].fields and 'month' in y[1].fields:
xmonth = int(x[1].fields['month'])
ymonth = int(y[1].fields['month'])
if xmonth != ymonth:
return xmonth - ymonth
return x[0] < y[0]
def print_output(s):
global output_file
output_file.write(s)
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
def print_tag(t,extra_args=""):
if extra_args == "":
print_output("<"+t+">")
else:
print_output("<"+t+" " + extra_args+">")
def put_header():
open_tag("head");
print_output("<script src=\"http://code.jquery.com/jquery-1.5.1.js\"></script>\n")
print_output("<script type=\"text/javascript\" src=\"showhide.js\"> </script>")
print_output("<link href=\"style.css\" type=text/css rel=stylesheet>\n")
close_tag("head");
def open_tag(t, args=""):
print_tag(t,args)
def close_tag(t):
print_tag('/'+t)
def html_intag(t,x,args=""):
open_tag(t,args)
printtex(x)
close_tag(t)
def html_strong(x):
html_intag("strong",x)
def html_h3(x):
html_intag("h3",x)
def html_i(x):
html_intag("em",x)
def html_br():
print_tag("br/")
def html_a(target,text):
open_tag("a","href=\""+target+"\"")
printtex(text)
close_tag("a")
def open_div():
open_tag("div")
def open_div(classname=None):
if classname is None:
open_tag("div")
else:
open_tag("div","class=\""+classname+"\"")
def open_span(classname):
open_tag("span","class=\""+classname+"\"")
def close_span():
close_tag("span")
def close_div():
close_tag("div")
print_output("\n\n")
def printtex_replace_command(t,command,newtext,argc,commandprefix="\\\\"):
#e.g. t="bla blac \frac{x}{y}", command=frac arg_map=\\1 / \\2
parseargs=""
for i in range(0,argc):
parseargs+="{([^{]*)}"
pattern=commandprefix+command+parseargs
return re.sub(pattern,newtext,t)
def printtex_text(t):
### remove comments
t=t.replace("\\%","%") #first get rid of actual %'s in the input
t=re.sub("%.*","",t) #the remaining %'s are comments
t=printtex_replace_command(t,"emph","<em>\\1</em>",1)
t=t.replace("\\&","&") #first get rid of actual %'s in the input
t=t.replace("\\o{}","ø")
t=t.replace("\\o{}","ø")
t=t.replace("\\ae{}","æ")
t=t.replace("\\aa{}","å")
t=t.replace("\\begin{itemize}","<ul>")
t=t.replace("\\end{itemize}","</ul>")
t=t.replace("\\item","<li>")
t=t.replace("{","")
t=t.replace("~"," ")
t=t.replace("}","")
t=t.replace("\\-","-")
t=t.replace("---","—")
#print_output(t)
return t
def printtex_math(t):
t=t.replace("<","<")
t=t.replace(">",">")
t=t.replace("\delta","δ")
t=t.replace("\dots","...")
t=t.replace("\Theta","Θ")
t=t.replace("\Omega","Ω")
t=t.replace("\log","</em>log<em>")
t=t.replace("\ell","l")
t=printtex_replace_command(t,"mathitbf","<strong>\\1</strong>",1)
t=printtex_replace_command(t,"mathbf","<strong>\\1</strong>",1)
t=printtex_replace_command(t,"mathrm","</em>\\1<em>",1)
t=printtex_replace_command(t,"frac","\\1/\\2",2)
t=printtex_replace_command(t,"_","<sub>\\1</sub>",1,"")
open_tag("em")
print_output(t)
close_tag("em")
def printtex(t):
tokens=t.split('$')
math=False
for tok in tokens:
if math:
printtex_math(tok)
else:
print_output(printtex_text(tok) )
math=not math
def put_title(key,value):
print_output("\n\n<!-- - - - - - - NEW PAPER - - - - - - - - -->\n\n\n")
if 'note' in value.fields:
open_span('paper-note')
print_output(value.fields['note'])
close_span()
open_span("papertitle")
open_paper_link(key)
printtex(value.fields['title'])
close_paper_link(key)
close_span()
print_output("\n")
def get_paper_path(key):
prefix="papers/"
key_underscore=key.replace(':','_')
p = prefix+key_underscore+".pdf"
if os.path.isfile(p):
return p
else:
return ""
def open_paper_link(key):
p = get_paper_path(key)
if p != "":
open_tag("a","href="+p+" title=\"Download PDF\"")
else:
print "No pdf for " + key
def close_paper_link(key):
p = get_paper_path(key)
if p != "":
close_tag("a")
def put_image(key):
#Look for fig/key.{png,jpg}
prefix="paper_figs/"
key_underscore=key.replace(':','_')
png_path=prefix+key_underscore+".png"
jpg_path=prefix+key_underscore+".jpg"
thumb_path=prefix+key_underscore+"_thumb.jpg"
path=""
if os.path.isfile(png_path):
path=png_path
if os.path.isfile(jpg_path):
path=jpg_path
if path != "":
#open_tag("img","src=\""+thumb_path+"\" alt=\"Figure for "+key_underscore+"\" "+"width=\"80\" height=\"80\"")
open_tag("img","src=\""+thumb_path+"\" alt=\"Figure for "+key_underscore+"\"")
# print "Image for " + key + ": " + path
else:
print "Image for " + key + " not found."
def put_title_author(value,key):
put_image(key)
put_title(key,value)
print_authors(value)
def new_entry_end(key,value):
print_output("\n<!-- END ENTRY -->\n")
def new_entry_begin(key,value):
print_output("\n<!-- NEW ENTRY -->\n")
def put_data_line(value,f,f2=""):
open_div("paper-data")
if f in value.fields:
html_i(value.fields[f]+"")
print_output(",")
print_output(" "+value.fields['year']+".")
close_div()
def put_details(value):
if 'abstract' not in value.fields and 'doi' not in value.fields:
return
open_hidden_div()
if 'abstract' in value.fields:
print_abstract(value)
print_doi(value)
close_hidden_div()
def print_authors(value):
global author_data
open_div("paper-authors")
firstPerson=True
for p in value.persons.items()[0][1]:
if not firstPerson:
print_output(", ")
else:
firstPerson=False
name=""
first=True
for f in p.first():
name+=f+" "
for f in p.middle():
name+=f+" "
for f in p.last():
name+=f+" "
link=None
for pattern in author_data:
link_=author_data[pattern]
if re.search(pattern,name) != None:
link=link_
if link==None:
print_output(printtex_text(name[0:len(name)-1]))
# sys.stderr.write("No link for: " + name + "\n")
else:
print_output('<a href=\"'+link+'\">')
print_output(printtex_text(name[0:len(name)-1]))
print_output('</a>')
print_output(".")
close_div()
def print_doi(value):
if 'doi' in value.fields:
html_intag("strong","doi: ")
doi=value.fields['doi']
print_output(" ")
html_a("http://dx.doi.org/"+doi, ""+doi+"")
def read_author_data(filename):
f=open(filename)
global author_data
for l in f:
test=l.split('=') # format is "author pattern"="email"
if len(test)!=2:
if (l != ""):
sys.stderr.write("Skipping author data line: " + l + "\n")
continue
name=test[0]
url=test[1]
url=url.rstrip('\n')
author_data[name]=url
def open_hidden_div():
global publication_counter
p=str(publication_counter)
open_div()
print_output("<div class=\"module-det mod" + p + "\" style=\"display:none;\">")
def close_hidden_div():
global publication_counter
p=str(publication_counter)
close_div()
close_div()
print_output("<a class=\"details-less detl" + p +"\" href=\"javascript:;\" onClick=\"details("+p+")\" id=\"hide_mod"+p+"\" style=\"display:none;\">hide details</a> <a class=\"details-more detm"+p+"\" href=\"javascript:;\" onClick=\"details("+p+")\" id=\"show_mod"+p+"\">read details</a>")
def print_abstract(value):
html_intag("strong","Abstract:")
open_div("paper-abstract")
printtex(value.fields['abstract'])
close_div()
def handle_default(key,value):
new_entry_begin(key,value)
open_div("titlewrap")
put_image(key)
put_title(key,value)
close_div()
open_div("infowrap")
print_authors(value)
if 'journal' in value.fields:
html_i(value.fields['journal'])
if 'howpublished' in value.fields:
html_i(value.fields['howpublished'])
print_output(", ")
print_output(value.fields['year']+".")
put_details(value)
close_div()
new_entry_end(key,value)
def handle_article(key,value):
new_entry_begin(key,value)
open_div("titlewrap")
put_image(key)
put_title(key,value)
close_div()
open_div("infowrap")
print_authors(value)
put_data_line(value,"journal")
put_details(value)
new_entry_end(key,value)
close_div()
def handle_phdthesis(key,value):
new_entry_begin(key,value)
open_div("titlewrap")
put_image(key)
put_title(key,value)
close_div()
open_div("infowrap")
print_authors(value)
put_data_line(value,"school")
put_details(value)
new_entry_end(key,value)
close_div()
def handle_inproceedings(key,value):
new_entry_begin(key,value)
open_div("titlewrap")
put_image(key)
put_title(key,value)
close_div()
open_div("infowrap")
print_authors(value)
put_data_line(value,"booktitle")
put_details(value)
new_entry_end(key,value)
close_div()
def handle_techreport(key,value):
new_entry_begin(key,value)
open_div("titlewrap")
put_image(key)
put_title(key,value)
close_div()
open_div("infowrap")
print_authors(value)
put_data_line(value,"institution","number")
put_details(value)
new_entry_end(key,value)
close_div()
def handle_values(l,use_key_only):
global publication_counter
l_sorted = sorted(l, cmp=sort_by_year)
handlers = {'article':handle_article,'inproceedings':handle_inproceedings,'techreport':handle_techreport,'phdthesis':handle_phdthesis}
for key, value in l_sorted:
if use_key_only=="" or ("key" in value.fields and value.fields["key"]==use_key_only):
bibtex_class = value.type
open_tag("li")
if (bibtex_class in handlers):
handlers[bibtex_class](key,value)
else:
sys.stderr.write("No handler for: " + bibtex_class+", using default\n")
handle_default(key,value)
close_tag("li")
publication_counter+=1
def handle_types(list_of_types,typemaps, description,use_key_only=""):
print_output("<h3>"+description+"</h3>")
if publication_counter==1:
open_tag("ol","class=first")
else:
open_tag("ol")
for l in list_of_types:
handle_values(typemaps[l],use_key_only)
close_tag("ol")
def main():
import getopt
options,remainder=getopt.getopt(sys.argv[1:], 'i:o:a:',['input=','output=',"authors="])
from pybtex.database.input import bibtex
from operator import itemgetter, attrgetter
import pprint
parser = bibtex.Parser()
#bib_data = parser.parse_file('mypapers.bib')
global output_file
for opt,arg in options:
if opt in ('-o', '--output'):
output_file = open(arg,'w')
if opt in ('-i', '--input'):
input_filename = arg
if opt in ('-a', '--authors'):
author_filename = arg
read_author_data(author_filename)
bib_data = parser.parse_file(input_filename)
from collections import defaultdict
typemaps=defaultdict(list)
for key, value in bib_data.entries.items():
bibtex_class = value.type
typemaps[bibtex_class].append((key,value))
open_div("paper")
handle_types(["phdthesis"],typemaps,"Dissertation")
handle_types(["inproceedings"],typemaps,"Conference Papers")
handle_types(["article"],typemaps,"Journal Papers","journal")
handle_types(["article","techreport"],typemaps,"Other Papers","other")
handle_types(["misc"],typemaps,"Abstracts")
close_div()
print_output("<script>$(\".details-more\").click(details); $(\".details-less\").click(details)</script>")
# html_br()
# print_output("<small>Generated by ")
# html_a("https://github.com/thomasmoelhave/pybib2html","pybib2html")
# print_output("</small>")
main()