Mercurial > piecrust2
comparison piecrust/data/builder.py @ 12:30a42341cfa8
Define page slugs properly, avoid recursions with debug data.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 18 Aug 2014 16:49:54 -0700 |
parents | 474c9882decf |
children | 62c7a97c8340 |
comparison
equal
deleted
inserted
replaced
11:617191dec18e | 12:30a42341cfa8 |
---|---|
3 from piecrust.configuration import merge_dicts | 3 from piecrust.configuration import merge_dicts |
4 from piecrust.data.assetor import Assetor | 4 from piecrust.data.assetor import Assetor |
5 from piecrust.data.debug import build_debug_info | 5 from piecrust.data.debug import build_debug_info |
6 from piecrust.data.linker import Linker | 6 from piecrust.data.linker import Linker |
7 from piecrust.data.paginator import Paginator | 7 from piecrust.data.paginator import Paginator |
8 from piecrust.uriutil import get_slug | |
8 | 9 |
9 | 10 |
10 logger = logging.getLogger(__name__) | 11 logger = logging.getLogger(__name__) |
11 | 12 |
12 | 13 |
16 self.uri = uri | 17 self.uri = uri |
17 self.page_num = page_num | 18 self.page_num = page_num |
18 self.pagination_source = None | 19 self.pagination_source = None |
19 self.pagination_filter = None | 20 self.pagination_filter = None |
20 | 21 |
22 @property | |
23 def slug(self): | |
24 return get_slug(self.page.app, self.uri) | |
25 | |
21 | 26 |
22 def build_page_data(ctx): | 27 def build_page_data(ctx): |
23 page = ctx.page | 28 page = ctx.page |
24 app = page.app | 29 app = page.app |
25 | 30 |
31 pc_data = PieCrustData() | |
26 pgn_source = ctx.pagination_source or get_default_pagination_source(page) | 32 pgn_source = ctx.pagination_source or get_default_pagination_source(page) |
27 paginator = Paginator(page, pgn_source, ctx.uri, ctx.page_num, | 33 paginator = Paginator(page, pgn_source, ctx.uri, ctx.page_num, |
28 ctx.pagination_filter) | 34 ctx.pagination_filter) |
29 assetor = Assetor(page, ctx.uri) | 35 assetor = Assetor(page, ctx.uri) |
30 linker = Linker(page) | 36 linker = Linker(page) |
31 data = { | 37 data = { |
32 'piecrust': build_piecrust_data(), | 38 'piecrust': pc_data, |
33 'page': dict(page.config.get()), | 39 'page': dict(page.config.get()), |
34 'assets': assetor, | 40 'assets': assetor, |
35 'pagination': paginator, | 41 'pagination': paginator, |
36 'siblings': linker, | 42 'siblings': linker, |
37 'family': linker | 43 'family': linker |
38 } | 44 } |
39 page_data = data['page'] | 45 page_data = data['page'] |
40 page_data['url'] = ctx.uri | 46 page_data['url'] = ctx.uri |
47 page_data['slug'] = ctx.slug | |
41 page_data['timestamp'] = time.mktime(page.datetime.timetuple()) | 48 page_data['timestamp'] = time.mktime(page.datetime.timetuple()) |
42 date_format = app.config.get('site/date_format') | 49 date_format = app.config.get('site/date_format') |
43 if date_format: | 50 if date_format: |
44 page_data['date'] = page.datetime.strftime(date_format) | 51 page_data['date'] = page.datetime.strftime(date_format) |
45 | 52 |
50 | 57 |
51 # Do this at the end because we want all the data to be ready to be | 58 # Do this at the end because we want all the data to be ready to be |
52 # displayed in the debugger window. | 59 # displayed in the debugger window. |
53 if (app.debug and app.config.get('site/enable_debug_info') and | 60 if (app.debug and app.config.get('site/enable_debug_info') and |
54 not app.config.get('baker/is_baking')): | 61 not app.config.get('baker/is_baking')): |
55 data['piecrust']['debug_info'] = build_debug_info(page, data) | 62 pc_data._enableDebugInfo(page, data) |
56 | 63 |
57 return data | 64 return data |
58 | 65 |
59 | 66 |
60 def build_layout_data(page, page_data, contents): | 67 def build_layout_data(page, page_data, contents): |
71 from piecrust.__version__ import VERSION | 78 from piecrust.__version__ import VERSION |
72 except ImportError: | 79 except ImportError: |
73 from piecrust import APP_VERSION as VERSION | 80 from piecrust import APP_VERSION as VERSION |
74 | 81 |
75 | 82 |
76 def build_piecrust_data(): | 83 class PieCrustData(object): |
77 data = { | 84 debug_render = ['version', 'url', 'branding', 'debug_info'] |
78 'version': VERSION, | 85 debug_render_invoke = ['version', 'url', 'branding', 'debug_info'] |
79 'url': 'http://bolt80.com/piecrust/', | 86 debug_render_redirect = {'debug_info': '_debugRenderDebugInfo'} |
80 'branding': 'Baked with <em><a href="%s">PieCrust</a> %s</em>.' % ( | 87 |
88 def __init__(self): | |
89 self.version = VERSION | |
90 self.url = 'http://bolt80.com/piecrust/' | |
91 self.branding = 'Baked with <em><a href="%s">PieCrust</a> %s</em>.' % ( | |
81 'http://bolt80.com/piecrust/', VERSION) | 92 'http://bolt80.com/piecrust/', VERSION) |
82 } | 93 self._page = None |
83 return data | 94 self._data = None |
95 | |
96 @property | |
97 def debug_info(self): | |
98 if self._page is not None and self._data is not None: | |
99 return build_debug_info(self._page, self._data) | |
100 return None | |
101 | |
102 def _enableDebugInfo(self, page, data): | |
103 self._page = page | |
104 self._data = data | |
105 | |
106 def _debugRenderDebugInfo(self): | |
107 return "The very thing you're looking at!" | |
84 | 108 |
85 | 109 |
86 def build_site_data(page): | 110 def build_site_data(page): |
87 app = page.app | 111 app = page.app |
88 data = dict(app.config.get()) | 112 data = dict(app.config.get()) |