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