view piecrust/formatting/markdownformatter.py @ 1168:10520472cc73

routing: Fix breakages with routing on some versions of Python. Finally figured what happened with change 6baa94da8b16 (this is a Mercurial hash by the way if you're looking at the Git mirror). Between Python 3.6 and 3.7 there was a change where the percent sign ('%') went from being escaped by `re.escape` to _not_ being escaped. So now we need to use different regex patterns dependin on the Python version, yay.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 04 Oct 2019 11:13:33 -0700
parents 8178671f9a04
children
line wrap: on
line source

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', {})

        from markdown import Markdown
        self._formatter = Markdown(extensions=extensions,
                                   extension_configs=extension_configs)