summaryrefslogtreecommitdiffstats
path: root/Doc/tools/mkmodindex
blob: 14568d129ea1b42417f0fc910103dfbce91ad5a0 (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
#! /usr/bin/env python
#  -*- Python -*-

"""usage: %(program)s [options] file...

Supported options:

    --address addr
    -a addr         Set the address text to include at the end of the generated
                    HTML; this should be used for contact information.
    --columns cols
    -c cols         Set the number of columns each index section should be
                    displayed in.  The default is 1.
    --help
    -h              Display this help message.
    --letters
    -l              Split the output into sections by letter.
    --output file
    -o file         Write output to 'file' instead of standard out.
    --iconserver is Use 'is' as the directory containing icons for the
                    navigation bar.  The default is 'icons'.
    --title str     Set the page title to 'str'.  The default is 'Global
                    Module Index'.
    --uplink url    Set the upward link URL.  The default is './'.
    --uptitle str   Set the upward link title.  The default is 'Python
                    Documentation Index'.
"""
import buildindex
import getopt
import os
import re
import string
import sys


def usage():
    program = os.path.basename(sys.argv[0])
    print __doc__ % {"program": program}


def error(msg, rc=2):
    sys.stdout = sys.stderr
    print msg
    print
    usage()
    sys.exit(rc)


_rx = re.compile(
    "<dt><a href='(module-.*\.html)#l2h-\d+'><tt class='module'>"
    "([a-zA-Z_][a-zA-Z0-9_.]*</tt>(\s*<em>"
    "\(<span class='platform'>.*</span>\)</em>)?)</a>")

def main():
    outputfile = "-"
    columns = 1
    letters = 0
    uplink = "./"
    uptitle = "Python Documentation Index"
    variables = {"address": "",
                 "iconserver": "icons",
                 "imgtype": "gif",
                 "title": "Global Module Index",
                 "uplinkalt": "up",
                 "uplinkicon": "up",
                 }
    try:
        opts, args = getopt.getopt(sys.argv[1:], "a:c:hlo:",
                                   [# script controls:
                                    "columns=", "help", "letters", "output=",
                                    # content components:
                                    "address=", "iconserver=",
                                    "title=", "uplink=", "uptitle="])
    except getopt.error, msg:
        error(msg)
    for opt, val in opts:
        if opt in ("-a", "--address"):
            val = string.strip(val)
            variables["address"] = val and "<address>\n%s\n</address>\n" % val
        elif opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-o", "--output"):
            outputfile = val
        elif opt in ("-c", "--columns"):
            columns = string.atoi(val)
        elif opt in ("-l", "--letters"):
            letters = 1
        elif opt == "--title":
            variables["title"] = string.strip(val)
        elif opt == "--uplink":
            uplink = string.strip(val)
        elif opt == "--uptitle":
            uptitle = string.strip(val)
        elif opt == "--iconserver":
            variables["iconserver"] = string.strip(val) or "."
    if uplink and uptitle:
        variables["uplinkalt"] = "up"
        variables["uplinkicon"] = "up"
    else:
        variables["uplinkalt"] = ""
        variables["uplinkicon"] = "blank"
    variables["uplink"] = uplink
    variables["uptitle"] = uptitle
    if not args:
        args = ["-"]
    #
    # Collect the input data:
    #
    nodes = []
    seqno = 0
    has_plat_flag = 0
    for ifn in args:
        if ifn == "-":
            ifp = sys.stdin
            dirname = ''
        else:
            ifp = open(ifn)
            dirname = os.path.dirname(ifn)
        while 1:
            line = ifp.readline()
            if not line:
                break
            m = _rx.match(line)
            if m:
                # This line specifies a module!
                basename, modname = m.group(1, 2)
                has_plat_flag = has_plat_flag or m.group(3)
                linkfile = os.path.join(dirname, basename)
                nodes.append(buildindex.Node(
                    '<a href="%s">' % linkfile,
                    "<tt class=module>%s</tt>" % modname,
                    seqno))
                seqno = seqno + 1
        ifp.close()
    #
    # Generate all output:
    #
    num_nodes = len(nodes)
    # Here's the HTML generation:
    parts = [HEAD % variables,
             buildindex.process_nodes(nodes, columns, letters),
             TAIL % variables,
             ]
    if has_plat_flag:
        parts.insert(1, PLAT_DISCUSS)
    html = string.join(parts, '')
    program = os.path.basename(sys.argv[0])
    if outputfile == "-":
        sys.stdout.write(html)
        sys.stderr.write("%s: %d index nodes\n" % (program, num_nodes))
    else:
        open(outputfile, "w").write(html)
        print
        print "%s: %d index nodes" % (program, num_nodes)


PLAT_DISCUSS = """
<p> Some module names are followed by an annotation indicating what
platform they are available on.</p>

"""

NAVIGATION = """\
<div class=navigation>
<table width="100%%" cellpadding=0 cellspacing=2>
<tr>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="%(iconserver)s/blank.%(imgtype)s"></td>
<td><a href="%(uplink)s"
 title="%(uptitle)s"><img width=32 height=32 align=bottom border=0
 alt="%(uplinkalt)s"
 src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="%(iconserver)s/blank.%(imgtype)s"></td>
<td align=center bgcolor="#99CCFF" width="100%%">
 <b class=title>%(title)s</b></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="%(iconserver)s/blank.%(imgtype)s"></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="%(iconserver)s/blank.%(imgtype)s"></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="%(iconserver)s/blank.%(imgtype)s"></td>
</tr></table>
<b class=navlabel>Up:</b> <span class=sectref><a href="%(uplink)s"
 title="%(uptitle)s">%(uptitle)s</A></span>
<br></div>
"""

HEAD = """\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Global Module Index</title>
<meta name="description" content="%(title)s">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib/lib.css">
</head>
<body bgcolor=white>
""" + NAVIGATION + """\
<hr>

<h2>%(title)s</h2>

"""

TAIL = "<hr>\n" + NAVIGATION + """\
%(address)s</body>
</html>
"""

if __name__ == "__main__":
    main()