Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # -*- Python -*- |
| 3 | |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 4 | """usage: %(program)s [options] file... |
| 5 | |
| 6 | Supported options: |
| 7 | |
| 8 | --address addr |
| 9 | -a addr Set the address text to include at the end of the generated |
| 10 | HTML; this should be used for contact information. |
| 11 | --columns cols |
| 12 | -c cols Set the number of columns each index section should be |
| 13 | displayed in. The default is 1. |
| 14 | --help |
| 15 | -h Display this help message. |
| 16 | --letters |
| 17 | -l Split the output into sections by letter. |
| 18 | --output file |
| 19 | -o file Write output to 'file' instead of standard out. |
| 20 | --iconserver is Use 'is' as the directory containing icons for the |
| 21 | navigation bar. The default is 'icons'. |
| 22 | --title str Set the page title to 'str'. The default is 'Global |
| 23 | Module Index'. |
| 24 | --uplink url Set the upward link URL. The default is './'. |
| 25 | --uptitle str Set the upward link title. The default is 'Python |
| 26 | Documentation Index'. |
| 27 | """ |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 28 | import os |
| 29 | import re |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 30 | import sys |
| 31 | |
Fred Drake | 4a47349 | 2002-10-30 21:32:40 +0000 | [diff] [blame] | 32 | from xml.sax.saxutils import quoteattr |
| 33 | |
Fred Drake | 06912b7 | 2002-10-16 15:29:07 +0000 | [diff] [blame] | 34 | import buildindex |
| 35 | import support |
| 36 | |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 37 | |
Fred Drake | 01a110b | 2000-10-05 05:14:26 +0000 | [diff] [blame] | 38 | class IndexOptions(support.Options): |
Fred Drake | e03e1fe | 2002-04-05 17:34:50 +0000 | [diff] [blame] | 39 | aesop_type = "links" |
| 40 | |
Fred Drake | 01a110b | 2000-10-05 05:14:26 +0000 | [diff] [blame] | 41 | def __init__(self): |
| 42 | support.Options.__init__(self) |
| 43 | self.add_args("l", ["letters"]) |
| 44 | self.letters = 0 |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 45 | |
Fred Drake | 01a110b | 2000-10-05 05:14:26 +0000 | [diff] [blame] | 46 | def handle_option(self, opt, val): |
| 47 | if opt in ("-l", "--letters"): |
| 48 | self.letters = 1 |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 49 | |
Fred Drake | 01a110b | 2000-10-05 05:14:26 +0000 | [diff] [blame] | 50 | def usage(self): |
| 51 | program = os.path.basename(sys.argv[0]) |
| 52 | print __doc__ % {"program": program} |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 53 | |
Fred Drake | 4a47349 | 2002-10-30 21:32:40 +0000 | [diff] [blame] | 54 | links = [ |
| 55 | ('author', 'acks.html', 'Acknowledgements'), |
| 56 | ('help', 'about.html', 'About the Python Documentation'), |
| 57 | ] |
| 58 | |
| 59 | def get_header(self): |
| 60 | header = support.Options.get_header(self) |
| 61 | s = '' |
| 62 | for rel, href, title in self.links: |
| 63 | s += '<link rel="%s" href="%s"' % (rel, href) |
| 64 | if title: |
| 65 | s += ' title=' + quoteattr(title) |
| 66 | s += '>\n ' |
| 67 | return header.replace("<link ", s + "<link ", 1) |
| 68 | |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 69 | |
Fred Drake | 0a0ef86 | 2000-11-28 16:20:50 +0000 | [diff] [blame] | 70 | class Node(buildindex.Node): |
Fred Drake | 9ca78ac | 2001-06-22 17:11:30 +0000 | [diff] [blame] | 71 | def __init__(self, link, str, seqno, platinfo): |
| 72 | self.annotation = platinfo or None |
| 73 | if str[0][-5:] == "</tt>": |
| 74 | str = str[:-5] |
| 75 | self.modname = str |
Fred Drake | 0a0ef86 | 2000-11-28 16:20:50 +0000 | [diff] [blame] | 76 | buildindex.Node.__init__(self, link, self.modname, seqno) |
Fred Drake | 9ca78ac | 2001-06-22 17:11:30 +0000 | [diff] [blame] | 77 | if platinfo: |
| 78 | s = '<tt class="module">%s</tt> %s' \ |
| 79 | % (self.modname, self.annotation) |
| 80 | else: |
| 81 | s = '<tt class="module">%s</tt>' % str |
| 82 | self.text = [s] |
Fred Drake | 0a0ef86 | 2000-11-28 16:20:50 +0000 | [diff] [blame] | 83 | |
| 84 | def __str__(self): |
Fred Drake | 9ca78ac | 2001-06-22 17:11:30 +0000 | [diff] [blame] | 85 | if self.annotation: |
| 86 | return '<tt class="module">%s</tt> %s' \ |
| 87 | % (self.modname, self.annotation) |
| 88 | else: |
| 89 | return '<tt class="module">%s</tt>' % self.modname |
Fred Drake | 0a0ef86 | 2000-11-28 16:20:50 +0000 | [diff] [blame] | 90 | |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 91 | _rx = re.compile( |
Fred Drake | 9ca78ac | 2001-06-22 17:11:30 +0000 | [diff] [blame] | 92 | "<dt><a href=['\"](module-.*\.html)(?:#l2h-\d+)?['\"]>" |
| 93 | "<tt class=['\"]module['\"]>([a-zA-Z_][a-zA-Z0-9_.]*)</tt>\s*(<em>" |
| 94 | "\(<span class=['\"]platform['\"]>.*</span>\)</em>)?</a>") |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 95 | |
| 96 | def main(): |
Fred Drake | 01a110b | 2000-10-05 05:14:26 +0000 | [diff] [blame] | 97 | options = IndexOptions() |
| 98 | options.variables["title"] = "Global Module Index" |
| 99 | options.parse(sys.argv[1:]) |
| 100 | args = options.args |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 101 | if not args: |
| 102 | args = ["-"] |
| 103 | # |
| 104 | # Collect the input data: |
| 105 | # |
| 106 | nodes = [] |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 107 | has_plat_flag = 0 |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 108 | for ifn in args: |
| 109 | if ifn == "-": |
| 110 | ifp = sys.stdin |
| 111 | dirname = '' |
| 112 | else: |
| 113 | ifp = open(ifn) |
| 114 | dirname = os.path.dirname(ifn) |
| 115 | while 1: |
| 116 | line = ifp.readline() |
| 117 | if not line: |
| 118 | break |
| 119 | m = _rx.match(line) |
| 120 | if m: |
| 121 | # This line specifies a module! |
Fred Drake | 9ca78ac | 2001-06-22 17:11:30 +0000 | [diff] [blame] | 122 | basename, modname, platinfo = m.group(1, 2, 3) |
| 123 | has_plat_flag = has_plat_flag or platinfo |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 124 | linkfile = os.path.join(dirname, basename) |
Fred Drake | 9ca78ac | 2001-06-22 17:11:30 +0000 | [diff] [blame] | 125 | nodes.append(Node('<a href="%s">' % linkfile, modname, |
| 126 | len(nodes), platinfo)) |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 127 | ifp.close() |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 128 | # |
| 129 | # Generate all output: |
| 130 | # |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 131 | num_nodes = len(nodes) |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 132 | # Here's the HTML generation: |
Fred Drake | 01a110b | 2000-10-05 05:14:26 +0000 | [diff] [blame] | 133 | parts = [options.get_header(), |
| 134 | buildindex.process_nodes(nodes, options.columns, options.letters), |
| 135 | options.get_footer(), |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 136 | ] |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 137 | if has_plat_flag: |
| 138 | parts.insert(1, PLAT_DISCUSS) |
Fred Drake | 06912b7 | 2002-10-16 15:29:07 +0000 | [diff] [blame] | 139 | html = ''.join(parts) |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 140 | program = os.path.basename(sys.argv[0]) |
Fred Drake | 01a110b | 2000-10-05 05:14:26 +0000 | [diff] [blame] | 141 | fp = options.get_output_file() |
Fred Drake | 06912b7 | 2002-10-16 15:29:07 +0000 | [diff] [blame] | 142 | fp.write(html.rstrip() + "\n") |
Fred Drake | 01a110b | 2000-10-05 05:14:26 +0000 | [diff] [blame] | 143 | if options.outputfile == "-": |
Fred Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 144 | sys.stderr.write("%s: %d index nodes\n" % (program, num_nodes)) |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 145 | else: |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 146 | print |
| 147 | print "%s: %d index nodes" % (program, num_nodes) |
| 148 | |
| 149 | |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 150 | PLAT_DISCUSS = """ |
| 151 | <p> Some module names are followed by an annotation indicating what |
| 152 | platform they are available on.</p> |
| 153 | |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 154 | """ |
| 155 | |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 156 | |
| 157 | if __name__ == "__main__": |
| 158 | main() |