comparison piecrust/sources/prose.py @ 157:55910ab4bfea

First draft of the `prose` page source.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 27 Dec 2014 18:17:30 -0800
parents
children c3831a762bc2
comparison
equal deleted inserted replaced
156:683baa977d97 157:55910ab4bfea
1 import os
2 import os.path
3 import logging
4 from piecrust.sources.base import (
5 SimplePageSource, SimplePaginationSourceMixin)
6
7
8 logger = logging.getLogger(__name__)
9
10
11 class ProseSource(SimplePageSource,
12 SimplePaginationSourceMixin):
13 SOURCE_NAME = 'prose'
14
15 def __init__(self, app, name, config):
16 super(ProseSource, self).__init__(app, name, config)
17 self.config_recipe = config.get('config', {})
18
19 def buildPageFactories(self):
20 factories = super(ProseSource, self).buildPageFactories()
21 for f in factories:
22 f.metadata['config'] = self._makeConfig(f.path)
23 logger.debug(f.__dict__)
24 yield f
25
26 def findPagePath(self, metadata, mode):
27 rel_path, metadata = super(ProseSource, self).findPagePath(metadata, mode)
28 if rel_path:
29 metadata['config'] = self._makeConfig(self.resolveRef(rel_path))
30 return rel_path, metadata
31
32 def _makeConfig(self, path):
33 c = dict(self.config_recipe)
34 if c.get('title') == '%first_line%':
35 c['title'] = get_first_line(path)
36 return c
37
38
39 def get_first_line(path):
40 with open(path, 'r') as f:
41 while True:
42 l = f.readline()
43 if not l:
44 break
45 l = l.strip()
46 if not l:
47 continue
48 return l
49 return None
50