aboutsummaryrefslogtreecommitdiffstats
path: root/BibTeX.py
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2003-05-19 18:09:21 +0000
committerNick Mathewson <nickm@torproject.org>2003-05-19 18:09:21 +0000
commit6e23d475308c780f0312795b201d7d44143d0f0e (patch)
tree44fc36f6c03f23b6fda535f80eb5b3b05dd5ba49 /BibTeX.py
parentc07e917777582ff1e70e8b57345397ef977d684e (diff)
downloadanonbib-6e23d475308c780f0312795b201d7d44143d0f0e.tar.gz
More work; get by-date working, cleanups, debugging, etc
svn:r9
Diffstat (limited to 'BibTeX.py')
-rw-r--r--BibTeX.py63
1 files changed, 59 insertions, 4 deletions
diff --git a/BibTeX.py b/BibTeX.py
index 9444a65..9b0d38c 100644
--- a/BibTeX.py
+++ b/BibTeX.py
@@ -10,6 +10,10 @@ __all__ = ( 'ParseError', 'BibTeX', 'BibTeXEntry', 'htmlize',
'ParsedAuthor', 'FileIter', 'Parser', 'parseFile',
'splitBibTeXEntriesBy',
'sortBibTexEntriesBy', )
+
+MONTHS = [ None,
+ "January", "February", "March", "April", "May", "June",
+ "July", "August", "September", "October", "November", "December"]
class ParseError(Exception):
pass
@@ -34,7 +38,6 @@ class BibTeX:
cr = self.byKey[ent['crossref'].lower()]
except KeyError:
print "No such crossref: %s", ent['crossref']
- print ent
break
if seen.get(cr.key):
raise ParseError("Circular crossref at %s" % ent.key)
@@ -60,10 +63,54 @@ def splitEntriesBy(entries, field):
result[key] = [ent]
return result
-def sortEntriesBy(self, field):
- tmp = [ (ent.get(field), ent) for ent in entries ]
+def splitSortedEntriesBy(entries, field):
+ result = []
+ curVal = "alskjdsakldj"
+ curList = []
+ for ent in entries:
+ key = ent.get(field)
+ if key == curVal:
+ curList.append(ent)
+ else:
+ curVal = key
+ curList = [ent]
+ result.append((curVal, curList))
+ return result
+
+def sortEntriesBy(entries, field, default):
+ tmp = []
+ for ent in entries:
+ tmp = [ (txtize(ent.get(field, default)), ent) for ent in entries ]
+ tmp.sort()
+ return [ t[1] for t in tmp ]
+
+def sortEntriesByAuthor(entries):
+ tmp = []
+ for ent in entries:
+ authors = [ txtize(" ".join(a.von+a.last+a.first+a.jr))
+ for a in ent.parsedAuthor ]
+ tmp.append((tuple(authors), ent))
+ tmp.sort()
+ return [ t[1] for t in tmp ]
+
+def sortEntriesByDate(entries):
+ tmp = []
+ for ent in entries:
+ try:
+ mon = MONTHS.index(ent.get("month"))
+ except ValueError:
+ print "Unknown month %r in %s"%(ent.get("month"), ent.key)
+ mon = 0
+
+ try:
+ date = int(ent['year'])*13 + mon
+ except KeyError:
+ print "ERROR: No year field in %s"%ent.key
+ date = 10000*13
+ tmp.append((date, ent))
tmp.sort()
- return [ t[2] for t in tmp ]
+ return [ t[1] for t in tmp ]
+
DISPLAYED_FIELDS = [ 'title', 'author', 'journal', 'booktitle',
'school', 'institution', 'organization', 'volume', 'number', 'year',
@@ -287,6 +334,14 @@ def htmlize(s):
s = RE_PAGE_SPAN.sub(lambda m: "%s-%s"%(m.groups()), s)
return s
+def txtize(s):
+ s = RE_LONE_I.sub(lambda m: "%s" % m.group(1), s)
+ s = RE_ACCENT.sub(lambda m: "%s" % m.group(2), s)
+ s = RE_TEX_CMD.sub("", s)
+ s = s.translate(ALLCHARS, "{}")
+ return s
+
+
PROCEEDINGS_RE = re.compile(
r'((?:proceedings|workshop record) of(?: the)? )(.*)',
re.I)