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

Source Code for Module logilab.common.ureports.html_writer

  1  # copyright 2003-2011 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 six.moves import range 
 24   
 25  from logilab.common.ureports import BaseWriter 
 26   
 27   
28 -class HTMLWriter(BaseWriter):
29 """format layouts as HTML""" 30
31 - def __init__(self, snippet=None):
32 super(HTMLWriter, self).__init__() 33 self.snippet = snippet
34
35 - def handle_attrs(self, layout):
36 """get an attribute string from layout member attributes""" 37 attrs = u'' 38 klass = getattr(layout, 'klass', None) 39 if klass: 40 attrs += u' class="%s"' % klass 41 nid = getattr(layout, 'id', None) 42 if nid: 43 attrs += u' id="%s"' % nid 44 return attrs
45
46 - def begin_format(self, layout):
47 """begin to format a layout""" 48 super(HTMLWriter, self).begin_format(layout) 49 if self.snippet is None: 50 self.writeln(u'<html>') 51 self.writeln(u'<body>')
52
53 - def end_format(self, layout):
54 """finished to format a layout""" 55 if self.snippet is None: 56 self.writeln(u'</body>') 57 self.writeln(u'</html>')
58 59
60 - def visit_section(self, layout):
61 """display a section as html, using div + h[section level]""" 62 self.section += 1 63 self.writeln(u'<div%s>' % self.handle_attrs(layout)) 64 self.format_children(layout) 65 self.writeln(u'</div>') 66 self.section -= 1
67
68 - def visit_title(self, layout):
69 """display a title using <hX>""" 70 self.write(u'<h%s%s>' % (self.section, self.handle_attrs(layout))) 71 self.format_children(layout) 72 self.writeln(u'</h%s>' % self.section)
73
74 - def visit_table(self, layout):
75 """display a table as html""" 76 self.writeln(u'<table%s>' % self.handle_attrs(layout)) 77 table_content = self.get_table_content(layout) 78 for i in range(len(table_content)): 79 row = table_content[i] 80 if i == 0 and layout.rheaders: 81 self.writeln(u'<tr class="header">') 82 elif i+1 == len(table_content) and layout.rrheaders: 83 self.writeln(u'<tr class="header">') 84 else: 85 self.writeln(u'<tr class="%s">' % (i%2 and 'even' or 'odd')) 86 for j in range(len(row)): 87 cell = row[j] or u'&#160;' 88 if (layout.rheaders and i == 0) or \ 89 (layout.cheaders and j == 0) or \ 90 (layout.rrheaders and i+1 == len(table_content)) or \ 91 (layout.rcheaders and j+1 == len(row)): 92 self.writeln(u'<th>%s</th>' % cell) 93 else: 94 self.writeln(u'<td>%s</td>' % cell) 95 self.writeln(u'</tr>') 96 self.writeln(u'</table>')
97
98 - def visit_list(self, layout):
99 """display a list as html""" 100 self.writeln(u'<ul%s>' % self.handle_attrs(layout)) 101 for row in list(self.compute_content(layout)): 102 self.writeln(u'<li>%s</li>' % row) 103 self.writeln(u'</ul>')
104
105 - def visit_paragraph(self, layout):
106 """display links (using <p>)""" 107 self.write(u'<p>') 108 self.format_children(layout) 109 self.write(u'</p>')
110
111 - def visit_span(self, layout):
112 """display links (using <p>)""" 113 self.write(u'<span%s>' % self.handle_attrs(layout)) 114 self.format_children(layout) 115 self.write(u'</span>')
116
122 - def visit_verbatimtext(self, layout):
123 """display verbatim text (using <pre>)""" 124 self.write(u'<pre>') 125 self.write(layout.data.replace(u'&', u'&amp;').replace(u'<', u'&lt;')) 126 self.write(u'</pre>')
127
128 - def visit_text(self, layout):
129 """add some text""" 130 data = layout.data 131 if layout.escaped: 132 data = data.replace(u'&', u'&amp;').replace(u'<', u'&lt;') 133 self.write(data)
134