blob: 30d4575cb649a48254c31b346b9de381abc1913f [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:
114 link = ('<link rel="up" href="%s" title="%s">'
115 % (self.uplink, self.uptitle))
116 else:
117 link = '<link rel="up" href="%s">' % self.uplink
118 repl = " %s\n</head>" % link
119 s = s.replace("</head>", repl, 1)
Fred Drakee03e1fe2002-04-05 17:34:50 +0000120 if self.aesop_type:
Fred Drakeb07216b2002-10-30 17:05:03 +0000121 meta = '<meta name="aesop" content="%s">\n ' % self.aesop_type
Fred Drakee03e1fe2002-04-05 17:34:50 +0000122 # Insert this in the middle of the head that's been
123 # generated so far, keeping <meta> and <link> elements in
124 # neat groups:
125 s = s.replace("<link ", meta + "<link ", 1)
Fred Drakeb07216b2002-10-30 17:05:03 +0000126 if self.favicon:
127 ext = os.path.splitext(self.favicon)[1]
128 if ext in (".gif", ".png"):
129 type = ' type="image/%s"' % ext[1:]
130 else:
131 type = ''
132 link = ('<link rel="SHORTCUT ICON" href="%s"%s>\n '
133 % (self.favicon, type))
134 s = s.replace("<link ", link + "<link ", 1)
Fred Drakef10584c2001-10-22 15:07:16 +0000135 return s
Fred Drakeca2b2e02000-10-05 05:11:57 +0000136
137 def get_footer(self):
138 return TAIL % self.variables
139
140 def get_output_file(self, filename=None):
141 if filename is None:
142 filename = self.outputfile
143 if filename == "-":
144 return sys.stdout
145 else:
146 return open(filename, "w")
147
148
149NAVIGATION = '''\
150<div class="navigation">
151<table width="100%%" cellpadding="0" cellspacing="2">
152<tr>
153<td><img width="32" height="32" align="bottom" border="0" alt=""
154 src="%(iconserver)s/blank.%(imgtype)s"></td>
155<td><a href="%(uplink)s"
156 title="%(uptitle)s"><img width="32" height="32" align="bottom" border="0"
157 alt="%(uplinkalt)s"
158 src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td>
159<td><img width="32" height="32" align="bottom" border="0" alt=""
160 src="%(iconserver)s/blank.%(imgtype)s"></td>
161<td align="center" width="100%%">%(title)s</td>
162<td><img width="32" height="32" align="bottom" border="0" alt=""
163 src="%(iconserver)s/blank.%(imgtype)s"></td>
164<td><img width="32" height="32" align="bottom" border="0" alt=""
165 src="%(iconserver)s/blank.%(imgtype)s"></td>
166<td><img width="32" height="32" align="bottom" border="0" alt=""
167 src="%(iconserver)s/blank.%(imgtype)s"></td>
168</tr></table>
169<b class="navlabel">Up:</b> <span class="sectref"><a href="%(uplink)s"
170 title="%(uptitle)s">%(uptitle)s</A></span>
171<br></div>
172'''
173
174HEAD = '''\
175<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
176<html>
177<head>
178 <title>%(title)s</title>
179 <meta name="description" content="%(title)s">
180 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
181 <link rel="STYLESHEET" href="lib/lib.css">
182</head>
183<body>
184''' + NAVIGATION + '''\
185<hr>
186
187<h2>%(title)s</h2>
188
189'''
190
191TAIL = "<hr>\n" + NAVIGATION + '''\
192%(address)s</body>
193</html>
194'''