diff options
Diffstat (limited to 'logilab/common/ureports/html_writer.py')
-rw-r--r-- | logilab/common/ureports/html_writer.py | 94 |
1 files changed, 51 insertions, 43 deletions
diff --git a/logilab/common/ureports/html_writer.py b/logilab/common/ureports/html_writer.py index 0783075..23ff588 100644 --- a/logilab/common/ureports/html_writer.py +++ b/logilab/common/ureports/html_writer.py @@ -20,8 +20,16 @@ __docformat__ = "restructuredtext en" from logilab.common.ureports import BaseWriter -from logilab.common.ureports.nodes import (Section, Title, Table, List, - Paragraph, Link, VerbatimText, Text) +from logilab.common.ureports.nodes import ( + Section, + Title, + Table, + List, + Paragraph, + Link, + VerbatimText, + Text, +) from typing import Any @@ -34,100 +42,100 @@ class HTMLWriter(BaseWriter): def handle_attrs(self, layout: Any) -> str: """get an attribute string from layout member attributes""" - attrs = u'' - klass = getattr(layout, 'klass', None) + attrs = "" + klass = getattr(layout, "klass", None) if klass: - attrs += u' class="%s"' % klass - nid = getattr(layout, 'id', None) + attrs += ' class="%s"' % klass + nid = getattr(layout, "id", None) if nid: - attrs += u' id="%s"' % nid + attrs += ' id="%s"' % nid return attrs def begin_format(self, layout: Any) -> None: """begin to format a layout""" super(HTMLWriter, self).begin_format(layout) if self.snippet is None: - self.writeln(u'<html>') - self.writeln(u'<body>') + self.writeln("<html>") + self.writeln("<body>") def end_format(self, layout: Any) -> None: """finished to format a layout""" if self.snippet is None: - self.writeln(u'</body>') - self.writeln(u'</html>') + self.writeln("</body>") + self.writeln("</html>") def visit_section(self, layout: Section) -> None: """display a section as html, using div + h[section level]""" self.section += 1 - self.writeln(u'<div%s>' % self.handle_attrs(layout)) + self.writeln("<div%s>" % self.handle_attrs(layout)) self.format_children(layout) - self.writeln(u'</div>') + self.writeln("</div>") self.section -= 1 def visit_title(self, layout: Title) -> None: """display a title using <hX>""" - self.write(u'<h%s%s>' % (self.section, self.handle_attrs(layout))) + self.write("<h%s%s>" % (self.section, self.handle_attrs(layout))) self.format_children(layout) - self.writeln(u'</h%s>' % self.section) + self.writeln("</h%s>" % self.section) def visit_table(self, layout: Table) -> None: """display a table as html""" - self.writeln(u'<table%s>' % self.handle_attrs(layout)) + self.writeln("<table%s>" % self.handle_attrs(layout)) table_content = self.get_table_content(layout) for i in range(len(table_content)): row = table_content[i] if i == 0 and layout.rheaders: - self.writeln(u'<tr class="header">') - elif i+1 == len(table_content) and layout.rrheaders: - self.writeln(u'<tr class="header">') + self.writeln('<tr class="header">') + elif i + 1 == len(table_content) and layout.rrheaders: + self.writeln('<tr class="header">') else: - self.writeln(u'<tr class="%s">' % (i % 2 and 'even' or 'odd')) + self.writeln('<tr class="%s">' % (i % 2 and "even" or "odd")) for j in range(len(row)): - cell = row[j] or u' ' - if (layout.rheaders and i == 0) or \ - (layout.cheaders and j == 0) or \ - (layout.rrheaders and i+1 == len(table_content)) or \ - (layout.rcheaders and j+1 == len(row)): - self.writeln(u'<th>%s</th>' % cell) + cell = row[j] or " " + if ( + (layout.rheaders and i == 0) + or (layout.cheaders and j == 0) + or (layout.rrheaders and i + 1 == len(table_content)) + or (layout.rcheaders and j + 1 == len(row)) + ): + self.writeln("<th>%s</th>" % cell) else: - self.writeln(u'<td>%s</td>' % cell) - self.writeln(u'</tr>') - self.writeln(u'</table>') + self.writeln("<td>%s</td>" % cell) + self.writeln("</tr>") + self.writeln("</table>") def visit_list(self, layout: List) -> None: """display a list as html""" - self.writeln(u'<ul%s>' % self.handle_attrs(layout)) + self.writeln("<ul%s>" % self.handle_attrs(layout)) for row in list(self.compute_content(layout)): - self.writeln(u'<li>%s</li>' % row) - self.writeln(u'</ul>') + self.writeln("<li>%s</li>" % row) + self.writeln("</ul>") def visit_paragraph(self, layout: Paragraph) -> None: """display links (using <p>)""" - self.write(u'<p>') + self.write("<p>") self.format_children(layout) - self.write(u'</p>') + self.write("</p>") def visit_span(self, layout): """display links (using <p>)""" - self.write(u'<span%s>' % self.handle_attrs(layout)) + self.write("<span%s>" % self.handle_attrs(layout)) self.format_children(layout) - self.write(u'</span>') + self.write("</span>") def visit_link(self, layout: Link) -> None: """display links (using <a>)""" - self.write(u' <a href="%s"%s>%s</a>' % (layout.url, - self.handle_attrs(layout), - layout.label)) + self.write(' <a href="%s"%s>%s</a>' % (layout.url, self.handle_attrs(layout), layout.label)) def visit_verbatimtext(self, layout: VerbatimText) -> None: """display verbatim text (using <pre>)""" - self.write(u'<pre>') - self.write(layout.data.replace(u'&', u'&').replace(u'<', u'<')) - self.write(u'</pre>') + self.write("<pre>") + self.write(layout.data.replace("&", "&").replace("<", "<")) + self.write("</pre>") def visit_text(self, layout: Text) -> None: """add some text""" data = layout.data if layout.escaped: - data = data.replace(u'&', u'&').replace(u'<', u'<') + data = data.replace("&", "&").replace("<", "<") self.write(data) |