docbook_writer.py 5.6 KB

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