mkmodindex 4.21 KB
Newer Older
1 2 3
#! /usr/bin/env python
#  -*- Python -*-

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
"""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'.
"""
28 29 30 31
import buildindex
import os
import re
import string
32
import support
33 34 35
import sys


36
class IndexOptions(support.Options):
37 38
    aesop_type = "links"

39 40 41 42
    def __init__(self):
        support.Options.__init__(self)
        self.add_args("l", ["letters"])
        self.letters = 0
43

44 45 46
    def handle_option(self, opt, val):
        if opt in ("-l", "--letters"):
            self.letters = 1
47

48 49 50
    def usage(self):
        program = os.path.basename(sys.argv[0])
        print __doc__ % {"program": program}
51 52


53
class Node(buildindex.Node):
54 55 56 57 58
    def __init__(self, link, str, seqno, platinfo):
        self.annotation = platinfo or None
        if str[0][-5:] == "</tt>":
            str = str[:-5]
        self.modname = str
59
        buildindex.Node.__init__(self, link, self.modname, seqno)
60 61 62 63 64 65
        if platinfo:
            s = '<tt class="module">%s</tt> %s' \
                % (self.modname, self.annotation)
        else:
            s = '<tt class="module">%s</tt>' % str
        self.text = [s]
66 67

    def __str__(self):
68 69 70 71 72
        if self.annotation:
            return '<tt class="module">%s</tt> %s' \
                   % (self.modname, self.annotation)
        else:
            return '<tt class="module">%s</tt>' % self.modname
73

74
_rx = re.compile(
75 76 77
    "<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>")
78 79

def main():
80 81 82 83
    options = IndexOptions()
    options.variables["title"] = "Global Module Index"
    options.parse(sys.argv[1:])
    args = options.args
84 85 86 87 88 89
    if not args:
        args = ["-"]
    #
    # Collect the input data:
    #
    nodes = []
90
    has_plat_flag = 0
91 92 93 94 95 96 97 98 99 100 101 102 103 104
    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!
105 106
                basename, modname, platinfo = m.group(1, 2, 3)
                has_plat_flag = has_plat_flag or platinfo
107
                linkfile = os.path.join(dirname, basename)
108 109
                nodes.append(Node('<a href="%s">' % linkfile, modname,
                                  len(nodes), platinfo))
110
        ifp.close()
111 112 113
    #
    # Generate all output:
    #
114
    num_nodes = len(nodes)
115
    # Here's the HTML generation:
116 117 118
    parts = [options.get_header(),
             buildindex.process_nodes(nodes, options.columns, options.letters),
             options.get_footer(),
119
             ]
120 121 122
    if has_plat_flag:
        parts.insert(1, PLAT_DISCUSS)
    html = string.join(parts, '')
123
    program = os.path.basename(sys.argv[0])
124
    fp = options.get_output_file()
125
    fp.write(string.rstrip(html) + "\n")
126
    if options.outputfile == "-":
127
        sys.stderr.write("%s: %d index nodes\n" % (program, num_nodes))
128 129 130 131 132
    else:
        print
        print "%s: %d index nodes" % (program, num_nodes)


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

137 138 139 140 141
"""


if __name__ == "__main__":
    main()