Mercurial > piecrust2
comparison piecrust/rendering.py @ 698:33ab9badfd7a
render: Change how we store render passes info.
Previously we used a dictionary with integers as keys, which doesn't go well
with JSON serialization. Now replace with an array of fixed length with items
that are `None` by default.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Wed, 23 Mar 2016 16:39:22 -0700 |
| parents | 59268b4d8c71 |
| children | ab5c6a8ae90a |
comparison
equal
deleted
inserted
replaced
| 697:9e5393fcfab2 | 698:33ab9badfd7a |
|---|---|
| 56 self.page = page | 56 self.page = page |
| 57 self.uri = uri | 57 self.uri = uri |
| 58 self.num = num | 58 self.num = num |
| 59 self.data = None | 59 self.data = None |
| 60 self.content = None | 60 self.content = None |
| 61 self.render_info = None | 61 self.render_info = [None, None] |
| 62 | 62 |
| 63 @property | 63 @property |
| 64 def app(self): | 64 def app(self): |
| 65 return self.page.app | 65 return self.page.app |
| 66 | 66 |
| 67 def copyRenderInfo(self): | 67 def copyRenderInfo(self): |
| 68 return copy.deepcopy(self.render_info) | 68 return copy.deepcopy(self.render_info) |
| 69 | 69 |
| 70 | 70 |
| 71 PASS_NONE = 0 | 71 PASS_NONE = -1 |
| 72 PASS_FORMATTING = 1 | 72 PASS_FORMATTING = 0 |
| 73 PASS_RENDERING = 2 | 73 PASS_RENDERING = 1 |
| 74 | 74 |
| 75 | 75 |
| 76 RENDER_PASSES = [PASS_FORMATTING, PASS_RENDERING] | 76 RENDER_PASSES = [PASS_FORMATTING, PASS_RENDERING] |
| 77 | 77 |
| 78 | 78 |
| 125 self.pagination_source = None | 125 self.pagination_source = None |
| 126 self.pagination_filter = None | 126 self.pagination_filter = None |
| 127 self.custom_data = None | 127 self.custom_data = None |
| 128 self._current_pass = PASS_NONE | 128 self._current_pass = PASS_NONE |
| 129 | 129 |
| 130 self.render_passes = {} | 130 self.render_passes = [None, None] # Same length as RENDER_PASSES |
| 131 | 131 |
| 132 @property | 132 @property |
| 133 def app(self): | 133 def app(self): |
| 134 return self.page.app | 134 return self.page.app |
| 135 | 135 |
| 141 def uri(self): | 141 def uri(self): |
| 142 return self.page.getUri(self.page_num) | 142 return self.page.getUri(self.page_num) |
| 143 | 143 |
| 144 @property | 144 @property |
| 145 def current_pass_info(self): | 145 def current_pass_info(self): |
| 146 return self.render_passes.get(self._current_pass) | 146 if self._current_pass != PASS_NONE: |
| 147 return self.render_passes[self._current_pass] | |
| 148 return None | |
| 147 | 149 |
| 148 def setCurrentPass(self, rdr_pass): | 150 def setCurrentPass(self, rdr_pass): |
| 149 if rdr_pass != PASS_NONE: | 151 if rdr_pass != PASS_NONE: |
| 150 self.render_passes.setdefault(rdr_pass, RenderPassInfo()) | 152 self.render_passes[rdr_pass] = RenderPassInfo() |
| 151 self._current_pass = rdr_pass | 153 self._current_pass = rdr_pass |
| 152 | 154 |
| 153 def setPagination(self, paginator): | 155 def setPagination(self, paginator): |
| 154 self._raiseIfNoCurrentPass() | 156 self._raiseIfNoCurrentPass() |
| 155 pass_info = self.current_pass_info | 157 pass_info = self.current_pass_info |
| 274 'pass_info': None} | 276 'pass_info': None} |
| 275 | 277 |
| 276 rp = RenderedPage(page, ctx.uri, ctx.page_num) | 278 rp = RenderedPage(page, ctx.uri, ctx.page_num) |
| 277 rp.data = page_data | 279 rp.data = page_data |
| 278 rp.content = layout_result['content'] | 280 rp.content = layout_result['content'] |
| 279 rp.render_info = { | 281 rp.render_info[PASS_FORMATTING] = RenderPassInfo._fromJson( |
| 280 PASS_FORMATTING: RenderPassInfo._fromJson( | 282 render_result['pass_info']) |
| 281 render_result['pass_info'])} | |
| 282 if layout_result['pass_info'] is not None: | 283 if layout_result['pass_info'] is not None: |
| 283 rp.render_info[PASS_RENDERING] = RenderPassInfo._fromJson( | 284 rp.render_info[PASS_RENDERING] = RenderPassInfo._fromJson( |
| 284 layout_result['pass_info']) | 285 layout_result['pass_info']) |
| 285 return rp | 286 return rp |
| 286 except Exception as ex: | 287 except Exception as ex: |
| 370 if m: | 371 if m: |
| 371 offset = m.start() | 372 offset = m.start() |
| 372 content_abstract = seg_text[:offset] | 373 content_abstract = seg_text[:offset] |
| 373 formatted_segments['content.abstract'] = content_abstract | 374 formatted_segments['content.abstract'] = content_abstract |
| 374 | 375 |
| 375 pass_info = cpi.render_ctx.render_passes.get(PASS_FORMATTING) | 376 pass_info = cpi.render_ctx.render_passes[PASS_FORMATTING] |
| 376 res = { | 377 res = { |
| 377 'segments': formatted_segments, | 378 'segments': formatted_segments, |
| 378 'pass_info': pass_info._toJson()} | 379 'pass_info': pass_info._toJson()} |
| 379 return res | 380 return res |
| 380 | 381 |
| 403 except TemplateNotFoundError as ex: | 404 except TemplateNotFoundError as ex: |
| 404 msg = "Can't find template for page: %s\n" % page.path | 405 msg = "Can't find template for page: %s\n" % page.path |
| 405 msg += "Looked for: %s" % ', '.join(full_names) | 406 msg += "Looked for: %s" % ', '.join(full_names) |
| 406 raise Exception(msg) from ex | 407 raise Exception(msg) from ex |
| 407 | 408 |
| 408 pass_info = cpi.render_ctx.render_passes.get(PASS_RENDERING) | 409 pass_info = cpi.render_ctx.render_passes[PASS_RENDERING] |
| 409 res = {'content': output, 'pass_info': pass_info._toJson()} | 410 res = {'content': output, 'pass_info': pass_info._toJson()} |
| 410 return res | 411 return res |
| 411 | 412 |
| 412 | 413 |
| 413 def get_template_engine(app, engine_name): | 414 def get_template_engine(app, engine_name): |
