view piecrust/sources/prose.py @ 853:f070a4fc033c

core: Continue PieCrust3 refactor, simplify pages. The asset pipeline is still the only function pipeline at this point. * No more `QualifiedPage`, and several other pieces of code deleted. * Data providers are simpler and more focused. For instance, the page iterator doesn't try to support other types of items. * Route parameters are proper known source metadata to remove the confusion between the two. * Make the baker and pipeline more correctly manage records and record histories. * Add support for record collapsing and deleting stale outputs in the asset pipeline.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 21 May 2017 00:06:59 -0700
parents 4850f8c21b6e
children d9059257743c
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 _doCreateItemMetadata(self, path):
        metadata = super()._doCreateItemMetadata(path)
        config = metadata.setdefault('config', {})
        config.update(self._makeConfig(path))
        return config

    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:
            l = f.readline()
            if not l:
                break
            l = l.strip()
            if not l:
                continue
            return l
    return None