annotate piecrust/formatting/hoedownformatter.py @ 868:8d25f76fce98

bake: Add ability to specify pipelines to exclude during the bake.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 12 Jun 2017 22:22:19 -0700
parents 0c688063890f
children 8adc27285d93
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import logging
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 from piecrust.formatting.base import Formatter
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 logger = logging.getLogger(__name__)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 class HoedownFormatter(Formatter):
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 FORMAT_NAMES = ['hoedown']
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 OUTPUT_FORMAT = 'html'
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 def __init__(self):
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 super(HoedownFormatter, self).__init__()
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 self._formatter = None
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 def render(self, format_name, txt):
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 assert format_name in self.FORMAT_NAMES
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 self._ensureInitialized()
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 return self._formatter.render(txt)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 def _ensureInitialized(self):
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 if self._formatter is not None:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 return
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24
695
0c688063890f formatting: Don't import `hoedown` until we need it.
Ludovic Chabant <ludovic@chabant.com>
parents: 694
diff changeset
25 import hoedown
0c688063890f formatting: Don't import `hoedown` until we need it.
Ludovic Chabant <ludovic@chabant.com>
parents: 694
diff changeset
26
694
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 # Don't show warnings once for each worker when baking, so only
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 # show them for the first. If the variable is not set, we're not
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 # baking so do show them either way.
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 show_warnings = (self.app.config.get('baker/worker_id', 0) == 0)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 config = self.app.config.get('hoedown')
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 if config is None:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 config = {}
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 elif not isinstance(config, dict):
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 raise Exception("The `hoedown` configuration setting must be "
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 "a dictionary.")
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 extensions = config.get('extensions', [])
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 if isinstance(extensions, str):
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 extensions = [e.strip() for e in extensions.split(',')]
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 # Compatibility with PieCrust 1.x
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 if config.get('use_markdown_extra'):
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 extensions.append('extra')
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 render_flags = config.get('render_flags')
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 if render_flags is None:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 render_flags = []
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 # Translate standard Markdown formatter extensions to Hoedown
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 # extension/render flags to make it easier to use Hoedown as a drop-in
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 # replacement.
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 exts = 0
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 rdrf = 0
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 other = 0
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 for n in extensions:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 # Try an extension?
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 e = getattr(hoedown, 'EXT_' + n.upper(), None)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 if e is not None:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 exts |= e
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 continue
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 # Try a render flag?
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 f = getattr(hoedown, 'HTML_' + n.upper(), None)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 if f is not None:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 rdrf |= f
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 # Other flag?
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 f = getattr(hoedown, 'TABLE_' + n.upper(), None)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 if f is not None:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 other |= f
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 # Try translating from a Markdown extension name.
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 t = ext_translate.get(n)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 if t is None:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 if show_warnings:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 logger.warning("Unknown Hoedown Markdown extension or flag: "
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 "%s" % n)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 continue
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 if not isinstance(t, list):
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 t = [t]
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 for i in t:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 if i.startswith('EXT_'):
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 exts |= getattr(hoedown, i)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 elif i.startswith('HTML_'):
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 rdrf |= getattr(hoedown, i)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 elif show_warnings:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 logger.warning("Unknown Hoedown Markdown extension or flag:"
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 "%s" % n)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 if n == 'extra' and show_warnings:
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 # Special warning for the 'extra' extension.
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 logger.warning(
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 "The 'extra' extension doesn't have a full equivalent "
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 "in Hoedown Markdown. Only 'fenced_code', 'footnotes' and "
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 "'tables' extensions will be active. "
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 "To remove this warning, replace 'extra' with those 3 "
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 "specific extensions.")
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 # Enable a few things by default.
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 exts |= hoedown.EXT_NO_INTRA_EMPHASIS
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 renderer = hoedown.HtmlRenderer(flags=rdrf)
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 self._formatter = hoedown.Markdown(
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 renderer, extensions=(exts | other))
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 ext_translate = {
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 'fenced_code': 'EXT_FENCED_CODE',
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 'footnotes': 'EXT_FOOTNOTES',
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 'tables': 'EXT_TABLES',
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 'nl2br': 'HTML_HARD_WRAP',
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 'smarty': 'HTML_SMARTYPANTS',
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 'smartypants': 'HTML_SMARTYPANTS',
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 'toc': 'HTML_TOC',
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 'extra': ['EXT_FENCED_CODE', 'EXT_FOOTNOTES', 'EXT_TABLES']
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 }
b917ae071994 formatting: Add a `hoedown` formatter.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117