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

Source Code for Module logilab.common.ureports.docbook_writer

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