From 6e23d475308c780f0312795b201d7d44143d0f0e Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 19 May 2003 18:09:21 +0000 Subject: More work; get by-date working, cleanups, debugging, etc svn:r9 --- writeHTML.py | 125 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 99 insertions(+), 26 deletions(-) (limited to 'writeHTML.py') diff --git a/writeHTML.py b/writeHTML.py index 0d2d41a..8037abd 100644 --- a/writeHTML.py +++ b/writeHTML.py @@ -1,40 +1,113 @@ #!/usr/bin/python +import re + import BibTeX import config -bib = BibTeX.parseFile("anonbib.bib") +TEMPLATE_S, TEMPLATE_E = None, None -f = open("_template_.html") -template = f.read() -f.close() +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 -f = open("z.html", 'w') +def writeBody(f, sections): + '''f: an open file + sections: list of (sectionname, [list of BibTeXEntry])''' + for s, entries in sections: + print >>f, ('

%s

'%(url_untranslate(s),s)) + print >>f, "" -template_s, template_e = template.split("%(entries)s") +def writeHTML(f, sections, sectionType, fieldName, choices): + """sections: list of (sectionname, [list of BibTeXEntry])''' + sectionType: str + fieldName: str + choices: list of (choice, url)""" + # + secStr = [] + for s, _ in sections: + secStr.append("

%s

\n"% + ((url_untranslate(s),s))) + secStr = "".join(secStr) + + # + choiceStr = [] + for choice, url in choices: + if url: + choiceStr.append("%s"%(url, choice)) + else: + choiceStr.append(choice) + + choiceStr = "

%s

" % (" | ".join(choiceStr)) -print >>f, template_s + fields = { 'command_line' : "", + 'sectiontypes' : sectionType, + 'choices' : choiceStr, + 'field': fieldName, + 'sections' : secStr, + } -entries = BibTeX.splitEntriesBy(bib.entries, "www_section") -sections = entries.keys() -sections.sort() -if entries.has_key(None): - for ent in entries[None]: - ent['www_section'] = "Miscellaneous" + header, footer = getTemplate() + print >>f, header%fields + writeBody(f, sections) + print >>f, footer%fields + +bib = BibTeX.parseFile(config.MASTER_BIB) - entries["Miscellaneous"] = entries[None] - del entries[None] - sections.append("Miscellaneous") - sections = filter(None, sections) +##### Sorted views: -for s in sections: - #XXX print '

'; - print >>f, '

%s

'%s - print >>f, "
    " - for e in entries[s]: - print >>f, e.to_html() - print >>f, "
" - +## By topic. -print >>f, template_e +entries = BibTeX.sortEntriesBy(bib.entries, "www_section", "ZZZZZZZZZZZZZZZZZ") +entries = BibTeX.splitSortedEntriesBy(entries, "www_section") +if entries[-1][0] is None: + entries[-1] = ("Miscellaneous", entries[-1][1]) + +entries = [ (s, BibTeX.sortEntriesByAuthor(ents)) + for s, ents in entries + ] + +f = open("topic.html", 'w') +writeHTML(f, entries, "Topics", "topic", + (("By topic", None), + ("By date", "./date.html"))) +f.close() + +## By date. + +entries = BibTeX.sortEntriesByDate(bib.entries) +entries = BibTeX.splitSortedEntriesBy(entries, 'year') +if entries[-1][0] == None: + entries[-1] = ("Unknown", entries[-1][1]) +sections = [ ent[0] for ent in entries ] + +first_year = int(entries[0][1][0]['year']) +last_year = int(entries[-1][1][0].get('year', + entries[-2][1][0]['year'])) +years = map(str, range(first_year, last_year+1)) +if entries[-1][0] == 'Unknown': + years.append("Unknown") + +f = open("date.html", 'w') +writeHTML(f, entries, "Years", "date", + (("By topic", "./topic.html"), + ("By date", None))) +f.close() +## The big BibTeX -- cgit v1.2.3-70-g09d2