Mercurial > piecrust2
view piecrust/formatting/markdownformatter.py @ 666:81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
* Compute cache keys up front, so the cache directory is only chosen once.
* Buffer up config variants to apply before loading the config. Makes it
possible to cache variant-resulting configs, too.
* Make a factory class to reuse the logic that creates the `PieCrust` object
correctly for multi-process workers and such.
* Add a test.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 03 Mar 2016 08:22:41 -0800 |
parents | 79f03b5c1b8f |
children | 370e74941d32 |
line wrap: on
line source
from markdown import Markdown from piecrust.formatting.base import Formatter class MarkdownFormatter(Formatter): FORMAT_NAMES = ['markdown', 'mdown', 'md'] OUTPUT_FORMAT = 'html' def __init__(self): super(MarkdownFormatter, self).__init__() self._formatter = None def render(self, format_name, txt): assert format_name in self.FORMAT_NAMES self._ensureInitialized() return self._formatter.reset().convert(txt) def _ensureInitialized(self): if self._formatter is not None: return config = self.app.config.get('markdown') if config is None: config = {} elif not isinstance(config, dict): raise Exception("The `markdown` configuration setting must be " "a dictionary.") extensions = config.get('extensions', []) if isinstance(extensions, str): extensions = [e.strip() for e in extensions.split(',')] # Compatibility with PieCrust 1.x if config.get('use_markdown_extra'): extensions.append('extra') extension_configs = config.get('extension_configs', {}) self._formatter = Markdown(extensions=extensions, extension_configs=extension_configs)