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