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