aboutsummaryrefslogtreecommitdiffstats
path: root/writeHTML.py
blob: 3184ef074a8f67e142af4f49494c096855cc6576 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/python
# Copyright 2003-2007, Nick Mathewson.  See LICENSE for licensing info.

"""Generate indices by author, topic, date, and BibTeX key."""

import sys
import re
import os

assert sys.version_info[:3] >= (2,2,0)
os.umask(022)

import BibTeX
import config

def getTemplate(name):
    f = open(name)
    template = f.read()
    f.close()
    template_s, template_e = template.split("%(entries)s")
    return template_s, template_e

def pathLength(s):
    n = 0
    while s:
        parent, leaf = os.path.split(s)
        if leaf != '' and leaf != '.':
            n += 1
        s = parent
    return n

def writeBody(f, sections, section_urls, cache_path):
    '''f: an open file
       sections: list of (sectionname, [list of BibTeXEntry])
       section_urls: map from sectionname to external url'''
    for s, entries in sections:
        u = section_urls.get(s)
        sDisp = re.sub(r'\s+', ' ', s.strip())
        sDisp = sDisp.replace(" ", " ")
        if u:
            print >>f, ('<li><h3><a name="%s"></a><a href="%s">%s</a></h3>'%(
                (BibTeX.url_untranslate(s), u, sDisp)))
        else:
            print >>f, ('<li><h3><a name="%s">%s</a></h3>'%(
                BibTeX.url_untranslate(s),sDisp))
        print >>f, "<ul class='expand'>"
        for e in entries:
            print >>f, e.to_html(cache_path=cache_path)
        print >>f, "</ul></li>"

def writeHTML(f, sections, sectionType, fieldName, choices,
              tag, config, cache_url_path, section_urls={}):
    """sections: list of (sectionname, [list of BibTeXEntry])'''
       sectionType: str
       fieldName: str
       choices: list of (choice, url)"""

    title = config.TAG_TITLES[tag]
    short_title = config.TAG_SHORT_TITLES[tag]
    #
    secStr = []
    for s, _ in sections:
        hts = re.sub(r'\s+', ' ', s.strip())
        hts = s.replace(" ", "&nbsp;")
        secStr.append("<p class='l2'><a href='#%s'>%s</a></p>\n"%
                      ((BibTeX.url_untranslate(s),hts)))
    secStr = "".join(secStr)

    #
    tagListStr = []
    st = config.TAG_SHORT_TITLES.keys()
    st.sort()
    root = "../"*pathLength(config.TAG_DIRECTORIES[tag])
    if root == "": root = "."
    for t in st:
        name = config.TAG_SHORT_TITLES[t]
        if t == tag:
            tagListStr.append(name)
        else:
            url = BibTeX.smartJoin(root, config.TAG_DIRECTORIES[t], "date.html")
            tagListStr.append("<a href='%s'>%s</a>"%(url, name))
    tagListStr = "&nbsp;|&nbsp;".join(tagListStr)

    #
    choiceStr = []
    for choice, url in choices:
        if url:
            choiceStr.append("<a href='%s'>%s</a>"%(url, choice))
        else:
            choiceStr.append(choice)

    choiceStr = ("&nbsp;|&nbsp;".join(choiceStr))

    fields = { 'command_line' :  "",
               'sectiontypes' :  sectionType,
               'choices' : choiceStr,
               'field': fieldName,
               'sections' : secStr,
               'otherbibs' : tagListStr,
               'title': title,
               'short_title': short_title,
               "root" : root,
         }

    header, footer = getTemplate(config.TEMPLATE_FILE)
    print >>f, header%fields
    writeBody(f, sections, section_urls, cache_path=cache_url_path)
    print >>f, footer%fields

def writePageSet(config, bib, tag):
    if tag:
        bib_entries = [ b for b in bib.entries
                          if tag in b.get('www_tags', "").split() ]
    else:
        bib_entries = bib.entries[:]

    if not bib_entries:
        print >>sys.stderr, "No entries with tag %r; skipping"%tag
        return

    tagdir = config.TAG_DIRECTORIES[tag]
    outdir = os.path.join(config.OUTPUT_DIR, tagdir)
    cache_url_path = BibTeX.smartJoin("../"*pathLength(tagdir),
                                      config.CACHE_DIR)
    if not os.path.exists(outdir):
        os.makedirs(outdir, 0755)
    ##### Sorted views:

    ## By topic.

    entries = BibTeX.sortEntriesBy(bib_entries, "www_section", "ZZZZZZZZZZZZZZ")
    entries = BibTeX.splitSortedEntriesBy(entries, "www_section")
    if entries[-1][0].startswith("<span class='bad'>"):
        entries[-1] = ("Miscellaneous", entries[-1][1])

    entries = [ (s, BibTeX.sortEntriesByDate(ents))
                for s, ents in entries
                ]

    f = open(os.path.join(outdir,"topic.html"), 'w')
    writeHTML(f, entries, "Topics", "topic",
              (("By topic", None),
               ("By date", "./date.html"),
               ("By author", "./author.html")
               ),
              tag=tag, config=config,
              cache_url_path=cache_url_path)
    f.close()

    ## By date.

    entries = BibTeX.sortEntriesByDate(bib_entries)
    entries = BibTeX.splitSortedEntriesBy(entries, 'year')
    for idx in -1, -2:
        if entries[idx][0].startswith("<span class='bad'>"):
            entries[idx] = ("Unknown", entries[idx][1])
        elif entries[idx][0].startswith("forthcoming"):
            entries[idx] = ("Forthcoming", entries[idx][1])
    sections = [ ent[0] for ent in entries ]

    first_year = int(entries[0][1][0]['year'])
    try:
        last_year = int(entries[-1][1][0].get('year'))
    except ValueError:
        last_year = int(entries[-2][1][0].get('year'))

    years = map(str, range(first_year, last_year+1))
    if entries[-1][0] == 'Unknown':
        years.append("Unknown")

    f = open(os.path.join(outdir,"date.html"), 'w')
    writeHTML(f, entries, "Years", "date",
              (("By topic", "./topic.html"),
               ("By date", None),
               ("By author", "./author.html")
               ),
              tag=tag, config=config,
              cache_url_path=cache_url_path)
    f.close()

    ## By author
    entries, url_map = BibTeX.splitEntriesByAuthor(bib_entries)

    f = open(os.path.join(outdir,"author.html"), 'w')
    writeHTML(f, entries, "Authors", "author",
              (("By topic", "./topic.html"),
               ("By date", "./date.html"),
               ("By author", None),
              ),
              tag=tag, config=config,
              cache_url_path=cache_url_path,
              section_urls=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(config.BIBTEX_TEMPLATE_FILE)
    f = open(os.path.join(outdir,"bibtex.html"), 'w')
    print >>f, header % { 'command_line' : "",
                          'title': config.TAG_TITLES[tag] }
    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, footer
    f.close()


if __name__ == '__main__':
    if len(sys.argv) == 2:
        print "Loading from %s"%sys.argv[1]
    else:
        print >>sys.stderr, "Expected a single configuration file as an argument"
        sys.exit(1)
    config.load(sys.argv[1])

    bib = BibTeX.parseFile(config.MASTER_BIB)

    for tag in config.TAG_DIRECTORIES.keys():
        writePageSet(config, bib, tag)