diff options
Diffstat (limited to 'writeHTML.py')
| -rw-r--r-- | writeHTML.py | 81 |
1 files changed, 54 insertions, 27 deletions
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(name): + f = open(name+".html") + template = f.read() + f.close() + template_s, template_e = template.split("%(entries)s") + return template_s, template_e -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 writeBody(f, sections, section_urls): '''f: an open file sections: list of (sectionname, [list of BibTeXEntry])''' for s, entries in sections: - print >>f, ('<h2><a name="%s">%s</a></h2>'%(url_untranslate(s),s)) + u = section_urls.get(s) + if u: + print >>f, ('<h3><a name="%s"><a href="%s">%s</a></a></h3>'%( + u, BibTeX.url_untranslate(s),s)) + else: + print >>f, ('<h3><a name="%s">%s</a></h3>'%( + BibTeX.url_untranslate(s),s)) print >>f, "<ul class='expand'>" for e in entries: print >>f, e.to_html() print >>f, "</ul>" -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("<p class='l2'><a href='#%s'>%s</a></p>\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, ( + ("<tr><td class='bibtex'><a name='%s'>%s</a>" + "<pre class='bibtex'>%s</pre></td></tr>") + %(BibTeX.url_untranslate(ent.key), ent.key, ent.format(90,8,1))) + ##print >>f, "<p><pre>%s</pre></p>" % ent.format(80,1) +print >>f, footer f.close() -## The big BibTeX |
