annotate HTML.py @ 0:8be2feb96994

"planemo upload commit cb65588391944306ff3cb32a23e1c28f65122014"
author cstrittmatter
date Fri, 11 Mar 2022 15:50:35 -0500
parents
children
rev   line source
cstrittmatter@0 1 #!/usr/bin/python
cstrittmatter@0 2 # -*- coding: iso-8859-1 -*-
cstrittmatter@0 3 """
cstrittmatter@0 4 HTML.py - v0.04 2009-07-28 Philippe Lagadec
cstrittmatter@0 5
cstrittmatter@0 6 This module provides a few classes to easily generate HTML code such as tables
cstrittmatter@0 7 and lists.
cstrittmatter@0 8
cstrittmatter@0 9 Project website: http://www.decalage.info/python/html
cstrittmatter@0 10
cstrittmatter@0 11 License: CeCILL (open-source GPL compatible), see source code for details.
cstrittmatter@0 12 http://www.cecill.info
cstrittmatter@0 13 """
cstrittmatter@0 14
cstrittmatter@0 15 __version__ = '0.04'
cstrittmatter@0 16 __date__ = '2009-07-28'
cstrittmatter@0 17 __author__ = 'Philippe Lagadec'
cstrittmatter@0 18
cstrittmatter@0 19 #--- LICENSE ------------------------------------------------------------------
cstrittmatter@0 20
cstrittmatter@0 21 # Copyright Philippe Lagadec - see http://www.decalage.info/contact for contact info
cstrittmatter@0 22 #
cstrittmatter@0 23 # This module provides a few classes to easily generate HTML tables and lists.
cstrittmatter@0 24 #
cstrittmatter@0 25 # This software is governed by the CeCILL license under French law and
cstrittmatter@0 26 # abiding by the rules of distribution of free software. You can use,
cstrittmatter@0 27 # modify and/or redistribute the software under the terms of the CeCILL
cstrittmatter@0 28 # license as circulated by CEA, CNRS and INRIA at the following URL
cstrittmatter@0 29 # "http://www.cecill.info".
cstrittmatter@0 30 #
cstrittmatter@0 31 # A copy of the CeCILL license is also provided in these attached files:
cstrittmatter@0 32 # Licence_CeCILL_V2-en.html and Licence_CeCILL_V2-fr.html
cstrittmatter@0 33 #
cstrittmatter@0 34 # As a counterpart to the access to the source code and rights to copy,
cstrittmatter@0 35 # modify and redistribute granted by the license, users are provided only
cstrittmatter@0 36 # with a limited warranty and the software's author, the holder of the
cstrittmatter@0 37 # economic rights, and the successive licensors have only limited
cstrittmatter@0 38 # liability.
cstrittmatter@0 39 #
cstrittmatter@0 40 # In this respect, the user's attention is drawn to the risks associated
cstrittmatter@0 41 # with loading, using, modifying and/or developing or reproducing the
cstrittmatter@0 42 # software by the user in light of its specific status of free software,
cstrittmatter@0 43 # that may mean that it is complicated to manipulate, and that also
cstrittmatter@0 44 # therefore means that it is reserved for developers and experienced
cstrittmatter@0 45 # professionals having in-depth computer knowledge. Users are therefore
cstrittmatter@0 46 # encouraged to load and test the software's suitability as regards their
cstrittmatter@0 47 # requirements in conditions enabling the security of their systems and/or
cstrittmatter@0 48 # data to be ensured and, more generally, to use and operate it in the
cstrittmatter@0 49 # same conditions as regards security.
cstrittmatter@0 50 #
cstrittmatter@0 51 # The fact that you are presently reading this means that you have had
cstrittmatter@0 52 # knowledge of the CeCILL license and that you accept its terms.
cstrittmatter@0 53
cstrittmatter@0 54 #--- THANKS --------------------------------------------------------------------
cstrittmatter@0 55
cstrittmatter@0 56 # - Michal Cernoevic, for the idea of column styles.
cstrittmatter@0 57
cstrittmatter@0 58 #--- REFERENCES ----------------------------------------------------------------
cstrittmatter@0 59
cstrittmatter@0 60 # HTML 4.01 specs: http://www.w3.org/TR/html4/struct/tables.html
cstrittmatter@0 61
cstrittmatter@0 62 # Colors: http://www.w3.org/TR/html4/types.html#type-color
cstrittmatter@0 63
cstrittmatter@0 64 # Columns alignement and style, one of the oldest and trickiest bugs in Mozilla:
cstrittmatter@0 65 # https://bugzilla.mozilla.org/show_bug.cgi?id=915
cstrittmatter@0 66
cstrittmatter@0 67
cstrittmatter@0 68 #--- CONSTANTS -----------------------------------------------------------------
cstrittmatter@0 69
cstrittmatter@0 70 # Table style to get thin black lines in Mozilla/Firefox instead of 3D borders
cstrittmatter@0 71 #TABLE_STYLE_THINBORDER = "border: 1px solid #000000; border-collapse: collapse;"
cstrittmatter@0 72 #TABLE_STYLE_THINBORDER = "border: 1px solid #000000;"
cstrittmatter@0 73
cstrittmatter@0 74
cstrittmatter@0 75 #=== CLASSES ===================================================================
cstrittmatter@0 76
cstrittmatter@0 77 class TableCell (object):
cstrittmatter@0 78 """
cstrittmatter@0 79 a TableCell object is used to create a cell in a HTML table. (TD or TH)
cstrittmatter@0 80
cstrittmatter@0 81 Attributes:
cstrittmatter@0 82 - text: text in the cell (may contain HTML tags). May be any object which
cstrittmatter@0 83 can be converted to a string using str().
cstrittmatter@0 84 - header: bool, false for a normal data cell (TD), true for a header cell (TH)
cstrittmatter@0 85 - bgcolor: str, background color
cstrittmatter@0 86 - width: str, width
cstrittmatter@0 87 - align: str, horizontal alignement (left, center, right, justify or char)
cstrittmatter@0 88 - char: str, alignment character, decimal point if not specified
cstrittmatter@0 89 - charoff: str, see HTML specs
cstrittmatter@0 90 - valign: str, vertical alignment (top|middle|bottom|baseline)
cstrittmatter@0 91 - style: str, CSS style
cstrittmatter@0 92 - attribs: dict, additional attributes for the TD/TH tag
cstrittmatter@0 93
cstrittmatter@0 94 Reference: http://www.w3.org/TR/html4/struct/tables.html#h-11.2.6
cstrittmatter@0 95 """
cstrittmatter@0 96
cstrittmatter@0 97 def __init__(self, text="", bgcolor=None, header=False, width=None,
cstrittmatter@0 98 align=None, char=None, charoff=None, valign=None, style=None,
cstrittmatter@0 99 attribs=None):
cstrittmatter@0 100 """TableCell constructor"""
cstrittmatter@0 101 self.text = text
cstrittmatter@0 102 self.bgcolor = bgcolor
cstrittmatter@0 103 self.header = header
cstrittmatter@0 104 self.width = width
cstrittmatter@0 105 self.align = align
cstrittmatter@0 106 self.char = char
cstrittmatter@0 107 self.charoff = charoff
cstrittmatter@0 108 self.valign = valign
cstrittmatter@0 109 self.style = style
cstrittmatter@0 110 self.attribs = attribs
cstrittmatter@0 111 if attribs==None:
cstrittmatter@0 112 self.attribs = {}
cstrittmatter@0 113
cstrittmatter@0 114 def __str__(self):
cstrittmatter@0 115 """return the HTML code for the table cell as a string"""
cstrittmatter@0 116 attribs_str = ""
cstrittmatter@0 117 if self.bgcolor: self.attribs['bgcolor'] = self.bgcolor
cstrittmatter@0 118 if self.width: self.attribs['width'] = self.width
cstrittmatter@0 119 if self.align: self.attribs['align'] = self.align
cstrittmatter@0 120 if self.char: self.attribs['char'] = self.char
cstrittmatter@0 121 if self.charoff: self.attribs['charoff'] = self.charoff
cstrittmatter@0 122 if self.valign: self.attribs['valign'] = self.valign
cstrittmatter@0 123 if self.style: self.attribs['style'] = self.style
cstrittmatter@0 124 for attr in self.attribs:
cstrittmatter@0 125 attribs_str += ' %s="%s"' % (attr, self.attribs[attr])
cstrittmatter@0 126 if self.text:
cstrittmatter@0 127 text = str(self.text)
cstrittmatter@0 128 else:
cstrittmatter@0 129 # An empty cell should at least contain a non-breaking space
cstrittmatter@0 130 text = ' '
cstrittmatter@0 131 if self.header:
cstrittmatter@0 132 return '<th%s>%s</th>' % (attribs_str, text)
cstrittmatter@0 133 else:
cstrittmatter@0 134 return '<td%s>%s</td>' % (attribs_str, text)
cstrittmatter@0 135
cstrittmatter@0 136 #-------------------------------------------------------------------------------
cstrittmatter@0 137
cstrittmatter@0 138 class TableRow (object):
cstrittmatter@0 139 """
cstrittmatter@0 140 a TableRow object is used to create a row in a HTML table. (TR tag)
cstrittmatter@0 141
cstrittmatter@0 142 Attributes:
cstrittmatter@0 143 - cells: list, tuple or any iterable, containing one string or TableCell
cstrittmatter@0 144 object for each cell
cstrittmatter@0 145 - header: bool, true for a header row (TH), false for a normal data row (TD)
cstrittmatter@0 146 - bgcolor: str, background color
cstrittmatter@0 147 - col_align, col_valign, col_char, col_charoff, col_styles: see Table class
cstrittmatter@0 148 - attribs: dict, additional attributes for the TR tag
cstrittmatter@0 149
cstrittmatter@0 150 Reference: http://www.w3.org/TR/html4/struct/tables.html#h-11.2.5
cstrittmatter@0 151 """
cstrittmatter@0 152
cstrittmatter@0 153 def __init__(self, cells=None, bgcolor=None, header=False, attribs=None,
cstrittmatter@0 154 col_align=None, col_valign=None, col_char=None,
cstrittmatter@0 155 col_charoff=None, col_styles=None):
cstrittmatter@0 156 """TableCell constructor"""
cstrittmatter@0 157 self.bgcolor = bgcolor
cstrittmatter@0 158 self.cells = cells
cstrittmatter@0 159 self.header = header
cstrittmatter@0 160 self.col_align = col_align
cstrittmatter@0 161 self.col_valign = col_valign
cstrittmatter@0 162 self.col_char = col_char
cstrittmatter@0 163 self.col_charoff = col_charoff
cstrittmatter@0 164 self.col_styles = col_styles
cstrittmatter@0 165 self.attribs = attribs
cstrittmatter@0 166 if attribs==None:
cstrittmatter@0 167 self.attribs = {}
cstrittmatter@0 168
cstrittmatter@0 169 def __str__(self):
cstrittmatter@0 170 """return the HTML code for the table row as a string"""
cstrittmatter@0 171 attribs_str = ""
cstrittmatter@0 172 if self.bgcolor: self.attribs['bgcolor'] = self.bgcolor
cstrittmatter@0 173 for attr in self.attribs:
cstrittmatter@0 174 attribs_str += ' %s="%s"' % (attr, self.attribs[attr])
cstrittmatter@0 175 result = '<tr%s>' % attribs_str
cstrittmatter@0 176 for cell in self.cells:
cstrittmatter@0 177 col = self.cells.index(cell) # cell column index
cstrittmatter@0 178 if not isinstance(cell, TableCell):
cstrittmatter@0 179 cell = TableCell(cell, header=self.header)
cstrittmatter@0 180 # apply column alignment if specified:
cstrittmatter@0 181 if self.col_align and cell.align==None:
cstrittmatter@0 182 cell.align = self.col_align[col]
cstrittmatter@0 183 if self.col_char and cell.char==None:
cstrittmatter@0 184 cell.char = self.col_char[col]
cstrittmatter@0 185 if self.col_charoff and cell.charoff==None:
cstrittmatter@0 186 cell.charoff = self.col_charoff[col]
cstrittmatter@0 187 if self.col_valign and cell.valign==None:
cstrittmatter@0 188 cell.valign = self.col_valign[col]
cstrittmatter@0 189 # apply column style if specified:
cstrittmatter@0 190 if self.col_styles and cell.style==None:
cstrittmatter@0 191 cell.style = self.col_styles[col]
cstrittmatter@0 192 result += str(cell)
cstrittmatter@0 193 result += '</tr>\n'
cstrittmatter@0 194 return result
cstrittmatter@0 195
cstrittmatter@0 196 #-------------------------------------------------------------------------------
cstrittmatter@0 197
cstrittmatter@0 198 class Table (object):
cstrittmatter@0 199 """
cstrittmatter@0 200 a Table object is used to create a HTML table. (TABLE tag)
cstrittmatter@0 201
cstrittmatter@0 202 Attributes:
cstrittmatter@0 203 - rows: list, tuple or any iterable, containing one iterable or TableRow
cstrittmatter@0 204 object for each row
cstrittmatter@0 205 - header_row: list, tuple or any iterable, containing the header row (optional)
cstrittmatter@0 206 - border: str or int, border width
cstrittmatter@0 207 - style: str, table style in CSS syntax (thin black borders by default)
cstrittmatter@0 208 - width: str, width of the table on the page
cstrittmatter@0 209 - attribs: dict, additional attributes for the TABLE tag
cstrittmatter@0 210 - col_width: list or tuple defining width for each column
cstrittmatter@0 211 - col_align: list or tuple defining horizontal alignment for each column
cstrittmatter@0 212 - col_char: list or tuple defining alignment character for each column
cstrittmatter@0 213 - col_charoff: list or tuple defining charoff attribute for each column
cstrittmatter@0 214 - col_valign: list or tuple defining vertical alignment for each column
cstrittmatter@0 215 - col_styles: list or tuple of HTML styles for each column
cstrittmatter@0 216
cstrittmatter@0 217 Reference: http://www.w3.org/TR/html4/struct/tables.html#h-11.2.1
cstrittmatter@0 218 """
cstrittmatter@0 219
cstrittmatter@0 220 def __init__(self, rows=None, border='1', style=None, width=None,
cstrittmatter@0 221 cellspacing=None, cellpadding=4, attribs=None, header_row=None,
cstrittmatter@0 222 col_width=None, col_align=None, col_valign=None,
cstrittmatter@0 223 col_char=None, col_charoff=None, col_styles=None):
cstrittmatter@0 224 """TableCell constructor"""
cstrittmatter@0 225 self.border = border
cstrittmatter@0 226 self.style = style
cstrittmatter@0 227 # style for thin borders by default
cstrittmatter@0 228 #if style == None: self.style = TABLE_STYLE_THINBORDER
cstrittmatter@0 229 self.width = width
cstrittmatter@0 230 self.cellspacing = cellspacing
cstrittmatter@0 231 self.cellpadding = cellpadding
cstrittmatter@0 232 self.header_row = header_row
cstrittmatter@0 233 self.rows = rows
cstrittmatter@0 234 if not rows: self.rows = []
cstrittmatter@0 235 self.attribs = attribs
cstrittmatter@0 236 if not attribs: self.attribs = {}
cstrittmatter@0 237 self.col_width = col_width
cstrittmatter@0 238 self.col_align = col_align
cstrittmatter@0 239 self.col_char = col_char
cstrittmatter@0 240 self.col_charoff = col_charoff
cstrittmatter@0 241 self.col_valign = col_valign
cstrittmatter@0 242 self.col_styles = col_styles
cstrittmatter@0 243
cstrittmatter@0 244 def __str__(self):
cstrittmatter@0 245 """return the HTML code for the table as a string"""
cstrittmatter@0 246 attribs_str = ""
cstrittmatter@0 247 #if self.border: self.attribs['border'] = self.border
cstrittmatter@0 248 if self.style: self.attribs['style'] = self.style
cstrittmatter@0 249 if self.width: self.attribs['width'] = self.width
cstrittmatter@0 250 if self.cellspacing: self.attribs['cellspacing'] = self.cellspacing
cstrittmatter@0 251 if self.cellpadding: self.attribs['cellpadding'] = self.cellpadding
cstrittmatter@0 252 for attr in self.attribs:
cstrittmatter@0 253 attribs_str += ' %s="%s"' % (attr, self.attribs[attr])
cstrittmatter@0 254 result = '<table%s>\n' % attribs_str
cstrittmatter@0 255 # insert column tags and attributes if specified:
cstrittmatter@0 256 if self.col_width:
cstrittmatter@0 257 for width in self.col_width:
cstrittmatter@0 258 result += ' <col width="%s">\n' % width
cstrittmatter@0 259 # First insert a header row if specified:
cstrittmatter@0 260 if self.header_row:
cstrittmatter@0 261 if not isinstance(self.header_row, TableRow):
cstrittmatter@0 262 result += str(TableRow(self.header_row, header=True))
cstrittmatter@0 263 else:
cstrittmatter@0 264 result += str(self.header_row)
cstrittmatter@0 265 # Then all data rows:
cstrittmatter@0 266 for row in self.rows:
cstrittmatter@0 267 if not isinstance(row, TableRow):
cstrittmatter@0 268 row = TableRow(row)
cstrittmatter@0 269 # apply column alignments and styles to each row if specified:
cstrittmatter@0 270 # (Mozilla bug workaround)
cstrittmatter@0 271 if self.col_align and not row.col_align:
cstrittmatter@0 272 row.col_align = self.col_align
cstrittmatter@0 273 if self.col_char and not row.col_char:
cstrittmatter@0 274 row.col_char = self.col_char
cstrittmatter@0 275 if self.col_charoff and not row.col_charoff:
cstrittmatter@0 276 row.col_charoff = self.col_charoff
cstrittmatter@0 277 if self.col_valign and not row.col_valign:
cstrittmatter@0 278 row.col_valign = self.col_valign
cstrittmatter@0 279 if self.col_styles and not row.col_styles:
cstrittmatter@0 280 row.col_styles = self.col_styles
cstrittmatter@0 281 result += str(row)
cstrittmatter@0 282 result += '</table>'
cstrittmatter@0 283 return result
cstrittmatter@0 284
cstrittmatter@0 285
cstrittmatter@0 286 #-------------------------------------------------------------------------------
cstrittmatter@0 287
cstrittmatter@0 288 class List (object):
cstrittmatter@0 289 """
cstrittmatter@0 290 a List object is used to create an ordered or unordered list in HTML.
cstrittmatter@0 291 (UL/OL tag)
cstrittmatter@0 292
cstrittmatter@0 293 Attributes:
cstrittmatter@0 294 - lines: list, tuple or any iterable, containing one string for each line
cstrittmatter@0 295 - ordered: bool, choice between an ordered (OL) or unordered list (UL)
cstrittmatter@0 296 - attribs: dict, additional attributes for the OL/UL tag
cstrittmatter@0 297
cstrittmatter@0 298 Reference: http://www.w3.org/TR/html4/struct/lists.html
cstrittmatter@0 299 """
cstrittmatter@0 300
cstrittmatter@0 301 def __init__(self, lines=None, ordered=False, start=None, attribs=None):
cstrittmatter@0 302 """List constructor"""
cstrittmatter@0 303 if lines:
cstrittmatter@0 304 self.lines = lines
cstrittmatter@0 305 else:
cstrittmatter@0 306 self.lines = []
cstrittmatter@0 307 self.ordered = ordered
cstrittmatter@0 308 self.start = start
cstrittmatter@0 309 if attribs:
cstrittmatter@0 310 self.attribs = attribs
cstrittmatter@0 311 else:
cstrittmatter@0 312 self.attribs = {}
cstrittmatter@0 313
cstrittmatter@0 314 def __str__(self):
cstrittmatter@0 315 """return the HTML code for the list as a string"""
cstrittmatter@0 316 attribs_str = ""
cstrittmatter@0 317 if self.start: self.attribs['start'] = self.start
cstrittmatter@0 318 for attr in self.attribs:
cstrittmatter@0 319 attribs_str += ' %s="%s"' % (attr, self.attribs[attr])
cstrittmatter@0 320 if self.ordered: tag = 'ol'
cstrittmatter@0 321 else: tag = 'ul'
cstrittmatter@0 322 result = '<%s%s>\n' % (tag, attribs_str)
cstrittmatter@0 323 for line in self.lines:
cstrittmatter@0 324 result += ' <li>%s\n' % str(line)
cstrittmatter@0 325 result += '</%s>\n' % tag
cstrittmatter@0 326 return result
cstrittmatter@0 327
cstrittmatter@0 328 #=== FUNCTIONS ================================================================
cstrittmatter@0 329
cstrittmatter@0 330 # much simpler definition of a link as a function:
cstrittmatter@0 331 def Link(text, url):
cstrittmatter@0 332 return '<a href="%s">%s</a>' % (url, text)
cstrittmatter@0 333
cstrittmatter@0 334 def link(text, url):
cstrittmatter@0 335 return '<a href="%s">%s</a>' % (url, text)
cstrittmatter@0 336
cstrittmatter@0 337 def table(*args, **kwargs):
cstrittmatter@0 338 'return HTML code for a table as a string. See Table class for parameters.'
cstrittmatter@0 339 return str(Table(*args, **kwargs))
cstrittmatter@0 340
cstrittmatter@0 341 def list(*args, **kwargs):
cstrittmatter@0 342 'return HTML code for a list as a string. See List class for parameters.'
cstrittmatter@0 343 return str(List(*args, **kwargs))
cstrittmatter@0 344
cstrittmatter@0 345
cstrittmatter@0 346 #=== MAIN =====================================================================
cstrittmatter@0 347
cstrittmatter@0 348 # Show sample usage when this file is launched as a script.
cstrittmatter@0 349
cstrittmatter@0 350 if __name__ == '__main__':
cstrittmatter@0 351
cstrittmatter@0 352 # open an HTML file to show output in a browser
cstrittmatter@0 353 f = open('test.html', 'w')
cstrittmatter@0 354
cstrittmatter@0 355 t = Table()
cstrittmatter@0 356 t.rows.append(TableRow(['A', 'B', 'C'], header=True))
cstrittmatter@0 357 t.rows.append(TableRow(['D', 'E', 'F']))
cstrittmatter@0 358 t.rows.append(('i', 'j', 'k'))
cstrittmatter@0 359 f.write(str(t) + '<p>\n')
cstrittmatter@0 360 print (str(t))
cstrittmatter@0 361 f.close()