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