annotate piecrust/rendering.py @ 853:f070a4fc033c

core: Continue PieCrust3 refactor, simplify pages. The asset pipeline is still the only function pipeline at this point. * No more `QualifiedPage`, and several other pieces of code deleted. * Data providers are simpler and more focused. For instance, the page iterator doesn't try to support other types of items. * Route parameters are proper known source metadata to remove the confusion between the two. * Make the baker and pipeline more correctly manage records and record histories. * Add support for record collapsing and deleting stale outputs in the asset pipeline.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 21 May 2017 00:06:59 -0700
parents 4850f8c21b6e
children 08e02c2a2a1a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import re
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
3 import copy
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import logging
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
5 from piecrust.data.builder import (
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
6 DataBuildingContext, build_page_data, add_layout_data)
719
a066f4ac9094 rendering: Use `fastpickle` serialization before JSON.
Ludovic Chabant <ludovic@chabant.com>
parents: 715
diff changeset
7 from piecrust.fastpickle import _pickle_object, _unpickle_object
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
8 from piecrust.sources.base import ContentSource
153
1c3d229158ba Make a nice error message when a layout hasn't been found.
Ludovic Chabant <ludovic@chabant.com>
parents: 128
diff changeset
9 from piecrust.templating.base import TemplateNotFoundError, TemplatingError
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 logger = logging.getLogger(__name__)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 content_abstract_re = re.compile(r'^<!--\s*(more|(page)?break)\s*-->\s*$',
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 re.MULTILINE)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
19 class RenderingError(Exception):
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 pass
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
183
fff195335d0a render: When a template engine can't be found, show the correct name in the error.
Ludovic Chabant <ludovic@chabant.com>
parents: 174
diff changeset
23 class TemplateEngineNotFound(Exception):
fff195335d0a render: When a template engine can't be found, show the correct name in the error.
Ludovic Chabant <ludovic@chabant.com>
parents: 174
diff changeset
24 pass
fff195335d0a render: When a template engine can't be found, show the correct name in the error.
Ludovic Chabant <ludovic@chabant.com>
parents: 174
diff changeset
25
fff195335d0a render: When a template engine can't be found, show the correct name in the error.
Ludovic Chabant <ludovic@chabant.com>
parents: 174
diff changeset
26
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
27 class RenderedSegments(object):
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
28 def __init__(self, segments, render_pass_info):
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
29 self.segments = segments
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
30 self.render_pass_info = render_pass_info
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
31
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
32
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
33 class RenderedLayout(object):
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
34 def __init__(self, content, render_pass_info):
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
35 self.content = content
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
36 self.render_pass_info = render_pass_info
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
37
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
38
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 class RenderedPage(object):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
40 def __init__(self, page, sub_num):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
41 self.page = page
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
42 self.sub_num = sub_num
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 self.data = None
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 self.content = None
698
33ab9badfd7a render: Change how we store render passes info.
Ludovic Chabant <ludovic@chabant.com>
parents: 586
diff changeset
45 self.render_info = [None, None]
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 @property
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 def app(self):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
49 return self.page.app
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
51 def copyRenderInfo(self):
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
52 return copy.deepcopy(self.render_info)
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
53
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54
698
33ab9badfd7a render: Change how we store render passes info.
Ludovic Chabant <ludovic@chabant.com>
parents: 586
diff changeset
55 PASS_NONE = -1
33ab9badfd7a render: Change how we store render passes info.
Ludovic Chabant <ludovic@chabant.com>
parents: 586
diff changeset
56 PASS_FORMATTING = 0
33ab9badfd7a render: Change how we store render passes info.
Ludovic Chabant <ludovic@chabant.com>
parents: 586
diff changeset
57 PASS_RENDERING = 1
96
0445a2232de7 Improvements and fixes to incremental baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 50
diff changeset
58
0445a2232de7 Improvements and fixes to incremental baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 50
diff changeset
59
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
60 RENDER_PASSES = [PASS_FORMATTING, PASS_RENDERING]
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
61
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
62
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
63 class RenderPassInfo(object):
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
64 def __init__(self):
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
65 self.used_source_names = set()
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
66 self.used_pagination = False
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
67 self.pagination_has_more = False
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
68 self.used_assets = False
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
69 self._custom_info = {}
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
70
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
71 def setCustomInfo(self, key, info):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
72 self._custom_info[key] = info
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
73
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
74 def getCustomInfo(self, key, default=None, create_if_missing=False):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
75 if create_if_missing:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
76 return self._custom_info.setdefault(key, default)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
77 return self._custom_info.get(key, default)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
78
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
79
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
80 class RenderingContext(object):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
81 def __init__(self, page, *, sub_num=1, force_render=False):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
82 self.page = page
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
83 self.sub_num = sub_num
174
e9a3d405e18f serve: Always force render the page being previewed.
Ludovic Chabant <ludovic@chabant.com>
parents: 158
diff changeset
84 self.force_render = force_render
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 self.pagination_source = None
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 self.pagination_filter = None
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 719
diff changeset
87 self.custom_data = {}
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
88 self.render_passes = [None, None] # Same length as RENDER_PASSES
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
89 self._current_pass = PASS_NONE
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
90
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 @property
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 def app(self):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
93 return self.page.app
369
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 338
diff changeset
94
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
95 @property
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
96 def current_pass_info(self):
698
33ab9badfd7a render: Change how we store render passes info.
Ludovic Chabant <ludovic@chabant.com>
parents: 586
diff changeset
97 if self._current_pass != PASS_NONE:
33ab9badfd7a render: Change how we store render passes info.
Ludovic Chabant <ludovic@chabant.com>
parents: 586
diff changeset
98 return self.render_passes[self._current_pass]
33ab9badfd7a render: Change how we store render passes info.
Ludovic Chabant <ludovic@chabant.com>
parents: 586
diff changeset
99 return None
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
100
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
101 def setCurrentPass(self, rdr_pass):
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
102 if rdr_pass != PASS_NONE:
698
33ab9badfd7a render: Change how we store render passes info.
Ludovic Chabant <ludovic@chabant.com>
parents: 586
diff changeset
103 self.render_passes[rdr_pass] = RenderPassInfo()
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
104 self._current_pass = rdr_pass
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
105
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 def setPagination(self, paginator):
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
107 self._raiseIfNoCurrentPass()
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
108 pass_info = self.current_pass_info
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
109 if pass_info.used_pagination:
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 raise Exception("Pagination has already been used.")
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
111 assert paginator.is_loaded
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
112 pass_info.used_pagination = True
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
113 pass_info.pagination_has_more = paginator.has_more
96
0445a2232de7 Improvements and fixes to incremental baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 50
diff changeset
114 self.addUsedSource(paginator._source)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115
23
923699e816d0 Don't try to get the name of a source that doesn't have one.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
116 def addUsedSource(self, source):
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
117 self._raiseIfNoCurrentPass()
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
118 if isinstance(source, ContentSource):
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
119 pass_info = self.current_pass_info
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
120 pass_info.used_source_names.add(source.name)
23
923699e816d0 Don't try to get the name of a source that doesn't have one.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
121
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
122 def _raiseIfNoCurrentPass(self):
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
123 if self._current_pass == PASS_NONE:
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
124 raise Exception("No rendering pass is currently active.")
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
125
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
127 class RenderingContextStack(object):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
128 def __init__(self):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
129 self._ctx_stack = []
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
130
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
131 @property
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
132 def current_ctx(self):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
133 if len(self._ctx_stack) == 0:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
134 return None
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
135 return self._ctx_stack[-1]
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
136
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
137 @property
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
138 def is_main_ctx(self):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
139 return len(self._ctx_stack) == 1
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
140
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
141 def hasPage(self, page):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
142 for ei in self._ctx_stack:
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
143 if ei.page == page:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
144 return True
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
145 return False
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
146
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
147 def pushCtx(self, render_ctx):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
148 for ctx in self._ctx_stack:
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
149 if ctx.page == render_ctx.page:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
150 raise Exception("Loop detected during rendering!")
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
151 self._ctx_stack.append(render_ctx)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
152
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
153 def popCtx(self):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
154 del self._ctx_stack[-1]
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
155
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
156 def clear(self):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
157 self._ctx_stack = []
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
158
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
159
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
160 def render_page(ctx):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
161 env = ctx.app.env
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
162
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
163 stack = env.render_ctx_stack
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
164 stack.pushCtx(ctx)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
165
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
166 page = ctx.page
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
167 page_uri = page.getUri(ctx.sub_num)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
168
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169 try:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170 # Build the data for both segment and layout rendering.
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
171 with env.timerScope("BuildRenderData"):
586
59268b4d8c71 bake: Add new performance timers.
Ludovic Chabant <ludovic@chabant.com>
parents: 578
diff changeset
172 page_data = _build_render_data(ctx)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174 # Render content segments.
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
175 ctx.setCurrentPass(PASS_FORMATTING)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
176 repo = env.rendered_segments_repository
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
177 save_to_fs = True
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
178 if env.fs_cache_only_for_main_page and not stack.is_main_ctx:
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
179 save_to_fs = False
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
180 with env.timerScope("PageRenderSegments"):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
181 if repo is not None and not ctx.force_render:
586
59268b4d8c71 bake: Add new performance timers.
Ludovic Chabant <ludovic@chabant.com>
parents: 578
diff changeset
182 render_result = repo.get(
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
183 page_uri,
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
184 lambda: _do_render_page_segments(ctx, page_data),
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
185 fs_cache_time=page.content_mtime,
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
186 save_to_fs=save_to_fs)
586
59268b4d8c71 bake: Add new performance timers.
Ludovic Chabant <ludovic@chabant.com>
parents: 578
diff changeset
187 else:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
188 render_result = _do_render_page_segments(ctx, page_data)
586
59268b4d8c71 bake: Add new performance timers.
Ludovic Chabant <ludovic@chabant.com>
parents: 578
diff changeset
189 if repo:
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
190 repo.put(page_uri, render_result, save_to_fs)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192 # Render layout.
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
193 ctx.setCurrentPass(PASS_RENDERING)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
194 layout_name = page.config.get('layout')
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195 if layout_name is None:
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
196 layout_name = page.source.config.get(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
197 'default_layout', 'default')
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 null_names = ['', 'none', 'nil']
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199 if layout_name not in null_names:
586
59268b4d8c71 bake: Add new performance timers.
Ludovic Chabant <ludovic@chabant.com>
parents: 578
diff changeset
200 with ctx.app.env.timerScope("BuildRenderData"):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
201 add_layout_data(page_data, render_result['segments'])
586
59268b4d8c71 bake: Add new performance timers.
Ludovic Chabant <ludovic@chabant.com>
parents: 578
diff changeset
202
59268b4d8c71 bake: Add new performance timers.
Ludovic Chabant <ludovic@chabant.com>
parents: 578
diff changeset
203 with ctx.app.env.timerScope("PageRenderLayout"):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
204 layout_result = _do_render_layout(
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
205 layout_name, page, page_data)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206 else:
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
207 layout_result = {
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
208 'content': render_result['segments']['content'],
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
209 'pass_info': None}
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
211 rp = RenderedPage(page, ctx.sub_num)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212 rp.data = page_data
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
213 rp.content = layout_result['content']
719
a066f4ac9094 rendering: Use `fastpickle` serialization before JSON.
Ludovic Chabant <ludovic@chabant.com>
parents: 715
diff changeset
214 rp.render_info[PASS_FORMATTING] = _unpickle_object(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
215 render_result['pass_info'])
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
216 if layout_result['pass_info'] is not None:
719
a066f4ac9094 rendering: Use `fastpickle` serialization before JSON.
Ludovic Chabant <ludovic@chabant.com>
parents: 715
diff changeset
217 rp.render_info[PASS_RENDERING] = _unpickle_object(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
218 layout_result['pass_info'])
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 return rp
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
220
522
094bdf2f7c4c serve: Say what page a rendering error happened in.
Ludovic Chabant <ludovic@chabant.com>
parents: 520
diff changeset
221 except Exception as ex:
715
a14371c5cda7 debug: Pass the exceptions untouched when debugging.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
222 if ctx.app.debug:
a14371c5cda7 debug: Pass the exceptions untouched when debugging.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
223 raise
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
224 logger.exception(ex)
574
bc23465ed1b4 bug: Fix a crash when some errors occur during page rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 522
diff changeset
225 page_rel_path = os.path.relpath(ctx.page.path, ctx.app.root_dir)
522
094bdf2f7c4c serve: Say what page a rendering error happened in.
Ludovic Chabant <ludovic@chabant.com>
parents: 520
diff changeset
226 raise Exception("Error rendering page: %s" % page_rel_path) from ex
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
227
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 finally:
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
229 ctx.setCurrentPass(PASS_NONE)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
230 stack.popCtx()
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233 def render_page_segments(ctx):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
234 env = ctx.app.env
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
235
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
236 stack = env.render_ctx_stack
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
237 stack.pushCtx(ctx)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
238
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
239 page = ctx.page
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
240 page_uri = page.getUri(ctx.sub_num)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
241
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
242 try:
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
243 ctx.setCurrentPass(PASS_FORMATTING)
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
244 repo = ctx.app.env.rendered_segments_repository
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
245 save_to_fs = True
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
246 if ctx.app.env.fs_cache_only_for_main_page and not stack.is_main_ctx:
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
247 save_to_fs = False
782
df58592b40f8 internal: Add missing timer scope.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
248 with ctx.app.env.timerScope("PageRenderSegments"):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
249 if repo is not None and not ctx.force_render:
782
df58592b40f8 internal: Add missing timer scope.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
250 render_result = repo.get(
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
251 page_uri,
782
df58592b40f8 internal: Add missing timer scope.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
252 lambda: _do_render_page_segments_from_ctx(ctx),
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
253 fs_cache_time=page.content_mtime,
782
df58592b40f8 internal: Add missing timer scope.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
254 save_to_fs=save_to_fs)
df58592b40f8 internal: Add missing timer scope.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
255 else:
df58592b40f8 internal: Add missing timer scope.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
256 render_result = _do_render_page_segments_from_ctx(ctx)
df58592b40f8 internal: Add missing timer scope.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
257 if repo:
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
258 repo.put(page_uri, render_result, save_to_fs)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
259 finally:
338
938be93215cb bake: Improve render context and bake record, fix incremental bake bugs.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
260 ctx.setCurrentPass(PASS_NONE)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
261 stack.popCtx()
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
262
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
263 rs = RenderedSegments(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
264 render_result['segments'],
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
265 _unpickle_object(render_result['pass_info']))
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
266 return rs
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
267
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
268
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
269 def _build_render_data(ctx):
419
6801ad5aa1d4 internal: Optimize page segments rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 415
diff changeset
270 with ctx.app.env.timerScope("PageDataBuild"):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
271 data_ctx = DataBuildingContext(ctx.page, ctx.sub_num)
419
6801ad5aa1d4 internal: Optimize page segments rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 415
diff changeset
272 data_ctx.pagination_source = ctx.pagination_source
6801ad5aa1d4 internal: Optimize page segments rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 415
diff changeset
273 data_ctx.pagination_filter = ctx.pagination_filter
6801ad5aa1d4 internal: Optimize page segments rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 415
diff changeset
274 page_data = build_page_data(data_ctx)
6801ad5aa1d4 internal: Optimize page segments rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 415
diff changeset
275 if ctx.custom_data:
440
32c7c2d219d2 performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents: 430
diff changeset
276 page_data._appendMapping(ctx.custom_data)
419
6801ad5aa1d4 internal: Optimize page segments rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 415
diff changeset
277 return page_data
6801ad5aa1d4 internal: Optimize page segments rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 415
diff changeset
278
6801ad5aa1d4 internal: Optimize page segments rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 415
diff changeset
279
6801ad5aa1d4 internal: Optimize page segments rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 415
diff changeset
280 def _do_render_page_segments_from_ctx(ctx):
6801ad5aa1d4 internal: Optimize page segments rendering.
Ludovic Chabant <ludovic@chabant.com>
parents: 415
diff changeset
281 page_data = _build_render_data(ctx)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
282 return _do_render_page_segments(ctx, page_data)
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
283
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
284
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
285 def _do_render_page_segments(ctx, page_data):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
286 page = ctx.page
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
287 app = page.app
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
288
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
289 engine_name = page.config.get('template_engine')
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
290 format_name = page.config.get('format')
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
291
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
292 engine = get_template_engine(app, engine_name)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
293
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
294 formatted_segments = {}
578
683be25cbdb2 internal: Rename `raw_content` to `segments` since it's what it is.
Ludovic Chabant <ludovic@chabant.com>
parents: 574
diff changeset
295 for seg_name, seg in page.segments.items():
5
474c9882decf Upgrade to Python 3.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
296 seg_text = ''
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
297 for seg_part in seg.parts:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
298 part_format = seg_part.fmt or format_name
128
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
299 try:
803
bf9f4e55f751 rendering: Separate performance timers for renering segments and layouts.
Ludovic Chabant <ludovic@chabant.com>
parents: 784
diff changeset
300 with app.env.timerScope(
bf9f4e55f751 rendering: Separate performance timers for renering segments and layouts.
Ludovic Chabant <ludovic@chabant.com>
parents: 784
diff changeset
301 engine.__class__.__name__ + '_segment'):
454
96d363e2da4b templating: Let Jinja2 cache the parsed template for page contents.
Ludovic Chabant <ludovic@chabant.com>
parents: 440
diff changeset
302 part_text = engine.renderSegmentPart(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
303 page.path, seg_part, page_data)
128
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
304 except TemplatingError as err:
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
305 err.lineno += seg_part.line
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
306 raise err
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
307
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
308 part_text = format_text(app, part_format, part_text)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
309 seg_text += part_text
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
310 formatted_segments[seg_name] = seg_text
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
311
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
312 if seg_name == 'content':
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
313 m = content_abstract_re.search(seg_text)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
314 if m:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
315 offset = m.start()
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
316 content_abstract = seg_text[:offset]
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
317 formatted_segments['content.abstract'] = content_abstract
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
318
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
319 pass_info = ctx.render_passes[PASS_FORMATTING]
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
320 res = {
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
321 'segments': formatted_segments,
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 836
diff changeset
322 'pass_info': _pickle_object(pass_info)}
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
323 return res
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
324
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
325
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
326 def _do_render_layout(layout_name, page, layout_data):
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
327 cpi = page.app.env.exec_info_stack.current_page_info
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
328 assert cpi is not None
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
329 assert cpi.page == page
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
330
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
331 names = layout_name.split(',')
427
3b658190c02b performance: Compute default layout extensions only once.
Ludovic Chabant <ludovic@chabant.com>
parents: 419
diff changeset
332 default_exts = page.app.env.default_layout_extensions
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
333 full_names = []
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
334 for name in names:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
335 if '.' not in name:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
336 for ext in default_exts:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
337 full_names.append(name + ext)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
338 else:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
339 full_names.append(name)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
340
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
341 _, engine_name = os.path.splitext(full_names[0])
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
342 engine_name = engine_name.lstrip('.')
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
343 engine = get_template_engine(page.app, engine_name)
183
fff195335d0a render: When a template engine can't be found, show the correct name in the error.
Ludovic Chabant <ludovic@chabant.com>
parents: 174
diff changeset
344
153
1c3d229158ba Make a nice error message when a layout hasn't been found.
Ludovic Chabant <ludovic@chabant.com>
parents: 128
diff changeset
345 try:
803
bf9f4e55f751 rendering: Separate performance timers for renering segments and layouts.
Ludovic Chabant <ludovic@chabant.com>
parents: 784
diff changeset
346 with page.app.env.timerScope(engine.__class__.__name__ + '_layout'):
784
6d8fe8e93a91 internal: Add missing timer scope.
Ludovic Chabant <ludovic@chabant.com>
parents: 782
diff changeset
347 output = engine.renderFile(full_names, layout_data)
153
1c3d229158ba Make a nice error message when a layout hasn't been found.
Ludovic Chabant <ludovic@chabant.com>
parents: 128
diff changeset
348 except TemplateNotFoundError as ex:
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 698
diff changeset
349 logger.exception(ex)
153
1c3d229158ba Make a nice error message when a layout hasn't been found.
Ludovic Chabant <ludovic@chabant.com>
parents: 128
diff changeset
350 msg = "Can't find template for page: %s\n" % page.path
1c3d229158ba Make a nice error message when a layout hasn't been found.
Ludovic Chabant <ludovic@chabant.com>
parents: 128
diff changeset
351 msg += "Looked for: %s" % ', '.join(full_names)
1c3d229158ba Make a nice error message when a layout hasn't been found.
Ludovic Chabant <ludovic@chabant.com>
parents: 128
diff changeset
352 raise Exception(msg) from ex
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
353
698
33ab9badfd7a render: Change how we store render passes info.
Ludovic Chabant <ludovic@chabant.com>
parents: 586
diff changeset
354 pass_info = cpi.render_ctx.render_passes[PASS_RENDERING]
719
a066f4ac9094 rendering: Use `fastpickle` serialization before JSON.
Ludovic Chabant <ludovic@chabant.com>
parents: 715
diff changeset
355 res = {'content': output, 'pass_info': _pickle_object(pass_info)}
415
0e9a94b7fdfa bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
356 return res
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
357
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
358
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
359 def get_template_engine(app, engine_name):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
360 if engine_name == 'html':
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
361 engine_name = None
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
362 engine_name = engine_name or app.config.get('site/default_template_engine')
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
363 for engine in app.plugin_loader.getTemplateEngines():
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
364 if engine_name in engine.ENGINE_NAMES:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
365 return engine
183
fff195335d0a render: When a template engine can't be found, show the correct name in the error.
Ludovic Chabant <ludovic@chabant.com>
parents: 174
diff changeset
366 raise TemplateEngineNotFound("No such template engine: %s" % engine_name)
fff195335d0a render: When a template engine can't be found, show the correct name in the error.
Ludovic Chabant <ludovic@chabant.com>
parents: 174
diff changeset
367
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
368
128
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
369 def format_text(app, format_name, txt, exact_format=False):
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
370 if exact_format and not format_name:
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
371 raise Exception("You need to specify a format name.")
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
372
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
373 format_count = 0
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
374 format_name = format_name or app.config.get('site/default_format')
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
375 for fmt in app.plugin_loader.getFormatters():
457
7d868afc6791 rendering: Truly skip formatters that are not enabled.
Ludovic Chabant <ludovic@chabant.com>
parents: 454
diff changeset
376 if not fmt.enabled:
7d868afc6791 rendering: Truly skip formatters that are not enabled.
Ludovic Chabant <ludovic@chabant.com>
parents: 454
diff changeset
377 continue
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
378 if fmt.FORMAT_NAMES is None or format_name in fmt.FORMAT_NAMES:
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
379 with app.env.timerScope(fmt.__class__.__name__):
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
380 txt = fmt.render(format_name, txt)
128
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
381 format_count += 1
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
382 if fmt.OUTPUT_FORMAT is not None:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
383 format_name = fmt.OUTPUT_FORMAT
128
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
384 if exact_format and format_count == 0:
28444014ce7d Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents: 96
diff changeset
385 raise Exception("No such format: %s" % format_name)
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
386 return txt
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
387