comparison piecrust/sources/prose.py @ 170:c3831a762bc2

sources: Make the `SimplePageSource` more extensible, fix bugs in `prose` source. The `SimplePageSource` now calls a `_populateMetadata` function that subclasses can override to add/edit their custom metadata everwhere it would be returned to the system.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 03 Jan 2015 20:49:00 -0800
parents 55910ab4bfea
children e61fbae61402
comparison
equal deleted inserted replaced
169:f3aa511eef99 170:c3831a762bc2
14 14
15 def __init__(self, app, name, config): 15 def __init__(self, app, name, config):
16 super(ProseSource, self).__init__(app, name, config) 16 super(ProseSource, self).__init__(app, name, config)
17 self.config_recipe = config.get('config', {}) 17 self.config_recipe = config.get('config', {})
18 18
19 def buildPageFactories(self): 19 def _populateMetadata(self, rel_path, metadata):
20 factories = super(ProseSource, self).buildPageFactories() 20 metadata['config'] = self._makeConfig(rel_path)
21 for f in factories:
22 f.metadata['config'] = self._makeConfig(f.path)
23 logger.debug(f.__dict__)
24 yield f
25 21
26 def findPagePath(self, metadata, mode): 22 def _makeConfig(self, rel_path):
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) 23 c = dict(self.config_recipe)
34 if c.get('title') == '%first_line%': 24 if c.get('title') == '%first_line%':
25 path = os.path.join(self.fs_endpoint_path, rel_path)
35 c['title'] = get_first_line(path) 26 c['title'] = get_first_line(path)
36 return c 27 return c
37 28
38 29
39 def get_first_line(path): 30 def get_first_line(path):