view piecrust/formatting/markdownformatter.py @ 39:2f717f961996

Better error reporting and cache validation. Fix the processor pipeline in the preview server. Move the `pages` route to first position.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 21 Aug 2014 22:28:22 -0700
parents c95c90c82263
children 95590732e4c9
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._extensions = None

    def render(self, format_name, txt):
        self._ensureInitialized()
        return markdown(txt, extensions=self._extensions)

    def _ensureInitialized(self):
        if self._extensions is not None:
            return

        extensions = self.app.config.get('markdown/extensions')
        if extensions is None:
            extensions = []
        # Compatibility with PieCrust 1.x
        if self.app.config.get('markdown/use_markdown_extra'):
            extensions.append('extra')
        self._extensions = extensions