html_writer.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. from cgi import escape
  21. from logilab.common.ureports import BaseWriter
  22. class HTMLWriter(BaseWriter):
  23. """format layouts as HTML"""
  24. def __init__(self, snippet=None):
  25. super(HTMLWriter, self).__init__()
  26. self.snippet = snippet
  27. def handle_attrs(self, layout):
  28. """get an attribute string from layout member attributes"""
  29. attrs = ''
  30. klass = getattr(layout, 'klass', None)
  31. if klass:
  32. attrs += ' class="%s"' % klass
  33. nid = getattr(layout, 'id', None)
  34. if nid:
  35. attrs += ' id="%s"' % nid
  36. return attrs
  37. def begin_format(self, layout):
  38. """begin to format a layout"""
  39. super(HTMLWriter, self).begin_format(layout)
  40. if self.snippet is None:
  41. self.writeln('<html>')
  42. self.writeln('<body>')
  43. def end_format(self, layout):
  44. """finished to format a layout"""
  45. if self.snippet is None:
  46. self.writeln('</body>')
  47. self.writeln('</html>')
  48. def visit_section(self, layout):
  49. """display a section as html, using div + h[section level]"""
  50. self.section += 1
  51. self.writeln('<div%s>' % self.handle_attrs(layout))
  52. self.format_children(layout)
  53. self.writeln('</div>')
  54. self.section -= 1
  55. def visit_title(self, layout):
  56. """display a title using <hX>"""
  57. self.write('<h%s%s>' % (self.section, self.handle_attrs(layout)))
  58. self.format_children(layout)
  59. self.writeln('</h%s>' % self.section)
  60. def visit_table(self, layout):
  61. """display a table as html"""
  62. self.writeln('<table%s>' % self.handle_attrs(layout))
  63. table_content = self.get_table_content(layout)
  64. for i in range(len(table_content)):
  65. row = table_content[i]
  66. if i == 0 and layout.rheaders:
  67. self.writeln('<tr class="header">')
  68. elif i+1 == len(table_content) and layout.rrheaders:
  69. self.writeln('<tr class="header">')
  70. else:
  71. self.writeln('<tr class="%s">' % (i%2 and 'even' or 'odd'))
  72. for j in range(len(row)):
  73. cell = row[j] or '&#160;'
  74. if (layout.rheaders and i == 0) or \
  75. (layout.cheaders and j == 0) or \
  76. (layout.rrheaders and i+1 == len(table_content)) or \
  77. (layout.rcheaders and j+1 == len(row)):
  78. self.writeln('<th>%s</th>' % cell)
  79. else:
  80. self.writeln('<td>%s</td>' % cell)
  81. self.writeln('</tr>')
  82. self.writeln('</table>')
  83. def visit_list(self, layout):
  84. """display a list as html"""
  85. self.writeln('<ul%s>' % self.handle_attrs(layout))
  86. for row in list(self.compute_content(layout)):
  87. self.writeln('<li>%s</li>' % row)
  88. self.writeln('</ul>')
  89. def visit_paragraph(self, layout):
  90. """display links (using <p>)"""
  91. self.write('<p>')
  92. self.format_children(layout)
  93. self.write('</p>')
  94. def visit_span(self, layout):
  95. """display links (using <p>)"""
  96. self.write('<span%s>' % self.handle_attrs(layout))
  97. self.format_children(layout)
  98. self.write('</span>')
  99. def visit_link(self, layout):
  100. """display links (using <a>)"""
  101. self.write(' <a href="%s"%s>%s</a>' % (layout.url,
  102. self.handle_attrs(layout),
  103. layout.label))
  104. def visit_verbatimtext(self, layout):
  105. """display verbatim text (using <pre>)"""
  106. self.write('<pre>')
  107. self.write(layout.data.replace('&', '&amp;').replace('<', '&lt;'))
  108. self.write('</pre>')
  109. def visit_text(self, layout):
  110. """add some text"""
  111. data = layout.data
  112. if layout.escaped:
  113. data = data.replace('&', '&amp;').replace('<', '&lt;')
  114. self.write(data)