Mercurial > piecrust2
comparison piecrust/rendering.py @ 128:28444014ce7d
Fix error reporting and counting of lines.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 14 Nov 2014 22:49:50 +0100 |
parents | 0445a2232de7 |
children | 1c3d229158ba |
comparison
equal
deleted
inserted
replaced
127:bc63dc20baa0 | 128:28444014ce7d |
---|---|
2 import os.path | 2 import os.path |
3 import logging | 3 import logging |
4 from piecrust.data.builder import (DataBuildingContext, build_page_data, | 4 from piecrust.data.builder import (DataBuildingContext, build_page_data, |
5 build_layout_data) | 5 build_layout_data) |
6 from piecrust.sources.base import PageSource | 6 from piecrust.sources.base import PageSource |
7 from piecrust.templating.base import TemplatingError | |
7 from piecrust.uriutil import get_slug | 8 from piecrust.uriutil import get_slug |
8 | 9 |
9 | 10 |
10 logger = logging.getLogger(__name__) | 11 logger = logging.getLogger(__name__) |
11 | 12 |
162 formatted_content = {} | 163 formatted_content = {} |
163 for seg_name, seg in page.raw_content.items(): | 164 for seg_name, seg in page.raw_content.items(): |
164 seg_text = '' | 165 seg_text = '' |
165 for seg_part in seg.parts: | 166 for seg_part in seg.parts: |
166 part_format = seg_part.fmt or format_name | 167 part_format = seg_part.fmt or format_name |
167 part_text = engine.renderString(seg_part.content, page_data, | 168 try: |
168 filename=page.path, line_offset=seg_part.line) | 169 part_text = engine.renderString( |
170 seg_part.content, page_data, | |
171 filename=page.path) | |
172 except TemplatingError as err: | |
173 err.lineno += seg_part.line | |
174 raise err | |
175 | |
169 part_text = format_text(app, part_format, part_text) | 176 part_text = format_text(app, part_format, part_text) |
170 seg_text += part_text | 177 seg_text += part_text |
171 formatted_content[seg_name] = seg_text | 178 formatted_content[seg_name] = seg_text |
172 | 179 |
173 if seg_name == 'content': | 180 if seg_name == 'content': |
209 for engine in app.plugin_loader.getTemplateEngines(): | 216 for engine in app.plugin_loader.getTemplateEngines(): |
210 if engine_name in engine.ENGINE_NAMES: | 217 if engine_name in engine.ENGINE_NAMES: |
211 return engine | 218 return engine |
212 return None | 219 return None |
213 | 220 |
214 def format_text(app, format_name, txt): | 221 def format_text(app, format_name, txt, exact_format=False): |
222 if exact_format and not format_name: | |
223 raise Exception("You need to specify a format name.") | |
224 | |
225 format_count = 0 | |
215 format_name = format_name or app.config.get('site/default_format') | 226 format_name = format_name or app.config.get('site/default_format') |
216 for fmt in app.plugin_loader.getFormatters(): | 227 for fmt in app.plugin_loader.getFormatters(): |
217 if fmt.FORMAT_NAMES is None or format_name in fmt.FORMAT_NAMES: | 228 if fmt.FORMAT_NAMES is None or format_name in fmt.FORMAT_NAMES: |
218 txt = fmt.render(format_name, txt) | 229 txt = fmt.render(format_name, txt) |
230 format_count += 1 | |
219 if fmt.OUTPUT_FORMAT is not None: | 231 if fmt.OUTPUT_FORMAT is not None: |
220 format_name = fmt.OUTPUT_FORMAT | 232 format_name = fmt.OUTPUT_FORMAT |
233 if exact_format and format_count == 0: | |
234 raise Exception("No such format: %s" % format_name) | |
221 return txt | 235 return txt |
222 | 236 |