| 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 # |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 yield child | 35 yield child |
| 36 # recurse (FIXME: recursion controled by an option) | 36 # recurse (FIXME: recursion controled by an option) |
| 37 for grandchild in get_nodes(child, klass): | 37 for grandchild in get_nodes(child, klass): |
| 38 yield grandchild | 38 yield grandchild |
| 39 | 39 |
| 40 def layout_title(layout): | 40 def layout_title(layout): |
| 41 """try to return the layout's title as string, return None if not found | 41 """try to return the layout's title as string, return None if not found |
| 42 """ | 42 """ |
| 43 for child in layout.children: | 43 for child in layout.children: |
| 44 if isinstance(child, Title): | 44 if isinstance(child, Title): |
| 45 return ' '.join([node.data for node in get_nodes(child, Text)]) | 45 return u' '.join([node.data for node in get_nodes(child, Text)]) |
| 46 | 46 |
| 47 def build_summary(layout, level=1): | 47 def build_summary(layout, level=1): |
| 48 """make a summary for the report, including X level""" | 48 """make a summary for the report, including X level""" |
| 49 assert level > 0 | 49 assert level > 0 |
| 50 level -= 1 | 50 level -= 1 |
| 51 summary = List(klass='summary') | 51 summary = List(klass=u'summary') |
| 52 for child in layout.children: | 52 for child in layout.children: |
| 53 if not isinstance(child, Section): | 53 if not isinstance(child, Section): |
| 54 continue | 54 continue |
| 55 label = layout_title(child) | 55 label = layout_title(child) |
| 56 if not label and not child.id: | 56 if not label and not child.id: |
| 57 continue | 57 continue |
| 58 if not child.id: | 58 if not child.id: |
| 59 child.id = label.replace(' ', '-') | 59 child.id = label.replace(' ', '-') |
| 60 node = Link('#'+child.id, label=label or child.id) | 60 node = Link(u'#'+child.id, label=label or child.id) |
| 61 # FIXME: Three following lines produce not very compliant | 61 # FIXME: Three following lines produce not very compliant |
| 62 # docbook: there are some useless <para><para>. They might be | 62 # docbook: there are some useless <para><para>. They might be |
| 63 # replaced by the three commented lines but this then produces | 63 # replaced by the three commented lines but this then produces |
| 64 # a bug in html display... | 64 # a bug in html display... |
| 65 if level and [n for n in child.children if isinstance(n, Section)]: | 65 if level and [n for n in child.children if isinstance(n, Section)]: |
| 66 node = Paragraph([node, build_summary(child, level)]) | 66 node = Paragraph([node, build_summary(child, level)]) |
| 67 summary.append(node) | 67 summary.append(node) |
| 68 # summary.append(node) | 68 # summary.append(node) |
| 69 # if level and [n for n in child.children if isinstance(n, Section)]: | 69 # if level and [n for n in child.children if isinstance(n, Section)]: |
| 70 # summary.append(build_summary(child, level)) | 70 # summary.append(build_summary(child, level)) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 92 layout.accept(self) | 92 layout.accept(self) |
| 93 self.end_format(layout) | 93 self.end_format(layout) |
| 94 | 94 |
| 95 def format_children(self, layout): | 95 def format_children(self, layout): |
| 96 """recurse on the layout children and call their accept method | 96 """recurse on the layout children and call their accept method |
| 97 (see the Visitor pattern) | 97 (see the Visitor pattern) |
| 98 """ | 98 """ |
| 99 for child in getattr(layout, 'children', ()): | 99 for child in getattr(layout, 'children', ()): |
| 100 child.accept(self) | 100 child.accept(self) |
| 101 | 101 |
| 102 def writeln(self, string=''): | 102 def writeln(self, string=u''): |
| 103 """write a line in the output buffer""" | 103 """write a line in the output buffer""" |
| 104 self.write(string + linesep) | 104 self.write(string + linesep) |
| 105 | 105 |
| 106 def write(self, string): | 106 def write(self, string): |
| 107 """write a string in the output buffer""" | 107 """write a string in the output buffer""" |
| 108 try: | 108 try: |
| 109 self.out.write(string) | 109 self.out.write(string) |
| 110 except UnicodeEncodeError: | 110 except UnicodeEncodeError: |
| 111 self.out.write(string.encode(self.encoding)) | 111 self.out.write(string.encode(self.encoding)) |
| 112 | 112 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 125 result = [[]] | 125 result = [[]] |
| 126 cols = table.cols | 126 cols = table.cols |
| 127 for cell in self.compute_content(table): | 127 for cell in self.compute_content(table): |
| 128 if cols == 0: | 128 if cols == 0: |
| 129 result.append([]) | 129 result.append([]) |
| 130 cols = table.cols | 130 cols = table.cols |
| 131 cols -= 1 | 131 cols -= 1 |
| 132 result[-1].append(cell) | 132 result[-1].append(cell) |
| 133 # fill missing cells | 133 # fill missing cells |
| 134 while len(result[-1]) < cols: | 134 while len(result[-1]) < cols: |
| 135 result[-1].append('') | 135 result[-1].append(u'') |
| 136 return result | 136 return result |
| 137 | 137 |
| 138 def compute_content(self, layout): | 138 def compute_content(self, layout): |
| 139 """trick to compute the formatting of children layout before actually | 139 """trick to compute the formatting of children layout before actually |
| 140 writing it | 140 writing it |
| 141 | 141 |
| 142 return an iterator on strings (one for each child element) | 142 return an iterator on strings (one for each child element) |
| 143 """ | 143 """ |
| 144 # use cells ! | 144 # use cells ! |
| 145 def write(data): | 145 def write(data): |
| 146 try: | 146 try: |
| 147 stream.write(data) | 147 stream.write(data) |
| 148 except UnicodeEncodeError: | 148 except UnicodeEncodeError: |
| 149 stream.write(data.encode(self.encoding)) | 149 stream.write(data.encode(self.encoding)) |
| 150 def writeln(data=''): | 150 def writeln(data=u''): |
| 151 try: | 151 try: |
| 152 stream.write(data+linesep) | 152 stream.write(data+linesep) |
| 153 except UnicodeEncodeError: | 153 except UnicodeEncodeError: |
| 154 stream.write(data.encode(self.encoding)+linesep) | 154 stream.write(data.encode(self.encoding)+linesep) |
| 155 self.write = write | 155 self.write = write |
| 156 self.writeln = writeln | 156 self.writeln = writeln |
| 157 self.__compute_funcs.append((write, writeln)) | 157 self.__compute_funcs.append((write, writeln)) |
| 158 for child in layout.children: | 158 for child in layout.children: |
| 159 stream = StringIO() | 159 stream = StringIO() |
| 160 child.accept(self) | 160 child.accept(self) |
| 161 yield stream.getvalue() | 161 yield stream.getvalue() |
| 162 self.__compute_funcs.pop() | 162 self.__compute_funcs.pop() |
| 163 try: | 163 try: |
| 164 self.write, self.writeln = self.__compute_funcs[-1] | 164 self.write, self.writeln = self.__compute_funcs[-1] |
| 165 except IndexError: | 165 except IndexError: |
| 166 del self.write | 166 del self.write |
| 167 del self.writeln | 167 del self.writeln |
| 168 | 168 |
| 169 | 169 |
| 170 from logilab.common.ureports.nodes import * | 170 from logilab.common.ureports.nodes import * |
| 171 from logilab.common.ureports.text_writer import TextWriter | 171 from logilab.common.ureports.text_writer import TextWriter |
| 172 from logilab.common.ureports.html_writer import HTMLWriter | 172 from logilab.common.ureports.html_writer import HTMLWriter |
| OLD | NEW |