view piecrust/sources/prose.py @ 1194:09d5c233e840 draft

sources: Fix bug checking config setting.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 30 Dec 2022 16:49:32 -0800
parents a4d7ff2cdc5c
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