From 023a13db0f872156b098cff9e6005875ea368e3d Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 20 May 2003 15:43:38 +0000 Subject: Add BibTeX file, by-author listing svn:r11 --- BibTeX.py | 107 ++++++++++++++++++++++++++++++++++-------------- _template_.html | 125 +++++++------------------------------------------------- css/main.css | 6 ++- css/pubs.css | 21 ++++++++++ writeHTML.py | 83 ++++++++++++++++++++++++------------- 5 files changed, 172 insertions(+), 170 deletions(-) diff --git a/BibTeX.py b/BibTeX.py index 9b0d38c..f30e4ce 100644 --- a/BibTeX.py +++ b/BibTeX.py @@ -14,7 +14,14 @@ __all__ = ( 'ParseError', 'BibTeX', 'BibTeXEntry', 'htmlize', MONTHS = [ None, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] - + +def url_untranslate(s): + s = s.replace(" ", "+") + s = re.sub(r'([%<>])', + lambda m: "%%%02x"%ord(m.group(1)), + s) + return s + class ParseError(Exception): pass @@ -84,6 +91,29 @@ def sortEntriesBy(entries, field, default): tmp.sort() return [ t[1] for t in tmp ] +def splitEntriesByAuthor(entries): + entries = sortEntriesByDate(entries) + result = {} # Name in sorting order -> entries + htmlResult = {} # name in sorting order -> Full name + url_map = {} # Full name -> Url + for ent in entries: + for a in ent.parsedAuthor: + sortkey = txtize(" ".join(a.von+a.last+a.first+a.jr)) + secname = " ".join(a.first+a.von+a.last) + if a.jr: + secname += ", "+a.jr + secname = htmlize(secname) + url = author_url(secname) + if url: + url_map[secname] = url + + htmlResult[sortkey] = secname + result.setdefault(sortkey, []).append(ent) + sortnames = result.keys() + sortnames.sort() + sections = [ (htmlResult[n], result[n]) for n in sortnames ] + return sections, url_map + def sortEntriesByAuthor(entries): tmp = [] for ent in entries: @@ -131,7 +161,7 @@ class BibTeXEntry: self.entries[k] = v def __str__(self): return self.format(70,1) - def format(self, width=70,v=0): + def format(self, width=70, indent=8, v=0): d = ["@%s{%s,\n" % (self.type, self.key)] if v: df = DISPLAYED_FIELDS[:] @@ -145,8 +175,8 @@ class BibTeXEntry: continue v = self.entries[f] d.append(" ") - s = "%s = {%s}\n" % (f, v) - d.append(_split(s,width)) + s = "%s = {%s},\n" % (f, v) + d.append(_split(s,width,indent)) d.append("}\n") return "".join(d) def resolve(self): @@ -259,10 +289,11 @@ class BibTeXEntry: res[0:0] = [""] res.append("") - - res.append(" " - "(BibTeX entry)" - "") + + bibtexurl = "./bibtex.html#%s"%url_untranslate(self.key) + res.append((" " + "(BibTeX entry)" + "") %bibtexurl) return htmlize("".join(res)) def to_html(self): @@ -285,21 +316,8 @@ class BibTeXEntry: res.append("
by ") #res.append("\n\n" % self.parsedAuthor) - htmlAuthors = [] - for author in self.parsedAuthor: - f,v,l,j = author.first,author.von,author.last,author.jr - a = " ".join(f+v+l) - if j: - a = "%s, %s" %(a,j) - a = htmlize(a) - htmlAuthor = None - for pat, url in config.AUTHOR_RE_LIST: - if pat.search(a): - htmlAuthor = '%s' % (url, a) - break - if not htmlAuthor: - htmlAuthor = a - htmlAuthors.append(htmlAuthor) + htmlAuthors = [ htmlize_author(a) for a in self.parsedAuthor ] + if len(htmlAuthors) == 1: res.append(htmlAuthors[0]) elif len(htmlAuthors) == 2: @@ -319,8 +337,12 @@ class BibTeXEntry: RE_LONE_AMP = re.compile(r'&([^a-z0-9])') RE_LONE_I = re.compile(r'\\i([^a-z0-9])') RE_ACCENT = re.compile(r'\\([\'`~^"])(.)') -ACCENT_MAP = { "'": 'acute', "`" : 'grave', "~": 'tilde', - "^": 'circ', '"' : 'uml' } +ACCENT_MAP = { "'" : 'acute', + "`" : 'grave', + "~" : 'tilde', + "^" : 'circ', + '"' : 'uml', + } RE_TEX_CMD = re.compile(r"(?:\\[a-zA-Z@]+|\\.)") RE_PAGE_SPAN = re.compile(r"(\d)--(\d)") def htmlize(s): @@ -334,6 +356,25 @@ def htmlize(s): s = RE_PAGE_SPAN.sub(lambda m: "%s-%s"%(m.groups()), s) return s +def htmlize_author(author): + f,v,l,j = author.first,author.von,author.last,author.jr + a = " ".join(f+v+l) + if j: + a = "%s, %s" %(a,j) + a = htmlize(a) + u = author_url(a) + if u: + return "%s"%(u,a) + else: + return a + return a + +def author_url(author): + for pat, url in config.AUTHOR_RE_LIST: + if pat.search(author): + return url + return None + 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) @@ -359,19 +400,25 @@ class ParsedAuthor: def __str__(self): return " ".join(self.first+self.von+self.last+self.jr) -def _split(s,w=79): +def _split(s,w=79,indent=8): r = [] - s = s.replace("\n", " ") + s = re.sub(r"\s+", " ", s) + first = 1 + indentation = "" while len(s) > w: for i in xrange(w-1, 0, -1): if s[i] == ' ': - r.append(s[:i]) + r.append(indentation+s[:i]) s = s[i+1:] break else: - r.append(s[:w]) + r.append(indentation+s[:w]) s = s[w:] - r.append(s) + if first: + first = 0 + w -= indent + indentation = " "*indent + r.append(indentation+s) r.append("") return "\n".join(r) diff --git a/_template_.html b/_template_.html index ae9b05e..473fe4c 100644 --- a/_template_.html +++ b/_template_.html @@ -4,7 +4,7 @@ @@ -18,122 +18,26 @@ - - - -

Anonymity bibliography

%(choices)s

-
- - - - - - - - - + -

- - - - - - - - - - - - - - - - - - - + +
- +


%(sectiontypes)s:
+
+ + + - - - - - - - - - - - - - -
+
+

Publications by %(field)s

@@ -143,11 +47,10 @@ width="1" height="1" alt="">


+ - diff --git a/css/main.css b/css/main.css index e874ca4..8b336c9 100644 --- a/css/main.css +++ b/css/main.css @@ -77,9 +77,13 @@ H1 A, H2 A, H3 A, H4 A, H5 A, H6 A { } H1 { - color: #338; + color: #00B; } H2 { color: #006; } + +H3 { + color: #006; +} diff --git a/css/pubs.css b/css/pubs.css index 7281a1d..971616e 100644 --- a/css/pubs.css +++ b/css/pubs.css @@ -41,3 +41,24 @@ P.l2 { margin-top: 0.3em; margin-bottom: 0.3em; } + +TABLE.sidebar { + border-width: 2; + border-color: black; + border-style: solid; + background-color: #CFF; +} + +TD.bibtex { + font-family: lucidatypewriter, "Lucida Typewriter", Monaco, "Lucida Sans Unicode", monospace; + border-width: 2; + font-weight: normal; + border-color: black; + border-style: solid; + background-color: #DFF; +} + +PRE.bibtex { + font-family: lucidatypewriter, "Lucida Typewriter", Monaco, "Lucida Sans Unicode", monospace; + font-size: smaller; +} diff --git a/writeHTML.py b/writeHTML.py index 8037abd..2ffd7c7 100644 --- a/writeHTML.py +++ b/writeHTML.py @@ -5,36 +5,30 @@ import re import BibTeX import config -TEMPLATE_S, TEMPLATE_E = None, None - -def getTemplate(): - global TEMPLATE_S - global TEMPLATE_E - if not TEMPLATE_S: - f = open("_template_.html") - template = f.read() - f.close() - TEMPLATE_S, TEMPLATE_E = template.split("%(entries)s") - return TEMPLATE_S, TEMPLATE_E - -def url_untranslate(s): - s = s.replace(" ", "+") - s = re.sub(r'([%<>])', - lambda m: "%%%02x"%ord(m.group(1)), - s) - return s - -def writeBody(f, sections): +def getTemplate(name): + f = open(name+".html") + template = f.read() + f.close() + template_s, template_e = template.split("%(entries)s") + return template_s, template_e + +def writeBody(f, sections, section_urls): '''f: an open file sections: list of (sectionname, [list of BibTeXEntry])''' for s, entries in sections: - print >>f, ('

%s

'%(url_untranslate(s),s)) + u = section_urls.get(s) + if u: + print >>f, ('

%s

'%( + u, BibTeX.url_untranslate(s),s)) + else: + print >>f, ('

%s

'%( + BibTeX.url_untranslate(s),s)) print >>f, "" -def writeHTML(f, sections, sectionType, fieldName, choices): +def writeHTML(f, sections, sectionType, fieldName, choices, section_urls={}): """sections: list of (sectionname, [list of BibTeXEntry])''' sectionType: str fieldName: str @@ -43,7 +37,7 @@ def writeHTML(f, sections, sectionType, fieldName, choices): secStr = [] for s, _ in sections: secStr.append("

%s

\n"% - ((url_untranslate(s),s))) + ((BibTeX.url_untranslate(s),s))) secStr = "".join(secStr) # @@ -63,9 +57,9 @@ def writeHTML(f, sections, sectionType, fieldName, choices): 'sections' : secStr, } - header, footer = getTemplate() + header, footer = getTemplate("_template_") print >>f, header%fields - writeBody(f, sections) + writeBody(f, sections, section_urls) print >>f, footer%fields bib = BibTeX.parseFile(config.MASTER_BIB) @@ -86,7 +80,9 @@ entries = [ (s, BibTeX.sortEntriesByAuthor(ents)) f = open("topic.html", 'w') writeHTML(f, entries, "Topics", "topic", (("By topic", None), - ("By date", "./date.html"))) + ("By date", "./date.html"), + ("By author", "./author.html") + )) f.close() ## By date. @@ -107,7 +103,38 @@ if entries[-1][0] == 'Unknown': f = open("date.html", 'w') writeHTML(f, entries, "Years", "date", (("By topic", "./topic.html"), - ("By date", None))) + ("By date", None), + ("By author", "./author.html") + )) +f.close() + +## By author +entries, url_map = BibTeX.splitEntriesByAuthor(bib.entries) + +f = open("author.html", 'w') +writeHTML(f, entries, "Authors", "author", + (("By topic", "./topic.html"), + ("By date", "./date.html"), + ("By author", None), + ), + url_map) +f.close() + +## The big BibTeX file + +entries = bib.entries[:] +entries = [ (ent.key, ent) for ent in entries ] +entries.sort() +entries = [ ent[1] for ent in entries ] +header,footer = getTemplate("_template_bibtex") +f = open("bibtex.html", 'w') +print >>f, header % { 'command_line' : "" } +for ent in entries: + print >>f, ( + ("%s" + "
%s
") + %(BibTeX.url_untranslate(ent.key), ent.key, ent.format(90,8,1))) + ##print >>f, "

%s

" % ent.format(80,1) +print >>f, footer f.close() -## The big BibTeX -- cgit v1.2.3-70-g09d2