Package logilab :: Package common :: Package ureports :: Module html_writer
[frames] | no frames]

Source Code for Module logilab.common.ureports.html_writer

  1  """HTML formatting drivers for ureports. 
  2   
  3  :copyright: 
  4    2004-2008 `LOGILAB S.A. <http://www.logilab.fr>`_ (Paris, FRANCE), 
  5    all rights reserved. 
  6   
  7  :contact: 
  8    http://www.logilab.org/project/logilab-common -- 
  9    mailto:python-projects@logilab.org 
 10   
 11  :license: 
 12    `General Public License version 2 
 13    <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>`_ 
 14  """ 
 15  __docformat__ = "restructuredtext en" 
 16   
 17  from cgi import escape 
 18   
 19  from logilab.common.ureports import BaseWriter 
 20   
 21   
22 -class HTMLWriter(BaseWriter):
23 """format layouts as HTML""" 24
25 - def __init__(self, snippet=None):
26 super(HTMLWriter, self).__init__() 27 self.snippet = snippet
28
29 - def handle_attrs(self, layout):
30 """get an attribute string from layout member attributes""" 31 attrs = '' 32 klass = getattr(layout, 'klass', None) 33 if klass: 34 attrs += ' class="%s"' % klass 35 nid = getattr(layout, 'id', None) 36 if nid: 37 attrs += ' id="%s"' % nid 38 return attrs
39
40 - def begin_format(self, layout):
41 """begin to format a layout""" 42 super(HTMLWriter, self).begin_format(layout) 43 if self.snippet is None: 44 self.writeln('<html>') 45 self.writeln('<body>')
46
47 - def end_format(self, layout):
48 """finished to format a layout""" 49 if self.snippet is None: 50 self.writeln('</body>') 51 self.writeln('</html>')
52 53
54 - def visit_section(self, layout):
55 """display a section as html, using div + h[section level]""" 56 self.section += 1 57 self.writeln('<div%s>' % self.handle_attrs(layout)) 58 self.format_children(layout) 59 self.writeln('</div>') 60 self.section -= 1
61
62 - def visit_title(self, layout):
63 """display a title using <hX>""" 64 self.write('<h%s%s>' % (self.section, self.handle_attrs(layout))) 65 self.format_children(layout) 66 self.writeln('</h%s>' % self.section)
67
68 - def visit_table(self, layout):
69 """display a table as html""" 70 self.writeln('<table%s>' % self.handle_attrs(layout)) 71 table_content = self.get_table_content(layout) 72 for i in range(len(table_content)): 73 row = table_content[i] 74 if i == 0 and layout.rheaders: 75 self.writeln('<tr class="header">') 76 elif i+1 == len(table_content) and layout.rrheaders: 77 self.writeln('<tr class="header">') 78 else: 79 self.writeln('<tr class="%s">' % (i%2 and 'even' or 'odd')) 80 for j in range(len(row)): 81 cell = row[j] or '&nbsp;' 82 if (layout.rheaders and i == 0) or \ 83 (layout.cheaders and j == 0) or \ 84 (layout.rrheaders and i+1 == len(table_content)) or \ 85 (layout.rcheaders and j+1 == len(row)): 86 self.writeln('<th>%s</th>' % cell) 87 else: 88 self.writeln('<td>%s</td>' % cell) 89 self.writeln('</tr>') 90 self.writeln('</table>')
91
92 - def visit_list(self, layout):
93 """display a list as html""" 94 self.writeln('<ul%s>' % self.handle_attrs(layout)) 95 for row in list(self.compute_content(layout)): 96 self.writeln('<li>%s</li>' % row) 97 self.writeln('</ul>')
98
99 - def visit_paragraph(self, layout):
100 """display links (using <p>)""" 101 self.write('<p>') 102 self.format_children(layout) 103 self.write('</p>')
104
105 - def visit_span(self, layout):
106 """display links (using <p>)""" 107 self.write('<span%s>' % self.handle_attrs(layout)) 108 self.format_children(layout) 109 self.write('</span>')
110
116 - def visit_verbatimtext(self, layout):
117 """display verbatim text (using <pre>)""" 118 self.write('<pre>') 119 self.write(layout.data.replace('&', '&amp;').replace('<', '&lt;')) 120 self.write('</pre>')
121
122 - def visit_text(self, layout):
123 """add some text""" 124 data = layout.data 125 if layout.escaped: 126 data = data.replace('&', '&amp;').replace('<', '&lt;') 127 self.write(data)
128