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
|
import rank
import sys
import re
import config
import os
from utils import htmlize, url_untranslate, unTeXescapeURL, smartJoin,\
_split, ALLCHARS, PRINTINGCHARS
from author import parseAuthor
# Fields that we only care about for making web pages (BibTeX doesn't
# recognize them.)
WWW_FIELDS = ['www_section', 'www_important', 'www_remarks',
'www_abstract_url', 'www_html_url', 'www_pdf_url', 'www_ps_url',
'www_txt_url', 'www_ps_gz_url', 'www_amazon_url',
'www_excerpt_url', 'www_publisher_url',
'www_cache_section', 'www_tags']
def author_url(author):
"""Given an author's name, return a URL for his/her homepage."""
for pat, url in config.AUTHOR_RE_LIST:
if pat.search(author):
return url
return None
PROCEEDINGS_RE = re.compile(
r'((?:proceedings|workshop record) of(?: the)? )(.*)',
re.I)
# List of fields that appear when we display the entries as BibTeX.
DISPLAYED_FIELDS = [ 'title', 'author', 'journal', 'booktitle',
'school', 'institution', 'organization', 'volume', 'number', 'year',
'month', 'address', 'location', 'chapter', 'edition', 'pages', 'editor',
'howpublished', 'key', 'publisher', 'type', 'note', 'series' ]
class BibTeXEntry:
"""A single BibTeX entry."""
def __init__(self, type, key, entries):
self.type = type # What kind of entry is it? (@book,@injournal,etc)
self.key = key # What key does it have?
self.entries = entries # Map from key to value.
self.entryLine = 0 # Defined on this line number
def get(self, k, v=None):
return self.entries.get(k,v)
def has_key(self, k):
return self.entries.has_key(k)
def __getitem__(self, k):
return self.entries[k]
def __setitem__(self, k, v):
self.entries[k] = v
def __str__(self):
return self.format(70,1)
def getURL(self):
"""Return the best URL to use for this paper, or None."""
best = None
for field in ['www_pdf_url', 'www_ps_gz_url', 'www_ps_url',
'www_html_url', 'www_txt_url', ]:
u = self.get(field)
if u:
if not best:
best = u
elif (best.startswith("http://citeseer.nj.nec.com/")
and not u.startswith("http://citeseer.nj.nec.com/")):
best = u
return best
def format(self, width=70, indent=8, v=0, invStrings={}):
"""Format this entry as BibTeX."""
d = ["@%s{%s,\n" % (self.type, self.key)]
if v:
df = DISPLAYED_FIELDS[:]
for k in self.entries.keys():
if k not in df:
df.append(k)
else:
df = DISPLAYED_FIELDS
for f in df:
if not self.entries.has_key(f):
continue
v = self.entries[f]
if v.startswith("<span class='bad'>"):
d.append("%%%%% ERROR: Missing field\n")
d.append("%% %s = {?????},\n"%f)
continue
np = v.translate(ALLCHARS, PRINTINGCHARS)
if np:
d.append("%%%%% "+("ERROR: Non-ASCII characters: '%r'\n"%np))
d.append(" ")
v = v.replace("&", "&")
if invStrings.has_key(v):
s = "%s = %s,\n" %(f, invStrings[v])
else:
s = "%s = {%s},\n" % (f, v)
d.append(_split(s,width,indent))
d.append("}\n")
return "".join(d)
def resolve(self):
"""Handle post-processing for this entry"""
a = self.get('author')
if a:
self.parsedAuthor = parseAuthor(a)
#print a
#print " => ",repr(self.parsedAuthor)
else:
self.parsedAuthor = None
def isImportant(self):
"""Return 1 iff this entry is marked as important"""
imp = self.get("www_important")
if imp and imp.strip().lower() not in ("no", "false", "0"):
return 1
return 0
def check(self):
"""Print any errors for this entry, and return true if there were
none."""
errs = self._check()
for e in errs:
print e
return not errs
def _check(self):
errs = []
if self.type == 'inproceedings':
fields = 'booktitle', 'year'
elif self.type == 'incollection':
fields = 'booktitle', 'year'
elif self.type == 'proceedings':
fields = 'booktitle', 'editor'
elif self.type == 'article':
fields = 'journal', 'year'
elif self.type == 'techreport':
fields = 'institution',
elif self.type == 'misc':
fields = 'howpublished',
elif self.type in ('mastersthesis', 'phdthesis'):
fields = ()
else:
fields = ()
errs.append("ERROR: odd type %s"%self.type)
if self.type != 'proceedings':
fields += 'title', 'author', 'www_section', 'year'
for field in fields:
if self.get(field) is None or \
self.get(field).startswith("<span class='bad'>"):
errs.append("ERROR: %s has no %s field" % (self.key, field))
self.entries[field] = "<span class='bad'>%s:??</span>"%field
if self.type == 'inproceedings':
if self.get("booktitle"):
if not self['booktitle'].startswith("Proceedings of") and \
not self['booktitle'].startswith("{Proceedings of"):
errs.append("ERROR: %s's booktitle (%r) doesn't start with 'Proceedings of'" % (self.key, self['booktitle']))
if self.has_key("pages") and not re.search(r'\d+--\d+', self['pages']):
errs.append("ERROR: Misformed pages in %s"%self.key)
if self.type == 'proceedings':
if self.get('title'):
errs.append("ERROR: %s is a proceedings: it should have a booktitle, not a title." % self.key)
for field, value in self.entries.items():
if value.translate(ALLCHARS, PRINTINGCHARS):
errs.append("ERROR: %s.%s has non-ASCII characters"%(
self.key, field))
if field.startswith("www_") and field not in WWW_FIELDS:
errs.append("ERROR: unknown www field %s"% field)
if value.strip()[-1:] == '.' and \
field not in ("notes", "www_remarks", "author"):
errs.append("ERROR: %s.%s has an extraneous period"%(self.key,
field))
return errs
def biblio_to_html(self):
"""Return the HTML for the citation portion of entry."""
if self.type in ('inproceedings', 'incollection'):
booktitle = self['booktitle']
bookurl = self.get('bookurl')
if bookurl:
m = PROCEEDINGS_RE.match(booktitle)
if m:
res = ["In the ", m.group(1),
'<a href="%s">' % bookurl, m.group(2), "</a>"]
else:
res = ['In the <a href="%s">%s</a>' % (bookurl, booktitle)]
else:
res = ["In the ", booktitle]
if self.get("edition"):
res.append(",")
res.append(self['edition'])
if self.get("location"):
res.append(", ")
res.append(self['location'])
elif self.get("address"):
res.append(", ")
res.append(self['address'])
res.append(", %s %s" % (self.get('month', ""), self['year']))
if not self.get('pages'):
pass
elif "-" in self['pages']:
res.append(", pages %s" % self['pages'])
else:
res.append(", page %s" % self['pages'])
elif self.type == 'article':
res = ["In "]
if self.get('journalurl'):
res.append('<a href="%s">%s</a>' % (self['journalurl'],
self['journal']))
else:
res.append(self['journal'])
if self.get('volume'):
res.append(" <b>%s</b>" % self['volume'])
if self.get('number'):
res.append("(%s)" % self['number'])
res.append(", %s %s" % (self.get('month', ""), self['year']))
if not self.get('pages'):
pass
elif "-" in self['pages']:
res.append(", pages %s" % self['pages'])
else:
res.append(", page %s" % self['pages'])
elif self.type == 'techreport':
res = ["%s %s %s" % (self['institution'],
self.get('type', 'technical report'),
self.get('number', ""))]
if self.get('month') or self.get('year'):
res.append(", %s %s" % (self.get('month', ''),
self.get('year', '')))
elif self.type == 'mastersthesis' or self.type == 'phdthesis':
if self.get('type'):
res = [self['type']]
elif self.type == 'mastersthesis':
res = ["Masters's thesis"]
else:
res = ["Ph.D. thesis"]
if self.get('school'):
res.append(", %s" % (self['school']))
if self.get('month') or self.get('year'):
res.append(", %s %s" % (self.get('month', ''),
self.get('year', '')))
elif self.type == 'book':
res = [self['publisher']]
if self.get('year'):
res.append(" ")
res.append(self.get('year'))
# res.append(", %s"%(self.get('year')))
if self.get('series'):
res.append(",")
res.append(self['series'])
elif self.type == 'misc':
res = [self['howpublished']]
if self.get('month') or self.get('year'):
res.append(", %s %s" % (self.get('month', ''),
self.get('year', '')))
if not self.get('pages'):
pass
elif "-" in self['pages']:
res.append(", pages %s" % self['pages'])
else:
res.append(", page %s" % self['pages'])
else:
res = ["<Odd type %s>" % self.type]
res[0:0] = ["<span class='biblio'>"]
res.append(".</span>")
bibtexurl = "./bibtex.html#%s" % url_untranslate(self.key)
res.append((" <span class='availability'>"
"(<a href='%s'>BibTeX entry</a>)"
"</span>") % bibtexurl)
return htmlize("".join(res))
def to_html(self, cache_path="./cache", base_url="."):
"""Return the HTML for this entry."""
imp = self.isImportant()
draft = self.get('year') == 'forthcoming'
if imp:
res = ["<li><div class='impEntry'><p class='impEntry'>"]
elif draft:
res = ["<li><div class='draftEntry'><p class='draftEntry'>"]
else:
res = ["<li><p class='entry'>"]
if imp or not draft:
# Add a picture of the rank
# Only if year is known or paper important!
r = rank.get_rank_html(self['title'], self.get('year'),
update=False, base_url=base_url)
if r is not None:
res.append(r)
res.append("<span class='title'><a name='%s'>%s</a></span>"%(
url_untranslate(self.key),htmlize(self['title'])))
for cached in 0,1:
availability = []
if not cached:
for which in [ "amazon", "excerpt", "publisher" ]:
key = "www_%s_url"%which
if self.get(key):
url=self[key]
url = unTeXescapeURL(url)
availability.append('<a href="%s">%s</a>' %(url,which))
cache_section = self.get('www_cache_section', ".")
if cache_section not in config.CACHE_SECTIONS:
if cache_section != ".":
print >>sys.stderr, "Unrecognized cache section %s"%(
cache_section)
cache_section="."
for key, name, ext in (('www_abstract_url', 'abstract','abstract'),
('www_html_url', 'HTML', 'html'),
('www_pdf_url', 'PDF', 'pdf'),
('www_ps_url', 'PS', 'ps'),
('www_txt_url', 'TXT', 'txt'),
('www_ps_gz_url', 'gzipped PS','ps.gz')
):
if cached:
#XXXX the URL needs to be relative to the absolute
#XXXX cache path.
url = smartJoin(cache_path,cache_section,
"%s.%s"%(self.key,ext))
fname = smartJoin(config.OUTPUT_DIR, config.CACHE_DIR,
cache_section,
"%s.%s"%(self.key,ext))
if not os.path.exists(fname): continue
else:
url = self.get(key)
if not url: continue
url = unTeXescapeURL(url)
url = url.replace('&', '&')
availability.append('<a href="%s">%s</a>' %(url,name))
if availability:
res.append([" ", " "][cached])
res.append("<span class='availability'>(")
if cached: res.append("Cached: ")
res.append(", ".join(availability))
res.append(")</span>")
res.append("<br /><span class='author'>by ")
#res.append("\n<!-- %r -->\n" % self.parsedAuthor)
htmlAuthors = [ a.htmlizeWithLink() for a in self.parsedAuthor ]
if len(htmlAuthors) == 1:
res.append(htmlAuthors[0])
elif len(htmlAuthors) == 2:
res.append(" and ".join(htmlAuthors))
else:
res.append(", ".join(htmlAuthors[:-1]))
res.append(", and ")
res.append(htmlAuthors[-1])
if res[-1][-1] != '.':
res.append(".")
res.append("</span><br />\n")
res.append(self.biblio_to_html())
res.append("<a href='#%s'>·</a>"%url_untranslate(self.key))
res.append("</p>")
if self.get('www_remarks'):
res.append("<p class='remarks'>%s</p>"%htmlize(
self['www_remarks']))
if imp or draft:
res.append("</div>")
res.append("</li>\n\n")
return "".join(res)
|