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