annotate piecrust/sources/prose.py @ 877:d6d35b2efd04

bake: Rename "pass" to "step" and make the page pipeline use different steps. That pipeline is now first loading all pages, and then rendering full pages unless they trigger a sub-render.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 15 Jun 2017 22:16:23 -0700
parents d9059257743c
children a4d7ff2cdc5c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
369
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 242
diff changeset
1 import copy
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import logging
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 394
diff changeset
3 from piecrust.sources.default import DefaultContentSource
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 logger = logging.getLogger(__name__)
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
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 394
diff changeset
9 class ProseSource(DefaultContentSource):
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 SOURCE_NAME = 'prose'
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 def __init__(self, app, name, config):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
13 super().__init__(app, name, config)
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 self.config_recipe = config.get('config', {})
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
16 def _doCreateItemMetadata(self, path):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
17 metadata = super()._doCreateItemMetadata(path)
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
18 config = metadata.setdefault('config', {})
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
19 config.update(self._makeConfig(path))
866
d9059257743c refactor: Make the linker work again.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
20 return metadata
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
22 def _makeConfig(self, path):
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)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
24 if c.get('title') == '%first_line%':
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
25 c['title'] = get_first_line(path)
157
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 return c
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 def get_first_line(path):
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 with open(path, 'r') as f:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 while True:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 l = f.readline()
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 if not l:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 break
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 l = l.strip()
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 if not l:
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 continue
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 return l
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 return None
55910ab4bfea First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40