Package logilab-common-0 :: Package 39 :: Package 0 :: Package ureports :: Module docbook_writer
[frames] | no frames]

Source Code for Module logilab-common-0.39.0.ureports.docbook_writer

  1  """HTML formatting drivers for ureports. 
  2   
  3  :copyright: 
  4    2004-2008 `LOGILAB S.A. <http://www.logilab.fr>`_ (Paris, FRANCE), 
  5    all rights reserved. 
  6   
  7  :contact: 
  8    http://www.logilab.org/project/logilab-common -- 
  9    mailto:python-projects@logilab.org 
 10   
 11  :license: 
 12    `General Public License version 2 
 13    <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>`_ 
 14  """ 
 15  from __future__ import generators 
 16  __docformat__ = "restructuredtext en" 
 17   
 18  from logilab.common.ureports import HTMLWriter 
 19   
20 -class DocbookWriter(HTMLWriter):
21 """format layouts as HTML""" 22
23 - def begin_format(self, layout):
24 """begin to format a layout""" 25 super(HTMLWriter, self).begin_format(layout) 26 if self.snippet is None: 27 self.writeln('<?xml version="1.0" encoding="ISO-8859-1"?>') 28 self.writeln(""" 29 <book xmlns:xi='http://www.w3.org/2001/XInclude' 30 lang='fr'> 31 """)
32
33 - def end_format(self, layout):
34 """finished to format a layout""" 35 if self.snippet is None: 36 self.writeln('</book>')
37
38 - def visit_section(self, layout):
39 """display a section (using <chapter> (level 0) or <section>)""" 40 if self.section == 0: 41 tag = "chapter" 42 else: 43 tag = "section" 44 self.section += 1 45 self.writeln(self._indent('<%s%s>' % (tag, self.handle_attrs(layout)))) 46 self.format_children(layout) 47 self.writeln(self._indent('</%s>'% tag)) 48 self.section -= 1
49
50 - def visit_title(self, layout):
51 """display a title using <title>""" 52 self.write(self._indent(' <title%s>' % self.handle_attrs(layout))) 53 self.format_children(layout) 54 self.writeln('</title>')
55
56 - def visit_table(self, layout):
57 """display a table as html""" 58 self.writeln(self._indent(' <table%s><title>%s</title>' \ 59 % (self.handle_attrs(layout), layout.title))) 60 self.writeln(self._indent(' <tgroup cols="%s">'% layout.cols)) 61 for i in range(layout.cols): 62 self.writeln(self._indent(' <colspec colname="c%s" colwidth="1*"/>' % i)) 63 64 table_content = self.get_table_content(layout) 65 # write headers 66 if layout.cheaders: 67 self.writeln(self._indent(' <thead>')) 68 self._write_row(table_content[0]) 69 self.writeln(self._indent(' </thead>')) 70 table_content = table_content[1:] 71 elif layout.rcheaders: 72 self.writeln(self._indent(' <thead>')) 73 self._write_row(table_content[-1]) 74 self.writeln(self._indent(' </thead>')) 75 table_content = table_content[:-1] 76 # write body 77 self.writeln(self._indent(' <tbody>')) 78 for i in range(len(table_content)): 79 row = table_content[i] 80 self.writeln(self._indent(' <row>')) 81 for j in range(len(row)): 82 cell = row[j] or '&#160;' 83 self.writeln(self._indent(' <entry>%s</entry>' % cell)) 84 self.writeln(self._indent(' </row>')) 85 self.writeln(self._indent(' </tbody>')) 86 self.writeln(self._indent(' </tgroup>')) 87 self.writeln(self._indent(' </table>'))
88
89 - def _write_row(self, row):
90 """write content of row (using <row> <entry>)""" 91 self.writeln(' <row>') 92 for j in range(len(row)): 93 cell = row[j] or '&#160;' 94 self.writeln(' <entry>%s</entry>' % cell) 95 self.writeln(self._indent(' </row>'))
96
97 - def visit_list(self, layout):
98 """display a list (using <itemizedlist>)""" 99 self.writeln(self._indent(' <itemizedlist%s>' % self.handle_attrs(layout))) 100 for row in list(self.compute_content(layout)): 101 self.writeln(' <listitem><para>%s</para></listitem>' % row) 102 self.writeln(self._indent(' </itemizedlist>'))
103
104 - def visit_paragraph(self, layout):
105 """display links (using <para>)""" 106 self.write(self._indent(' <para>')) 107 self.format_children(layout) 108 self.writeln('</para>')
109
110 - def visit_span(self, layout):
111 """display links (using <p>)""" 112 #TODO: translate in docbook 113 self.write('<literal %s>' % self.handle_attrs(layout)) 114 self.format_children(layout) 115 self.write('</literal>')
116 122
123 - def visit_verbatimtext(self, layout):
124 """display verbatim text (using <programlisting>)""" 125 self.writeln(self._indent(' <programlisting>')) 126 self.write(layout.data.replace('&', '&amp;').replace('<', '&lt;')) 127 self.writeln(self._indent(' </programlisting>'))
128
129 - def visit_text(self, layout):
130 """add some text""" 131 self.write(layout.data.replace('&', '&amp;').replace('<', '&lt;'))
132
133 - def _indent(self, string):
134 """correctly indent string according to section""" 135 return ' ' * 2*(self.section) + string
136