summaryrefslogtreecommitdiff
path: root/ureports/docbook_writer.py
blob: fe8fc7929a380a4f0b51a99e79c916a1bda6ffba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""HTML formatting drivers for ureports.

:copyright:
  2004-2008 `LOGILAB S.A. <http://www.logilab.fr>`_ (Paris, FRANCE),
  all rights reserved.

:contact:
  http://www.logilab.org/project/logilab-common --
  mailto:python-projects@logilab.org

:license:
  `General Public License version 2
  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>`_
"""
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.snipet is None:
            self.writeln('<?xml version="1.0" encoding="ISO-8859-1"?>')
            self.writeln("""
<book xmlns:xi='http://www.w3.org/2001/XInclude'
      lang='fr'>
""")
    
    def end_format(self, layout):
        """finished to format a layout"""
        if self.snipet is None:
            self.writeln('</book>')

    def visit_section(self, layout):
        """display a section (using <chapter> (level 0) or <section>)"""
        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('</%s>'% tag))
        self.section -= 1

    def visit_title(self, layout):
        """display a title using <title>"""
        self.write(self._indent('  <title%s>' % self.handle_attrs(layout)))
        self.format_children(layout)
        self.writeln('</title>')

    def visit_table(self, layout):
        """display a table as html"""
        self.writeln(self._indent('  <table%s><title>%s</title>' \
                     % (self.handle_attrs(layout), layout.title)))
        self.writeln(self._indent('    <tgroup cols="%s">'% layout.cols))
        for i in range(layout.cols): 
            self.writeln(self._indent('      <colspec colname="c%s" colwidth="1*"/>' % i))
           
        table_content = self.get_table_content(layout)
        # write headers
        if layout.cheaders:
            self.writeln(self._indent('      <thead>'))
            self._write_row(table_content[0])
            self.writeln(self._indent('      </thead>'))
            table_content = table_content[1:]
        elif layout.rcheaders:
            self.writeln(self._indent('      <thead>'))
            self._write_row(table_content[-1])
            self.writeln(self._indent('      </thead>'))
            table_content = table_content[:-1]
        # write body
        self.writeln(self._indent('      <tbody>'))
        for i in range(len(table_content)):
            row = table_content[i]
            self.writeln(self._indent('        <row>'))
            for j in range(len(row)):
                cell = row[j] or '&#160;'
                self.writeln(self._indent('          <entry>%s</entry>' % cell))
            self.writeln(self._indent('        </row>'))
        self.writeln(self._indent('      </tbody>'))
        self.writeln(self._indent('    </tgroup>'))
        self.writeln(self._indent('  </table>'))

    def _write_row(self, row):
        """write content of row (using <row> <entry>)"""
        self.writeln('        <row>')
        for j in range(len(row)):
            cell = row[j] or '&#160;'
            self.writeln('          <entry>%s</entry>' % cell)
        self.writeln(self._indent('        </row>'))
        
    def visit_list(self, layout):
        """display a list (using <itemizedlist>)"""
        self.writeln(self._indent('  <itemizedlist%s>' % self.handle_attrs(layout)))
        for row in list(self.compute_content(layout)):
            self.writeln('    <listitem><para>%s</para></listitem>' % row)
        self.writeln(self._indent('  </itemizedlist>'))
        
    def visit_paragraph(self, layout):
        """display links (using <para>)"""
        self.write(self._indent('  <para>'))
        self.format_children(layout)
        self.writeln('</para>')
                   
    def visit_span(self, layout):
        """display links (using <p>)"""
        #TODO: translate in docbook
        self.write('<literal %s>' % self.handle_attrs(layout))
        self.format_children(layout)
        self.write('</literal>')
                   
    def visit_link(self, layout):
        """display links (using <ulink>)"""
        self.write('<ulink url="%s"%s>%s</ulink>' % (layout.url,
                                               self.handle_attrs(layout),
                                               layout.label))

    def visit_verbatimtext(self, layout):
        """display verbatim text (using <programlisting>)"""
        self.writeln(self._indent('  <programlisting>'))
        self.write(layout.data.replace('&', '&amp;').replace('<', '&lt;'))
        self.writeln(self._indent('  </programlisting>'))
        
    def visit_text(self, layout):
        """add some text"""
        self.write(layout.data.replace('&', '&amp;').replace('<', '&lt;'))
        
    def _indent(self, string):
        """correctly indent string according to section"""
        return ' ' * 2*(self.section) + string