root / logilab.pylintinstaller / logilab / common / ureports / html_writer.py

Revision 202:d67e86292521, 4.9 kB (checked in by tziade@…, 11 months ago)

added logilab.pylintinstaller

Line 
1# Copyright (c) 2004-2005 LOGILAB S.A. (Paris, FRANCE).
2# http://www.logilab.fr/ -- mailto:contact@logilab.fr
3#
4# This program is free software; you can redistribute it and/or modify it under
5# the terms of the GNU General Public License as published by the Free Software
6# Foundation; either version 2 of the License, or (at your option) any later
7# version.
8#
9# This program is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
12#
13# You should have received a copy of the GNU General Public License along with
14# this program; if not, write to the Free Software Foundation, Inc.,
15# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16"""HTML formatting drivers for ureports
17"""
18
19__revision__ = "$Id: html_writer.py,v 1.10 2006-03-08 09:47:29 katia Exp $"
20
21from cgi import escape
22
23from logilab.common.ureports import BaseWriter
24
25
26class HTMLWriter(BaseWriter):
27    """format layouts as HTML"""
28   
29    def __init__(self, snipet=None):
30        super(HTMLWriter, self).__init__(self)
31        self.snipet = snipet
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.snipet 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.snipet 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 '&nbsp;'
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                   
115    def visit_link(self, layout):
116        """display links (using <a>)"""
117        self.write(' <a href="%s"%s>%s</a>' % (layout.url,
118                                               self.handle_attrs(layout),
119                                               layout.label))
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)
Note: See TracBrowser for help on using the browser.