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

Source Code for Module logilab.common.ureports.html_writer

  1  # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
  2  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
  3  # 
  4  # This file is part of logilab-common. 
  5  # 
  6  # logilab-common is free software: you can redistribute it and/or modify it under 
  7  # the terms of the GNU Lesser General Public License as published by the Free 
  8  # Software Foundation, either version 2.1 of the License, or (at your option) any 
  9  # later version. 
 10  # 
 11  # logilab-common is distributed in the hope that it will be useful, but WITHOUT 
 12  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 13  # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more 
 14  # details. 
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public License along 
 17  # with logilab-common.  If not, see <http://www.gnu.org/licenses/>. 
 18  """HTML formatting drivers for ureports""" 
 19  __docformat__ = "restructuredtext en" 
 20   
 21  from cgi import escape 
 22   
 23  from logilab.common.ureports import BaseWriter 
 24   
 25   
26 -class HTMLWriter(BaseWriter):
27 """format layouts as HTML""" 28
29 - def __init__(self, snippet=None):
30 super(HTMLWriter, self).__init__() 31 self.snippet = snippet
32
33 - def handle_attrs(self, layout):
34 """get an attribute string from layout member attributes""" 35 attrs = '' 36 klass = getattr(layout, 'klass', None) 37 if klass: 38 attrs += ' class="%s"' % klass 39 nid = getattr(layout, 'id', None) 40 if nid: 41 attrs += ' id="%s"' % nid 42 return attrs
43
44 - def begin_format(self, layout):
45 """begin to format a layout""" 46 super(HTMLWriter, self).begin_format(layout) 47 if self.snippet is None: 48 self.writeln('<html>') 49 self.writeln('<body>')
50
51 - def end_format(self, layout):
52 """finished to format a layout""" 53 if self.snippet is None: 54 self.writeln('</body>') 55 self.writeln('</html>')
56 57
58 - def visit_section(self, layout):
59 """display a section as html, using div + h[section level]""" 60 self.section += 1 61 self.writeln('<div%s>' % self.handle_attrs(layout)) 62 self.format_children(layout) 63 self.writeln('</div>') 64 self.section -= 1
65
66 - def visit_title(self, layout):
67 """display a title using <hX>""" 68 self.write('<h%s%s>' % (self.section, self.handle_attrs(layout))) 69 self.format_children(layout) 70 self.writeln('</h%s>' % self.section)
71
72 - def visit_table(self, layout):
73 """display a table as html""" 74 self.writeln('<table%s>' % self.handle_attrs(layout)) 75 table_content = self.get_table_content(layout) 76 for i in range(len(table_content)): 77 row = table_content[i] 78 if i == 0 and layout.rheaders: 79 self.writeln('<tr class="header">') 80 elif i+1 == len(table_content) and layout.rrheaders: 81 self.writeln('<tr class="header">') 82 else: 83 self.writeln('<tr class="%s">' % (i%2 and 'even' or 'odd')) 84 for j in range(len(row)): 85 cell = row[j] or '&#160;' 86 if (layout.rheaders and i == 0) or \ 87 (layout.cheaders and j == 0) or \ 88 (layout.rrheaders and i+1 == len(table_content)) or \ 89 (layout.rcheaders and j+1 == len(row)): 90 self.writeln('<th>%s</th>' % cell) 91 else: 92 self.writeln('<td>%s</td>' % cell) 93 self.writeln('</tr>') 94 self.writeln('</table>')
95
96 - def visit_list(self, layout):
97 """display a list as html""" 98 self.writeln('<ul%s>' % self.handle_attrs(layout)) 99 for row in list(self.compute_content(layout)): 100 self.writeln('<li>%s</li>' % row) 101 self.writeln('</ul>')
102
103 - def visit_paragraph(self, layout):
104 """display links (using <p>)""" 105 self.write('<p>') 106 self.format_children(layout) 107 self.write('</p>')
108
109 - def visit_span(self, layout):
110 """display links (using <p>)""" 111 self.write('<span%s>' % self.handle_attrs(layout)) 112 self.format_children(layout) 113 self.write('</span>')
114
120 - def visit_verbatimtext(self, layout):
121 """display verbatim text (using <pre>)""" 122 self.write('<pre>') 123 self.write(layout.data.replace('&', '&amp;').replace('<', '&lt;')) 124 self.write('</pre>')
125
126 - def visit_text(self, layout):
127 """add some text""" 128 data = layout.data 129 if layout.escaped: 130 data = data.replace('&', '&amp;').replace('<', '&lt;') 131 self.write(data)
132