OLD | NEW |
(Empty) | |
| 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 unde
r |
| 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 |
| 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 from __future__ import generators |
| 20 __docformat__ = "restructuredtext en" |
| 21 |
| 22 from logilab.common.ureports import HTMLWriter |
| 23 |
| 24 class DocbookWriter(HTMLWriter): |
| 25 """format layouts as HTML""" |
| 26 |
| 27 def begin_format(self, layout): |
| 28 """begin to format a layout""" |
| 29 super(HTMLWriter, self).begin_format(layout) |
| 30 if self.snippet is None: |
| 31 self.writeln('<?xml version="1.0" encoding="ISO-8859-1"?>') |
| 32 self.writeln(""" |
| 33 <book xmlns:xi='http://www.w3.org/2001/XInclude' |
| 34 lang='fr'> |
| 35 """) |
| 36 |
| 37 def end_format(self, layout): |
| 38 """finished to format a layout""" |
| 39 if self.snippet is None: |
| 40 self.writeln('</book>') |
| 41 |
| 42 def visit_section(self, layout): |
| 43 """display a section (using <chapter> (level 0) or <section>)""" |
| 44 if self.section == 0: |
| 45 tag = "chapter" |
| 46 else: |
| 47 tag = "section" |
| 48 self.section += 1 |
| 49 self.writeln(self._indent('<%s%s>' % (tag, self.handle_attrs(layout)))) |
| 50 self.format_children(layout) |
| 51 self.writeln(self._indent('</%s>'% tag)) |
| 52 self.section -= 1 |
| 53 |
| 54 def visit_title(self, layout): |
| 55 """display a title using <title>""" |
| 56 self.write(self._indent(' <title%s>' % self.handle_attrs(layout))) |
| 57 self.format_children(layout) |
| 58 self.writeln('</title>') |
| 59 |
| 60 def visit_table(self, layout): |
| 61 """display a table as html""" |
| 62 self.writeln(self._indent(' <table%s><title>%s</title>' \ |
| 63 % (self.handle_attrs(layout), layout.title))) |
| 64 self.writeln(self._indent(' <tgroup cols="%s">'% layout.cols)) |
| 65 for i in range(layout.cols): |
| 66 self.writeln(self._indent(' <colspec colname="c%s" colwidth="1*
"/>' % i)) |
| 67 |
| 68 table_content = self.get_table_content(layout) |
| 69 # write headers |
| 70 if layout.cheaders: |
| 71 self.writeln(self._indent(' <thead>')) |
| 72 self._write_row(table_content[0]) |
| 73 self.writeln(self._indent(' </thead>')) |
| 74 table_content = table_content[1:] |
| 75 elif layout.rcheaders: |
| 76 self.writeln(self._indent(' <thead>')) |
| 77 self._write_row(table_content[-1]) |
| 78 self.writeln(self._indent(' </thead>')) |
| 79 table_content = table_content[:-1] |
| 80 # write body |
| 81 self.writeln(self._indent(' <tbody>')) |
| 82 for i in range(len(table_content)): |
| 83 row = table_content[i] |
| 84 self.writeln(self._indent(' <row>')) |
| 85 for j in range(len(row)): |
| 86 cell = row[j] or ' ' |
| 87 self.writeln(self._indent(' <entry>%s</entry>' % cell)) |
| 88 self.writeln(self._indent(' </row>')) |
| 89 self.writeln(self._indent(' </tbody>')) |
| 90 self.writeln(self._indent(' </tgroup>')) |
| 91 self.writeln(self._indent(' </table>')) |
| 92 |
| 93 def _write_row(self, row): |
| 94 """write content of row (using <row> <entry>)""" |
| 95 self.writeln(' <row>') |
| 96 for j in range(len(row)): |
| 97 cell = row[j] or ' ' |
| 98 self.writeln(' <entry>%s</entry>' % cell) |
| 99 self.writeln(self._indent(' </row>')) |
| 100 |
| 101 def visit_list(self, layout): |
| 102 """display a list (using <itemizedlist>)""" |
| 103 self.writeln(self._indent(' <itemizedlist%s>' % self.handle_attrs(layou
t))) |
| 104 for row in list(self.compute_content(layout)): |
| 105 self.writeln(' <listitem><para>%s</para></listitem>' % row) |
| 106 self.writeln(self._indent(' </itemizedlist>')) |
| 107 |
| 108 def visit_paragraph(self, layout): |
| 109 """display links (using <para>)""" |
| 110 self.write(self._indent(' <para>')) |
| 111 self.format_children(layout) |
| 112 self.writeln('</para>') |
| 113 |
| 114 def visit_span(self, layout): |
| 115 """display links (using <p>)""" |
| 116 #TODO: translate in docbook |
| 117 self.write('<literal %s>' % self.handle_attrs(layout)) |
| 118 self.format_children(layout) |
| 119 self.write('</literal>') |
| 120 |
| 121 def visit_link(self, layout): |
| 122 """display links (using <ulink>)""" |
| 123 self.write('<ulink url="%s"%s>%s</ulink>' % (layout.url, |
| 124 self.handle_attrs(layout), |
| 125 layout.label)) |
| 126 |
| 127 def visit_verbatimtext(self, layout): |
| 128 """display verbatim text (using <programlisting>)""" |
| 129 self.writeln(self._indent(' <programlisting>')) |
| 130 self.write(layout.data.replace('&', '&').replace('<', '<')) |
| 131 self.writeln(self._indent(' </programlisting>')) |
| 132 |
| 133 def visit_text(self, layout): |
| 134 """add some text""" |
| 135 self.write(layout.data.replace('&', '&').replace('<', '<')) |
| 136 |
| 137 def _indent(self, string): |
| 138 """correctly indent string according to section""" |
| 139 return ' ' * 2*(self.section) + string |
OLD | NEW |