| 1 | # Copyright (c) 2004-2005 LOGILAB S.A. (Paris, FRANCE). |
|---|
| 2 | # http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|---|
| 3 | # |
|---|
| 4 | # This program is free software; you can redistribute it and/or modify it under |
|---|
| 5 | # the terms of the GNU General Public License as published by the Free Software |
|---|
| 6 | # Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 7 | # version. |
|---|
| 8 | # |
|---|
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details |
|---|
| 12 | # |
|---|
| 13 | # You should have received a copy of the GNU General Public License along with |
|---|
| 14 | # this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 15 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 16 | """Text formatting drivers for ureports""" |
|---|
| 17 | |
|---|
| 18 | __revision__ = "$Id: text_writer.py,v 1.9 2005-11-22 13:13:13 syt Exp $" |
|---|
| 19 | |
|---|
| 20 | from os import linesep |
|---|
| 21 | |
|---|
| 22 | from logilab.common.ureports import BaseWriter |
|---|
| 23 | |
|---|
| 24 | TITLE_UNDERLINES = ['', '=', '-', '`', '.', '~', '^'] |
|---|
| 25 | BULLETS = ['*', '-'] |
|---|
| 26 | |
|---|
| 27 | class TextWriter(BaseWriter): |
|---|
| 28 | """format layouts as text |
|---|
| 29 | (ReStructured inspiration but not totally handled yet) |
|---|
| 30 | """ |
|---|
| 31 | def begin_format(self, layout): |
|---|
| 32 | super(TextWriter, self).begin_format(layout) |
|---|
| 33 | self.list_level = 0 |
|---|
| 34 | self.pending_urls = [] |
|---|
| 35 | |
|---|
| 36 | def visit_section(self, layout): |
|---|
| 37 | """display a section as text |
|---|
| 38 | """ |
|---|
| 39 | self.section += 1 |
|---|
| 40 | self.writeln() |
|---|
| 41 | self.format_children(layout) |
|---|
| 42 | if self.pending_urls: |
|---|
| 43 | self.writeln() |
|---|
| 44 | for label, url in self.pending_urls: |
|---|
| 45 | self.writeln('.. _`%s`: %s' % (label, url)) |
|---|
| 46 | self.pending_urls = [] |
|---|
| 47 | self.section -= 1 |
|---|
| 48 | self.writeln() |
|---|
| 49 | |
|---|
| 50 | def visit_title(self, layout): |
|---|
| 51 | title = ''.join(list(self.compute_content(layout))) |
|---|
| 52 | self.writeln(title) |
|---|
| 53 | try: |
|---|
| 54 | self.writeln(TITLE_UNDERLINES[self.section] * len(title)) |
|---|
| 55 | except IndexError: |
|---|
| 56 | print "FIXME TITLE TOO DEEP. TURNING TITLE INTO TEXT" |
|---|
| 57 | |
|---|
| 58 | def visit_paragraph(self, layout): |
|---|
| 59 | """enter a paragraph""" |
|---|
| 60 | self.format_children(layout) |
|---|
| 61 | self.writeln() |
|---|
| 62 | |
|---|
| 63 | def visit_span(self, layout): |
|---|
| 64 | """enter a span""" |
|---|
| 65 | self.format_children(layout) |
|---|
| 66 | |
|---|
| 67 | def visit_table(self, layout): |
|---|
| 68 | """display a table as text""" |
|---|
| 69 | table_content = self.get_table_content(layout) |
|---|
| 70 | # get columns width |
|---|
| 71 | cols_width = [0]*len(table_content[0]) |
|---|
| 72 | for row in table_content: |
|---|
| 73 | for index in range(len(row)): |
|---|
| 74 | col = row[index] |
|---|
| 75 | cols_width[index] = max(cols_width[index], len(col)) |
|---|
| 76 | if layout.klass == 'field': |
|---|
| 77 | self.field_table(layout, table_content, cols_width) |
|---|
| 78 | else: |
|---|
| 79 | self.default_table(layout, table_content, cols_width) |
|---|
| 80 | self.writeln() |
|---|
| 81 | |
|---|
| 82 | def default_table(self, layout, table_content, cols_width): |
|---|
| 83 | """format a table""" |
|---|
| 84 | cols_width = [size+1 for size in cols_width] |
|---|
| 85 | format_strings = ' '.join(['%%-%ss'] * len(cols_width)) |
|---|
| 86 | format_strings = format_strings % tuple(cols_width) |
|---|
| 87 | format_strings = format_strings.split(' ') |
|---|
| 88 | table_linesep = '\n+' + '+'.join(['-'*w for w in cols_width]) + '+\n' |
|---|
| 89 | headsep = '\n+' + '+'.join(['='*w for w in cols_width]) + '+\n' |
|---|
| 90 | # FIXME: layout.cheaders |
|---|
| 91 | self.write(table_linesep) |
|---|
| 92 | for i in range(len(table_content)): |
|---|
| 93 | self.write('|') |
|---|
| 94 | line = table_content[i] |
|---|
| 95 | for j in range(len(line)): |
|---|
| 96 | self.write(format_strings[j] % line[j]) |
|---|
| 97 | self.write('|') |
|---|
| 98 | if i == 0 and layout.rheaders: |
|---|
| 99 | self.write(headsep) |
|---|
| 100 | else: |
|---|
| 101 | self.write(table_linesep) |
|---|
| 102 | |
|---|
| 103 | def field_table(self, layout, table_content, cols_width): |
|---|
| 104 | """special case for field table""" |
|---|
| 105 | assert layout.cols == 2 |
|---|
| 106 | format_string = '%s%%-%ss: %%s' % (linesep, cols_width[0]) |
|---|
| 107 | for field, value in table_content: |
|---|
| 108 | self.write(format_string % (field, value)) |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | def visit_list(self, layout): |
|---|
| 112 | """display a list layout as text""" |
|---|
| 113 | bullet = BULLETS[self.list_level % len(BULLETS)] |
|---|
| 114 | indent = ' ' * self.list_level |
|---|
| 115 | self.list_level += 1 |
|---|
| 116 | for child in layout.children: |
|---|
| 117 | self.write('%s%s%s ' % (linesep, indent, bullet)) |
|---|
| 118 | child.accept(self) |
|---|
| 119 | self.list_level -= 1 |
|---|
| 120 | |
|---|
| 121 | def visit_link(self, layout): |
|---|
| 122 | """add a hyperlink""" |
|---|
| 123 | if layout.label != layout.url: |
|---|
| 124 | self.write('`%s`_' % layout.label) |
|---|
| 125 | self.pending_urls.append( (layout.label, layout.url) ) |
|---|
| 126 | else: |
|---|
| 127 | self.write(layout.url) |
|---|
| 128 | |
|---|
| 129 | def visit_verbatimtext(self, layout): |
|---|
| 130 | """display a verbatim layout as text (so difficult ;) |
|---|
| 131 | """ |
|---|
| 132 | self.writeln('::\n') |
|---|
| 133 | for line in layout.data.splitlines(): |
|---|
| 134 | self.writeln(' ' + line) |
|---|
| 135 | self.writeln() |
|---|
| 136 | |
|---|
| 137 | def visit_text(self, layout): |
|---|
| 138 | """add some text""" |
|---|
| 139 | self.write(layout.data) |
|---|
| 140 | |
|---|