blob: bc17c6eccc69d349ae4ff9073b7624d4ab1bdedd [file] [log] [blame]
Fred Drake85788ed2000-10-05 05:20:55 +00001"""Miscellaneous support code shared by some of the tool scripts.
2
3This includes option parsing code, HTML formatting code, and a couple of
4useful helpers.
5
Fred Drakeca2b2e02000-10-05 05:11:57 +00006"""
7__version__ = '$Revision$'
8
9
10import getopt
Fred Drakeb258bed2001-02-12 15:30:22 +000011import string
Fred Drakeca2b2e02000-10-05 05:11:57 +000012import sys
13
14
15class Options:
16 __short_args = "a:c:ho:"
17 __long_args = [
18 # script controls
19 "columns=", "help", "output=",
20
21 # content components
22 "address=", "iconserver=",
23 "title=", "uplink=", "uptitle="]
24
25 outputfile = "-"
26 columns = 1
27 letters = 0
Fred Drakebaf43c52002-02-04 21:15:42 +000028 uplink = "index.html"
Fred Drakeca2b2e02000-10-05 05:11:57 +000029 uptitle = "Python Documentation Index"
30
Fred Drakee03e1fe2002-04-05 17:34:50 +000031 # The "Aesop Meta Tag" is poorly described, and may only be used
32 # by the Aesop search engine (www.aesop.com), but doesn't hurt.
33 #
34 # There are a number of values this may take to roughly categorize
35 # a page. A page should be marked according to its primary
36 # category. Known values are:
37 # 'personal' -- personal-info
38 # 'information' -- information
39 # 'interactive' -- interactive media
40 # 'multimedia' -- multimedia presenetation (non-sales)
41 # 'sales' -- sales material
42 # 'links' -- links to other information pages
43 #
44 # Setting the aesop_type value to one of these strings will cause
45 # get_header() to add the appropriate <meta> tag to the <head>.
46 #
47 aesop_type = None
48
Fred Drakeca2b2e02000-10-05 05:11:57 +000049 def __init__(self):
50 self.args = []
51 self.variables = {"address": "",
52 "iconserver": "icons",
53 "imgtype": "gif",
54 "title": "Global Module Index",
55 }
56
57 def add_args(self, short=None, long=None):
58 if short:
Fred Drakeb258bed2001-02-12 15:30:22 +000059 self.__short_args = self.__short_args + short
Fred Drakeca2b2e02000-10-05 05:11:57 +000060 if long:
Fred Drakeb258bed2001-02-12 15:30:22 +000061 self.__long_args = self.__long_args + long
Fred Drakeca2b2e02000-10-05 05:11:57 +000062
63 def parse(self, args):
64 try:
65 opts, args = getopt.getopt(args, self.__short_args,
66 self.__long_args)
67 except getopt.error:
68 sys.stdout = sys.stderr
69 self.usage()
70 sys.exit(2)
Fred Drakeb258bed2001-02-12 15:30:22 +000071 self.args = self.args + args
Fred Drakeca2b2e02000-10-05 05:11:57 +000072 for opt, val in opts:
73 if opt in ("-a", "--address"):
Fred Drakeb258bed2001-02-12 15:30:22 +000074 val = string.strip(val)
Fred Drakeca2b2e02000-10-05 05:11:57 +000075 if val:
76 val = "<address>\n%s\n</address>\n" % val
77 self.variables["address"] = val
78 elif opt in ("-h", "--help"):
79 self.usage()
80 sys.exit()
81 elif opt in ("-o", "--output"):
82 self.outputfile = val
83 elif opt in ("-c", "--columns"):
84 self.columns = int(val)
85 elif opt == "--title":
86 self.variables["title"] = val.strip()
87 elif opt == "--uplink":
88 self.uplink = val.strip()
89 elif opt == "--uptitle":
90 self.uptitle = val.strip()
91 elif opt == "--iconserver":
92 self.variables["iconserver"] = val.strip() or "."
93 else:
94 self.handle_option(opt, val)
95 if self.uplink and self.uptitle:
96 self.variables["uplinkalt"] = "up"
97 self.variables["uplinkicon"] = "up"
98 else:
99 self.variables["uplinkalt"] = ""
100 self.variables["uplinkicon"] = "blank"
101 self.variables["uplink"] = self.uplink
102 self.variables["uptitle"] = self.uptitle
103
104 def handle_option(self, opt, val):
105 raise getopt.error("option %s not recognized" % opt)
106
107 def get_header(self):
Fred Drakef10584c2001-10-22 15:07:16 +0000108 s = HEAD % self.variables
109 if self.uplink:
110 if self.uptitle:
111 link = ('<link rel="up" href="%s" title="%s">'
112 % (self.uplink, self.uptitle))
113 else:
114 link = '<link rel="up" href="%s">' % self.uplink
115 repl = " %s\n</head>" % link
116 s = s.replace("</head>", repl, 1)
Fred Drakee03e1fe2002-04-05 17:34:50 +0000117 if self.aesop_type:
118 meta = '\n <meta name="aesop" content="%s">'
119 # Insert this in the middle of the head that's been
120 # generated so far, keeping <meta> and <link> elements in
121 # neat groups:
122 s = s.replace("<link ", meta + "<link ", 1)
Fred Drakef10584c2001-10-22 15:07:16 +0000123 return s
Fred Drakeca2b2e02000-10-05 05:11:57 +0000124
125 def get_footer(self):
126 return TAIL % self.variables
127
128 def get_output_file(self, filename=None):
129 if filename is None:
130 filename = self.outputfile
131 if filename == "-":
132 return sys.stdout
133 else:
134 return open(filename, "w")
135
136
137NAVIGATION = '''\
138<div class="navigation">
139<table width="100%%" cellpadding="0" cellspacing="2">
140<tr>
141<td><img width="32" height="32" align="bottom" border="0" alt=""
142 src="%(iconserver)s/blank.%(imgtype)s"></td>
143<td><a href="%(uplink)s"
144 title="%(uptitle)s"><img width="32" height="32" align="bottom" border="0"
145 alt="%(uplinkalt)s"
146 src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td>
147<td><img width="32" height="32" align="bottom" border="0" alt=""
148 src="%(iconserver)s/blank.%(imgtype)s"></td>
149<td align="center" width="100%%">%(title)s</td>
150<td><img width="32" height="32" align="bottom" border="0" alt=""
151 src="%(iconserver)s/blank.%(imgtype)s"></td>
152<td><img width="32" height="32" align="bottom" border="0" alt=""
153 src="%(iconserver)s/blank.%(imgtype)s"></td>
154<td><img width="32" height="32" align="bottom" border="0" alt=""
155 src="%(iconserver)s/blank.%(imgtype)s"></td>
156</tr></table>
157<b class="navlabel">Up:</b> <span class="sectref"><a href="%(uplink)s"
158 title="%(uptitle)s">%(uptitle)s</A></span>
159<br></div>
160'''
161
162HEAD = '''\
163<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
164<html>
165<head>
166 <title>%(title)s</title>
167 <meta name="description" content="%(title)s">
168 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
169 <link rel="STYLESHEET" href="lib/lib.css">
170</head>
171<body>
172''' + NAVIGATION + '''\
173<hr>
174
175<h2>%(title)s</h2>
176
177'''
178
179TAIL = "<hr>\n" + NAVIGATION + '''\
180%(address)s</body>
181</html>
182'''