OLD | NEW |
1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # | 3 # |
4 # This file is part of logilab-common. | 4 # This file is part of logilab-common. |
5 # | 5 # |
6 # logilab-common is free software: you can redistribute it and/or modify it unde
r | 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 | 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 | 8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
9 # later version. | 9 # later version. |
10 # | 10 # |
11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT | 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 | 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 | 13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
14 # details. | 14 # details. |
15 # | 15 # |
16 # You should have received a copy of the GNU Lesser General Public License along | 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/>. | 17 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. |
18 """Text formatting drivers for ureports""" | 18 """Text formatting drivers for ureports""" |
19 | 19 |
20 from __future__ import print_function | 20 from __future__ import print_function |
21 | 21 |
22 __docformat__ = "restructuredtext en" | 22 __docformat__ = "restructuredtext en" |
23 | 23 |
24 from six.moves import range | 24 from six.moves import range |
25 | 25 |
26 from logilab.common.textutils import linesep | 26 from logilab.common.textutils import linesep |
27 from logilab.common.ureports import BaseWriter | 27 from logilab.common.ureports import BaseWriter |
28 | 28 |
29 | 29 |
30 TITLE_UNDERLINES = ['', '=', '-', '`', '.', '~', '^'] | 30 TITLE_UNDERLINES = [u'', u'=', u'-', u'`', u'.', u'~', u'^'] |
31 BULLETS = ['*', '-'] | 31 BULLETS = [u'*', u'-'] |
32 | 32 |
33 class TextWriter(BaseWriter): | 33 class TextWriter(BaseWriter): |
34 """format layouts as text | 34 """format layouts as text |
35 (ReStructured inspiration but not totally handled yet) | 35 (ReStructured inspiration but not totally handled yet) |
36 """ | 36 """ |
37 def begin_format(self, layout): | 37 def begin_format(self, layout): |
38 super(TextWriter, self).begin_format(layout) | 38 super(TextWriter, self).begin_format(layout) |
39 self.list_level = 0 | 39 self.list_level = 0 |
40 self.pending_urls = [] | 40 self.pending_urls = [] |
41 | 41 |
42 def visit_section(self, layout): | 42 def visit_section(self, layout): |
43 """display a section as text | 43 """display a section as text |
44 """ | 44 """ |
45 self.section += 1 | 45 self.section += 1 |
46 self.writeln() | 46 self.writeln() |
47 self.format_children(layout) | 47 self.format_children(layout) |
48 if self.pending_urls: | 48 if self.pending_urls: |
49 self.writeln() | 49 self.writeln() |
50 for label, url in self.pending_urls: | 50 for label, url in self.pending_urls: |
51 self.writeln('.. _`%s`: %s' % (label, url)) | 51 self.writeln(u'.. _`%s`: %s' % (label, url)) |
52 self.pending_urls = [] | 52 self.pending_urls = [] |
53 self.section -= 1 | 53 self.section -= 1 |
54 self.writeln() | 54 self.writeln() |
55 | 55 |
56 def visit_title(self, layout): | 56 def visit_title(self, layout): |
57 title = ''.join(list(self.compute_content(layout))) | 57 title = u''.join(list(self.compute_content(layout))) |
58 self.writeln(title) | 58 self.writeln(title) |
59 try: | 59 try: |
60 self.writeln(TITLE_UNDERLINES[self.section] * len(title)) | 60 self.writeln(TITLE_UNDERLINES[self.section] * len(title)) |
61 except IndexError: | 61 except IndexError: |
62 print("FIXME TITLE TOO DEEP. TURNING TITLE INTO TEXT") | 62 print("FIXME TITLE TOO DEEP. TURNING TITLE INTO TEXT") |
63 | 63 |
64 def visit_paragraph(self, layout): | 64 def visit_paragraph(self, layout): |
65 """enter a paragraph""" | 65 """enter a paragraph""" |
66 self.format_children(layout) | 66 self.format_children(layout) |
67 self.writeln() | 67 self.writeln() |
(...skipping 13 matching lines...) Expand all Loading... |
81 cols_width[index] = max(cols_width[index], len(col)) | 81 cols_width[index] = max(cols_width[index], len(col)) |
82 if layout.klass == 'field': | 82 if layout.klass == 'field': |
83 self.field_table(layout, table_content, cols_width) | 83 self.field_table(layout, table_content, cols_width) |
84 else: | 84 else: |
85 self.default_table(layout, table_content, cols_width) | 85 self.default_table(layout, table_content, cols_width) |
86 self.writeln() | 86 self.writeln() |
87 | 87 |
88 def default_table(self, layout, table_content, cols_width): | 88 def default_table(self, layout, table_content, cols_width): |
89 """format a table""" | 89 """format a table""" |
90 cols_width = [size+1 for size in cols_width] | 90 cols_width = [size+1 for size in cols_width] |
91 format_strings = ' '.join(['%%-%ss'] * len(cols_width)) | 91 format_strings = u' '.join([u'%%-%ss'] * len(cols_width)) |
92 format_strings = format_strings % tuple(cols_width) | 92 format_strings = format_strings % tuple(cols_width) |
93 format_strings = format_strings.split(' ') | 93 format_strings = format_strings.split(' ') |
94 table_linesep = '\n+' + '+'.join(['-'*w for w in cols_width]) + '+\n' | 94 table_linesep = u'\n+' + u'+'.join([u'-'*w for w in cols_width]) + u'+\n
' |
95 headsep = '\n+' + '+'.join(['='*w for w in cols_width]) + '+\n' | 95 headsep = u'\n+' + u'+'.join([u'='*w for w in cols_width]) + u'+\n' |
96 # FIXME: layout.cheaders | 96 # FIXME: layout.cheaders |
97 self.write(table_linesep) | 97 self.write(table_linesep) |
98 for i in range(len(table_content)): | 98 for i in range(len(table_content)): |
99 self.write('|') | 99 self.write(u'|') |
100 line = table_content[i] | 100 line = table_content[i] |
101 for j in range(len(line)): | 101 for j in range(len(line)): |
102 self.write(format_strings[j] % line[j]) | 102 self.write(format_strings[j] % line[j]) |
103 self.write('|') | 103 self.write(u'|') |
104 if i == 0 and layout.rheaders: | 104 if i == 0 and layout.rheaders: |
105 self.write(headsep) | 105 self.write(headsep) |
106 else: | 106 else: |
107 self.write(table_linesep) | 107 self.write(table_linesep) |
108 | 108 |
109 def field_table(self, layout, table_content, cols_width): | 109 def field_table(self, layout, table_content, cols_width): |
110 """special case for field table""" | 110 """special case for field table""" |
111 assert layout.cols == 2 | 111 assert layout.cols == 2 |
112 format_string = '%s%%-%ss: %%s' % (linesep, cols_width[0]) | 112 format_string = u'%s%%-%ss: %%s' % (linesep, cols_width[0]) |
113 for field, value in table_content: | 113 for field, value in table_content: |
114 self.write(format_string % (field, value)) | 114 self.write(format_string % (field, value)) |
115 | 115 |
116 | 116 |
117 def visit_list(self, layout): | 117 def visit_list(self, layout): |
118 """display a list layout as text""" | 118 """display a list layout as text""" |
119 bullet = BULLETS[self.list_level % len(BULLETS)] | 119 bullet = BULLETS[self.list_level % len(BULLETS)] |
120 indent = ' ' * self.list_level | 120 indent = ' ' * self.list_level |
121 self.list_level += 1 | 121 self.list_level += 1 |
122 for child in layout.children: | 122 for child in layout.children: |
123 self.write('%s%s%s ' % (linesep, indent, bullet)) | 123 self.write(u'%s%s%s ' % (linesep, indent, bullet)) |
124 child.accept(self) | 124 child.accept(self) |
125 self.list_level -= 1 | 125 self.list_level -= 1 |
126 | 126 |
127 def visit_link(self, layout): | 127 def visit_link(self, layout): |
128 """add a hyperlink""" | 128 """add a hyperlink""" |
129 if layout.label != layout.url: | 129 if layout.label != layout.url: |
130 self.write('`%s`_' % layout.label) | 130 self.write(u'`%s`_' % layout.label) |
131 self.pending_urls.append( (layout.label, layout.url) ) | 131 self.pending_urls.append( (layout.label, layout.url) ) |
132 else: | 132 else: |
133 self.write(layout.url) | 133 self.write(layout.url) |
134 | 134 |
135 def visit_verbatimtext(self, layout): | 135 def visit_verbatimtext(self, layout): |
136 """display a verbatim layout as text (so difficult ;) | 136 """display a verbatim layout as text (so difficult ;) |
137 """ | 137 """ |
138 self.writeln('::\n') | 138 self.writeln(u'::\n') |
139 for line in layout.data.splitlines(): | 139 for line in layout.data.splitlines(): |
140 self.writeln(' ' + line) | 140 self.writeln(u' ' + line) |
141 self.writeln() | 141 self.writeln() |
142 | 142 |
143 def visit_text(self, layout): | 143 def visit_text(self, layout): |
144 """add some text""" | 144 """add some text""" |
145 self.write(layout.data) | 145 self.write(u'%s' % layout.data) |
OLD | NEW |