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