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