Mercurial > piecrust2
comparison piecrust/formatting/hoedownformatter.py @ 1006:58ef814cc83e
formatting: Replace `python-hoedown` with `misaka`.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 25 Nov 2017 22:37:46 -0800 |
parents | 8adc27285d93 |
children | 0302f690a4c5 |
comparison
equal
deleted
inserted
replaced
1005:2e5c5d33d62c | 1006:58ef814cc83e |
---|---|
14 self._formatter = None | 14 self._formatter = None |
15 | 15 |
16 def render(self, format_name, txt): | 16 def render(self, format_name, txt): |
17 assert format_name in self.FORMAT_NAMES | 17 assert format_name in self.FORMAT_NAMES |
18 self._ensureInitialized() | 18 self._ensureInitialized() |
19 return self._formatter.render(txt) | 19 return self._formatter(txt) |
20 | 20 |
21 def _ensureInitialized(self): | 21 def _ensureInitialized(self): |
22 if self._formatter is not None: | 22 if self._formatter is not None: |
23 return | 23 return |
24 | 24 |
25 import hoedown | 25 import misaka |
26 | 26 |
27 # Don't show warnings once for each worker when baking, so only | 27 # Don't show warnings once for each worker when baking, so only |
28 # show them for the first. If the variable is not set, we're not | 28 # show them for the first. If the variable is not set, we're not |
29 # baking so do show them either way. | 29 # baking so do show them either way. |
30 show_warnings = (self.app.config.get('baker/worker_id', 0) == 0) | 30 show_warnings = (self.app.config.get('baker/worker_id', 0) == 0) |
53 exts = 0 | 53 exts = 0 |
54 rdrf = 0 | 54 rdrf = 0 |
55 other = 0 | 55 other = 0 |
56 for n in extensions: | 56 for n in extensions: |
57 # Try an extension? | 57 # Try an extension? |
58 e = getattr(hoedown, 'EXT_' + n.upper(), None) | 58 e = getattr(misaka, 'EXT_' + n.upper(), None) |
59 if e is not None: | 59 if e is not None: |
60 exts |= e | 60 exts |= e |
61 continue | 61 continue |
62 | 62 |
63 # Try a render flag? | 63 # Try a render flag? |
64 f = getattr(hoedown, 'HTML_' + n.upper(), None) | 64 f = getattr(misaka, 'HTML_' + n.upper(), None) |
65 if f is not None: | 65 if f is not None: |
66 rdrf |= f | 66 rdrf |= f |
67 | 67 |
68 # Other flag? | 68 # Other flag? |
69 f = getattr(hoedown, 'TABLE_' + n.upper(), None) | 69 f = getattr(misaka, 'TABLE_' + n.upper(), None) |
70 if f is not None: | 70 if f is not None: |
71 other |= f | 71 other |= f |
72 | 72 |
73 # Try translating from a Markdown extension name. | 73 # Try translating from a Markdown extension name. |
74 t = ext_translate.get(n) | 74 t = ext_translate.get(n) |
79 continue | 79 continue |
80 if not isinstance(t, list): | 80 if not isinstance(t, list): |
81 t = [t] | 81 t = [t] |
82 for i in t: | 82 for i in t: |
83 if i.startswith('EXT_'): | 83 if i.startswith('EXT_'): |
84 exts |= getattr(hoedown, i) | 84 exts |= getattr(misaka, i) |
85 elif i.startswith('HTML_'): | 85 elif i.startswith('HTML_'): |
86 rdrf |= getattr(hoedown, i) | 86 rdrf |= getattr(misaka, i) |
87 elif show_warnings: | 87 elif show_warnings: |
88 logger.warning("Unknown Hoedown Markdown extension or flag:" | 88 logger.warning("Unknown Hoedown Markdown extension or flag:" |
89 "%s" % n) | 89 "%s" % n) |
90 if n == 'extra' and show_warnings: | 90 if n == 'extra' and show_warnings: |
91 # Special warning for the 'extra' extension. | 91 # Special warning for the 'extra' extension. |
95 "'tables' extensions will be active. " | 95 "'tables' extensions will be active. " |
96 "To remove this warning, replace 'extra' with those 3 " | 96 "To remove this warning, replace 'extra' with those 3 " |
97 "specific extensions.") | 97 "specific extensions.") |
98 | 98 |
99 # Enable a few things by default. | 99 # Enable a few things by default. |
100 exts |= hoedown.EXT_NO_INTRA_EMPHASIS | 100 exts |= misaka.EXT_NO_INTRA_EMPHASIS |
101 | 101 |
102 renderer = hoedown.HtmlRenderer(flags=rdrf) | 102 renderer = misaka.HtmlRenderer(flags=rdrf) |
103 self._formatter = hoedown.Markdown( | 103 self._formatter = misaka.Markdown(renderer, extensions=(exts | other)) |
104 renderer, extensions=(exts | other)) | |
105 | 104 |
106 | 105 |
107 ext_translate = { | 106 ext_translate = { |
108 'fenced_code': 'EXT_FENCED_CODE', | 107 'fenced_code': 'EXT_FENCED_CODE', |
109 'footnotes': 'EXT_FOOTNOTES', | 108 'footnotes': 'EXT_FOOTNOTES', |