Mercurial > piecrust2
view piecrust/sources/prose.py @ 1089:a4d7ff2cdc5c
sources: Update prose source to correctly use the new source API.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 16 Feb 2018 00:14:27 -0800 |
parents | d9059257743c |
children |
line wrap: on
line source
import copy import logging from piecrust.sources.default import DefaultContentSource logger = logging.getLogger(__name__) class ProseSource(DefaultContentSource): SOURCE_NAME = 'prose' def __init__(self, app, name, config): super().__init__(app, name, config) self.config_recipe = config.get('config', {}) def _createItemMetadata(self, path): metadata = super()._createItemMetadata(path) config = metadata.setdefault('config', {}) config.update(self._makeConfig(path)) return metadata def _makeConfig(self, path): c = copy.deepcopy(self.config_recipe) if c.get('title') == '%first_line%': c['title'] = get_first_line(path) return c def get_first_line(path): with open(path, 'r') as f: while True: line = f.readline() if not line: break line = line.strip() if not line: continue return line return None