Fred Drake | 85788ed | 2000-10-05 05:20:55 +0000 | [diff] [blame] | 1 | """Miscellaneous support code shared by some of the tool scripts. |
| 2 | |
| 3 | This includes option parsing code, HTML formatting code, and a couple of |
| 4 | useful helpers. |
| 5 | |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 6 | """ |
| 7 | __version__ = '$Revision$' |
| 8 | |
| 9 | |
| 10 | import getopt |
Fred Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 11 | import string |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 12 | import sys |
| 13 | |
| 14 | |
| 15 | class 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 Drake | baf43c5 | 2002-02-04 21:15:42 +0000 | [diff] [blame] | 28 | uplink = "index.html" |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 29 | uptitle = "Python Documentation Index" |
| 30 | |
Fred Drake | e03e1fe | 2002-04-05 17:34:50 +0000 | [diff] [blame^] | 31 | # 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 Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 49 | 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 Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 59 | self.__short_args = self.__short_args + short |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 60 | if long: |
Fred Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 61 | self.__long_args = self.__long_args + long |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 62 | |
| 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 Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 71 | self.args = self.args + args |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 72 | for opt, val in opts: |
| 73 | if opt in ("-a", "--address"): |
Fred Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 74 | val = string.strip(val) |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 75 | 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 Drake | f10584c | 2001-10-22 15:07:16 +0000 | [diff] [blame] | 108 | 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 Drake | e03e1fe | 2002-04-05 17:34:50 +0000 | [diff] [blame^] | 117 | 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 Drake | f10584c | 2001-10-22 15:07:16 +0000 | [diff] [blame] | 123 | return s |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 124 | |
| 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 | |
| 137 | NAVIGATION = '''\ |
| 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 | |
| 162 | HEAD = '''\ |
| 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 | |
| 179 | TAIL = "<hr>\n" + NAVIGATION + '''\ |
| 180 | %(address)s</body> |
| 181 | </html> |
| 182 | ''' |