# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of logilab-common. # # logilab-common is free software: you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation, either version 2.1 of the License, or (at your option) any # later version. # # logilab-common is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License along # with logilab-common. If not, see . """HTML formatting drivers for ureports""" from __future__ import generators __docformat__ = "restructuredtext en" from logilab.common.ureports import HTMLWriter class DocbookWriter(HTMLWriter): """format layouts as HTML""" def begin_format(self, layout): """begin to format a layout""" super(HTMLWriter, self).begin_format(layout) if self.snippet is None: self.writeln('') self.writeln(""" """) def end_format(self, layout): """finished to format a layout""" if self.snippet is None: self.writeln('') def visit_section(self, layout): """display a section (using (level 0) or
)""" if self.section == 0: tag = "chapter" else: tag = "section" self.section += 1 self.writeln(self._indent('<%s%s>' % (tag, self.handle_attrs(layout)))) self.format_children(layout) self.writeln(self._indent(''% tag)) self.section -= 1 def visit_title(self, layout): """display a title using """ self.write(self._indent(' <title%s>' % self.handle_attrs(layout))) self.format_children(layout) self.writeln('') def visit_table(self, layout): """display a table as html""" self.writeln(self._indent(' %s' \ % (self.handle_attrs(layout), layout.title))) self.writeln(self._indent(' '% layout.cols)) for i in range(layout.cols): self.writeln(self._indent(' ' % i)) table_content = self.get_table_content(layout) # write headers if layout.cheaders: self.writeln(self._indent(' ')) self._write_row(table_content[0]) self.writeln(self._indent(' ')) table_content = table_content[1:] elif layout.rcheaders: self.writeln(self._indent(' ')) self._write_row(table_content[-1]) self.writeln(self._indent(' ')) table_content = table_content[:-1] # write body self.writeln(self._indent(' ')) for i in range(len(table_content)): row = table_content[i] self.writeln(self._indent(' ')) for j in range(len(row)): cell = row[j] or ' ' self.writeln(self._indent(' %s' % cell)) self.writeln(self._indent(' ')) self.writeln(self._indent(' ')) self.writeln(self._indent(' ')) self.writeln(self._indent(' ')) def _write_row(self, row): """write content of row (using )""" self.writeln(' ') for j in range(len(row)): cell = row[j] or ' ' self.writeln(' %s' % cell) self.writeln(self._indent(' ')) def visit_list(self, layout): """display a list (using )""" self.writeln(self._indent(' ' % self.handle_attrs(layout))) for row in list(self.compute_content(layout)): self.writeln(' %s' % row) self.writeln(self._indent(' ')) def visit_paragraph(self, layout): """display links (using )""" self.write(self._indent(' ')) self.format_children(layout) self.writeln('') def visit_span(self, layout): """display links (using

)""" #TODO: translate in docbook self.write('' % self.handle_attrs(layout)) self.format_children(layout) self.write('') def visit_link(self, layout): """display links (using )""" self.write('%s' % (layout.url, self.handle_attrs(layout), layout.label)) def visit_verbatimtext(self, layout): """display verbatim text (using )""" self.writeln(self._indent(' ')) self.write(layout.data.replace('&', '&').replace('<', '<')) self.writeln(self._indent(' ')) def visit_text(self, layout): """add some text""" self.write(layout.data.replace('&', '&').replace('<', '<')) def _indent(self, string): """correctly indent string according to section""" return ' ' * 2*(self.section) + string