Mercurial > piecrust2
comparison piecrust/formatting/markdownformatter.py @ 410:d1a472464e57
markdown: Cache the formatter once.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 30 May 2015 15:41:52 -0700 |
parents | cd049786c008 |
children | e7b865f8f335 |
comparison
equal
deleted
inserted
replaced
409:2bb5327c4c1f | 410:d1a472464e57 |
---|---|
1 from markdown import markdown | 1 from markdown import Markdown |
2 from piecrust.formatting.base import Formatter | 2 from piecrust.formatting.base import Formatter |
3 | 3 |
4 | 4 |
5 class MarkdownFormatter(Formatter): | 5 class MarkdownFormatter(Formatter): |
6 FORMAT_NAMES = ['markdown', 'mdown', 'md'] | 6 FORMAT_NAMES = ['markdown', 'mdown', 'md'] |
7 OUTPUT_FORMAT = 'html' | 7 OUTPUT_FORMAT = 'html' |
8 | 8 |
9 def __init__(self): | 9 def __init__(self): |
10 super(MarkdownFormatter, self).__init__() | 10 super(MarkdownFormatter, self).__init__() |
11 self._extensions = None | 11 self._formatter = None |
12 | 12 |
13 def render(self, format_name, txt): | 13 def render(self, format_name, txt): |
14 assert format_name in self.FORMAT_NAMES | 14 assert format_name in self.FORMAT_NAMES |
15 self._ensureInitialized() | 15 self._ensureInitialized() |
16 return markdown(txt, extensions=self._extensions) | 16 return self._formatter.convert(txt) |
17 | 17 |
18 def _ensureInitialized(self): | 18 def _ensureInitialized(self): |
19 if self._extensions is not None: | 19 if self._formatter is not None: |
20 return | 20 return |
21 | 21 |
22 config = self.app.config.get('markdown') | 22 config = self.app.config.get('markdown') |
23 if config is None: | 23 if config is None: |
24 config = {} | 24 config = {} |
32 if isinstance(extensions, str): | 32 if isinstance(extensions, str): |
33 extensions = [e.strip() for e in extensions.split(',')] | 33 extensions = [e.strip() for e in extensions.split(',')] |
34 # Compatibility with PieCrust 1.x | 34 # Compatibility with PieCrust 1.x |
35 if config.get('use_markdown_extra'): | 35 if config.get('use_markdown_extra'): |
36 extensions.append('extra') | 36 extensions.append('extra') |
37 self._extensions = extensions | |
38 | 37 |
38 self._formatter = Markdown(extensions=extensions) | |
39 |