text_writer.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. """Text formatting drivers for ureports"""
  19. __docformat__ = "restructuredtext en"
  20. from logilab.common.textutils import linesep
  21. from logilab.common.ureports import BaseWriter
  22. TITLE_UNDERLINES = ['', '=', '-', '`', '.', '~', '^']
  23. BULLETS = ['*', '-']
  24. class TextWriter(BaseWriter):
  25. """format layouts as text
  26. (ReStructured inspiration but not totally handled yet)
  27. """
  28. def begin_format(self, layout):
  29. super(TextWriter, self).begin_format(layout)
  30. self.list_level = 0
  31. self.pending_urls = []
  32. def visit_section(self, layout):
  33. """display a section as text
  34. """
  35. self.section += 1
  36. self.writeln()
  37. self.format_children(layout)
  38. if self.pending_urls:
  39. self.writeln()
  40. for label, url in self.pending_urls:
  41. self.writeln('.. _`%s`: %s' % (label, url))
  42. self.pending_urls = []
  43. self.section -= 1
  44. self.writeln()
  45. def visit_title(self, layout):
  46. title = ''.join(list(self.compute_content(layout)))
  47. self.writeln(title)
  48. try:
  49. self.writeln(TITLE_UNDERLINES[self.section] * len(title))
  50. except IndexError:
  51. print "FIXME TITLE TOO DEEP. TURNING TITLE INTO TEXT"
  52. def visit_paragraph(self, layout):
  53. """enter a paragraph"""
  54. self.format_children(layout)
  55. self.writeln()
  56. def visit_span(self, layout):
  57. """enter a span"""
  58. self.format_children(layout)
  59. def visit_table(self, layout):
  60. """display a table as text"""
  61. table_content = self.get_table_content(layout)
  62. # get columns width
  63. cols_width = [0]*len(table_content[0])
  64. for row in table_content:
  65. for index in range(len(row)):
  66. col = row[index]
  67. cols_width[index] = max(cols_width[index], len(col))
  68. if layout.klass == 'field':
  69. self.field_table(layout, table_content, cols_width)
  70. else:
  71. self.default_table(layout, table_content, cols_width)
  72. self.writeln()
  73. def default_table(self, layout, table_content, cols_width):
  74. """format a table"""
  75. cols_width = [size+1 for size in cols_width]
  76. format_strings = ' '.join(['%%-%ss'] * len(cols_width))
  77. format_strings = format_strings % tuple(cols_width)
  78. format_strings = format_strings.split(' ')
  79. table_linesep = '\n+' + '+'.join(['-'*w for w in cols_width]) + '+\n'
  80. headsep = '\n+' + '+'.join(['='*w for w in cols_width]) + '+\n'
  81. # FIXME: layout.cheaders
  82. self.write(table_linesep)
  83. for i in range(len(table_content)):
  84. self.write('|')
  85. line = table_content[i]
  86. for j in range(len(line)):
  87. self.write(format_strings[j] % line[j])
  88. self.write('|')
  89. if i == 0 and layout.rheaders:
  90. self.write(headsep)
  91. else:
  92. self.write(table_linesep)
  93. def field_table(self, layout, table_content, cols_width):
  94. """special case for field table"""
  95. assert layout.cols == 2
  96. format_string = '%s%%-%ss: %%s' % (linesep, cols_width[0])
  97. for field, value in table_content:
  98. self.write(format_string % (field, value))
  99. def visit_list(self, layout):
  100. """display a list layout as text"""
  101. bullet = BULLETS[self.list_level % len(BULLETS)]
  102. indent = ' ' * self.list_level
  103. self.list_level += 1
  104. for child in layout.children:
  105. self.write('%s%s%s ' % (linesep, indent, bullet))
  106. child.accept(self)
  107. self.list_level -= 1
  108. def visit_link(self, layout):
  109. """add a hyperlink"""
  110. if layout.label != layout.url:
  111. self.write('`%s`_' % layout.label)
  112. self.pending_urls.append( (layout.label, layout.url) )
  113. else:
  114. self.write(layout.url)
  115. def visit_verbatimtext(self, layout):
  116. """display a verbatim layout as text (so difficult ;)
  117. """
  118. self.writeln('::\n')
  119. for line in layout.data.splitlines():
  120. self.writeln(' ' + line)
  121. self.writeln()
  122. def visit_text(self, layout):
  123. """add some text"""
  124. self.write(layout.data)