blob: cd42fd0060d46a630eec50178149bdf161791229 [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 Drakeb07216b2002-10-30 17:05:03 +000011import os.path
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
Fred Drakeb07216b2002-10-30 17:05:03 +000022 "address=", "iconserver=", "favicon=",
Fred Drakeca2b2e02000-10-05 05:11:57 +000023 "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"
Fred Drakeb07216b2002-10-30 17:05:03 +000030 favicon = None
Fred Drakeca2b2e02000-10-05 05:11:57 +000031
Fred Drakee03e1fe2002-04-05 17:34:50 +000032 # The "Aesop Meta Tag" is poorly described, and may only be used
33 # by the Aesop search engine (www.aesop.com), but doesn't hurt.
34 #
35 # There are a number of values this may take to roughly categorize
36 # a page. A page should be marked according to its primary
37 # category. Known values are:
38 # 'personal' -- personal-info
39 # 'information' -- information
40 # 'interactive' -- interactive media
41 # 'multimedia' -- multimedia presenetation (non-sales)
42 # 'sales' -- sales material
43 # 'links' -- links to other information pages
44 #
45 # Setting the aesop_type value to one of these strings will cause
46 # get_header() to add the appropriate <meta> tag to the <head>.
47 #
48 aesop_type = None
49
Fred Drakeca2b2e02000-10-05 05:11:57 +000050 def __init__(self):
51 self.args = []
52 self.variables = {"address": "",
53 "iconserver": "icons",
54 "imgtype": "gif",
55 "title": "Global Module Index",
56 }
57
58 def add_args(self, short=None, long=None):
59 if short:
Fred Drakeb258bed2001-02-12 15:30:22 +000060 self.__short_args = self.__short_args + short
Fred Drakeca2b2e02000-10-05 05:11:57 +000061 if long:
Fred Drakeb258bed2001-02-12 15:30:22 +000062 self.__long_args = self.__long_args + long
Fred Drakeca2b2e02000-10-05 05:11:57 +000063
64 def parse(self, args):
65 try:
66 opts, args = getopt.getopt(args, self.__short_args,
67 self.__long_args)
68 except getopt.error:
69 sys.stdout = sys.stderr
70 self.usage()
71 sys.exit(2)
Fred Drakeb258bed2001-02-12 15:30:22 +000072 self.args = self.args + args
Fred Drakeca2b2e02000-10-05 05:11:57 +000073 for opt, val in opts:
74 if opt in ("-a", "--address"):
Fred Drake071972e2002-10-16 15:30:17 +000075 val = val.strip()
Fred Drakeca2b2e02000-10-05 05:11:57 +000076 if val:
77 val = "<address>\n%s\n</address>\n" % val
78 self.variables["address"] = val
79 elif opt in ("-h", "--help"):
80 self.usage()
81 sys.exit()
82 elif opt in ("-o", "--output"):
83 self.outputfile = val
84 elif opt in ("-c", "--columns"):
85 self.columns = int(val)
86 elif opt == "--title":
87 self.variables["title"] = val.strip()
88 elif opt == "--uplink":
89 self.uplink = val.strip()
90 elif opt == "--uptitle":
91 self.uptitle = val.strip()
92 elif opt == "--iconserver":
93 self.variables["iconserver"] = val.strip() or "."
Fred Drakeb07216b2002-10-30 17:05:03 +000094 elif opt == "--favicon":
95 self.favicon = val.strip()
Fred Drakeca2b2e02000-10-05 05:11:57 +000096 else:
97 self.handle_option(opt, val)
98 if self.uplink and self.uptitle:
99 self.variables["uplinkalt"] = "up"
100 self.variables["uplinkicon"] = "up"
101 else:
102 self.variables["uplinkalt"] = ""
103 self.variables["uplinkicon"] = "blank"
104 self.variables["uplink"] = self.uplink
105 self.variables["uptitle"] = self.uptitle
106
107 def handle_option(self, opt, val):
108 raise getopt.error("option %s not recognized" % opt)
109
110 def get_header(self):
Fred Drakef10584c2001-10-22 15:07:16 +0000111 s = HEAD % self.variables
112 if self.uplink:
113 if self.uptitle:
Fred Drake4a473492002-10-30 21:32:40 +0000114 link = ('<link rel="up" href="%s" title="%s">\n '
115 '<link rel="start" href="%s" title="%s">'
116 % (self.uplink, self.uptitle,
117 self.uplink, self.uptitle))
Fred Drakef10584c2001-10-22 15:07:16 +0000118 else:
Fred Drake4a473492002-10-30 21:32:40 +0000119 link = ('<link rel="up" href="%s">\n '
120 '<link rel="start" href="%s">'
121 % (self.uplink, self.uplink))
Fred Drakef10584c2001-10-22 15:07:16 +0000122 repl = " %s\n</head>" % link
123 s = s.replace("</head>", repl, 1)
Fred Drakee03e1fe2002-04-05 17:34:50 +0000124 if self.aesop_type:
Fred Drakeb07216b2002-10-30 17:05:03 +0000125 meta = '<meta name="aesop" content="%s">\n ' % self.aesop_type
Fred Drakee03e1fe2002-04-05 17:34:50 +0000126 # Insert this in the middle of the head that's been
127 # generated so far, keeping <meta> and <link> elements in
128 # neat groups:
129 s = s.replace("<link ", meta + "<link ", 1)
Fred Drakeb07216b2002-10-30 17:05:03 +0000130 if self.favicon:
131 ext = os.path.splitext(self.favicon)[1]
132 if ext in (".gif", ".png"):
133 type = ' type="image/%s"' % ext[1:]
134 else:
135 type = ''
136 link = ('<link rel="SHORTCUT ICON" href="%s"%s>\n '
137 % (self.favicon, type))
138 s = s.replace("<link ", link + "<link ", 1)
Fred Drakef10584c2001-10-22 15:07:16 +0000139 return s
Fred Drakeca2b2e02000-10-05 05:11:57 +0000140
141 def get_footer(self):
142 return TAIL % self.variables
143
144 def get_output_file(self, filename=None):
145 if filename is None:
146 filename = self.outputfile
147 if filename == "-":
148 return sys.stdout
149 else:
150 return open(filename, "w")
151
152
153NAVIGATION = '''\
154<div class="navigation">
155<table width="100%%" cellpadding="0" cellspacing="2">
156<tr>
157<td><img width="32" height="32" align="bottom" border="0" alt=""
158 src="%(iconserver)s/blank.%(imgtype)s"></td>
159<td><a href="%(uplink)s"
160 title="%(uptitle)s"><img width="32" height="32" align="bottom" border="0"
161 alt="%(uplinkalt)s"
162 src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td>
163<td><img width="32" height="32" align="bottom" border="0" alt=""
164 src="%(iconserver)s/blank.%(imgtype)s"></td>
165<td align="center" width="100%%">%(title)s</td>
166<td><img width="32" height="32" align="bottom" border="0" alt=""
167 src="%(iconserver)s/blank.%(imgtype)s"></td>
168<td><img width="32" height="32" align="bottom" border="0" alt=""
169 src="%(iconserver)s/blank.%(imgtype)s"></td>
170<td><img width="32" height="32" align="bottom" border="0" alt=""
171 src="%(iconserver)s/blank.%(imgtype)s"></td>
172</tr></table>
173<b class="navlabel">Up:</b> <span class="sectref"><a href="%(uplink)s"
174 title="%(uptitle)s">%(uptitle)s</A></span>
175<br></div>
176'''
177
178HEAD = '''\
179<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
180<html>
181<head>
182 <title>%(title)s</title>
183 <meta name="description" content="%(title)s">
184 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
185 <link rel="STYLESHEET" href="lib/lib.css">
186</head>
187<body>
188''' + NAVIGATION + '''\
189<hr>
190
191<h2>%(title)s</h2>
192
193'''
194
195TAIL = "<hr>\n" + NAVIGATION + '''\
196%(address)s</body>
197</html>
198'''