aboutsummaryrefslogtreecommitdiffstats
path: root/writeHTML.py
blob: 2ffd7c77ae3e0f853f1f7c0b31eff9c5da5dd058 (plain)
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
#!/usr/bin/python

import re

import BibTeX
import config

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:
        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, section_urls={}):
    """sections: list of (sectionname, [list of BibTeXEntry])'''
       sectionType: str
       fieldName: str
       choices: list of (choice, url)"""
    #
    secStr = []
    for s, _ in sections:
        secStr.append("<p class='l2'><a href='#%s'>%s</a></p>\n"%
                      ((BibTeX.url_untranslate(s),s)))
    secStr = "".join(secStr)
    
    # 
    choiceStr = []
    for choice, url in choices:
        if url:
            choiceStr.append("<a href='%s'>%s</a>"%(url, choice))
        else:
            choiceStr.append(choice)
        
    choiceStr = "<p align='center'>%s</p>" % (" | ".join(choiceStr))

    fields = { 'command_line' :  "",
               'sectiontypes' :  sectionType,
               'choices' : choiceStr,
               'field': fieldName,
               'sections' : secStr,
         }

    header, footer = getTemplate("_template_")
    print >>f, header%fields
    writeBody(f, sections, section_urls)
    print >>f, footer%fields
    
bib = BibTeX.parseFile(config.MASTER_BIB)

##### Sorted views:

## By topic.

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"),
           ("By author", "./author.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),
           ("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()