annotate piecrust/sources/prose.py @ 369:4b1019bb2533

serve: Giant refactor to change how we handle data when serving pages. * We need a distinction between source metadata and route metadata. In most cases they're the same, but in cases like taxonomy pages, route metadata contains more things that can't be in source metadata if we want to re-use cached pages. * Create a new `QualifiedPage` type which is a page with a specific route and route metadata. Pass this around in many places. * Instead of passing an URL around, use the route in the `QualifiedPage` to generate URLs. This is better since it removes the guess-work from trying to generate URLs for sub-pages. * Deep-copy app and page configurations before passing them around to things that could modify them, like data builders and such. * Exclude taxonomy pages from iterator data providers. * Properly nest iterator data providers for when the theme and user page sources are merged inside `site.pages`.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 03 May 2015 18:47:10 -0700
parents f130365568ff
children 65db6df28120
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
369
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
3 import copy
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import logging
369
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
5 from piecrust.sources.base import MODE_CREATING, MODE_PARSING
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 186
diff changeset
6 from piecrust.sources.default import DefaultPageSource
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 logger = logging.getLogger(__name__)
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 186
diff changeset
12 class ProseSource(DefaultPageSource):
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 SOURCE_NAME = 'prose'
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 def __init__(self, app, name, config):
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 super(ProseSource, self).__init__(app, name, config)
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 self.config_recipe = config.get('config', {})
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
186
e61fbae61402 sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 170
diff changeset
19 def _populateMetadata(self, rel_path, metadata, mode=None):
e61fbae61402 sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 170
diff changeset
20 metadata['config'] = self._makeConfig(rel_path, mode)
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
186
e61fbae61402 sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 170
diff changeset
22 def _makeConfig(self, rel_path, mode):
369
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
23 c = copy.deepcopy(self.config_recipe)
186
e61fbae61402 sources: Pass any current mode to `_populateMetadata` when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 170
diff changeset
24 if c.get('title') == '%first_line%' and mode != MODE_CREATING:
170
c3831a762bc2 sources: Make the `SimplePageSource` more extensible, fix bugs in `prose` source.
Ludovic Chabant <ludovic@chabant.com>
parents: 157
diff changeset
25 path = os.path.join(self.fs_endpoint_path, rel_path)
369
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
26 try:
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
27 c['title'] = get_first_line(path)
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
28 except OSError:
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
29 if mode == MODE_PARSING:
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
30 raise
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 return c
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 def get_first_line(path):
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 with open(path, 'r') as f:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 while True:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 l = f.readline()
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 if not l:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 break
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 l = l.strip()
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 if not l:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 continue
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 return l
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 return None
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45