forked from revolunet/sublimetext-markdown-preview
-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
markdown_wrapper.py
46 lines (39 loc) · 1.74 KB
/
markdown_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Markdown Preview wrapper."""
from __future__ import absolute_import
import traceback
from markdown import Markdown, util, version_info
from markdown.extensions import Extension
class StMarkdown(Markdown):
"""A wrapper around "markdown" lib."""
def __init__(self, *args, **kwargs):
"""Initialize."""
Markdown.__init__(self, *args, **kwargs)
self.Meta = {}
def registerExtensions(self, extensions, configs): # noqa
"""
Register extensions with this instance of Markdown.
Keyword arguments:
* `extensions`: A list of extensions, which can either
be strings or objects. See the docstring on Markdown.
* `configs`: A dictionary mapping module names to config options.
We are overriding this in order to gracefully handle bad extensions
and to prevent old deprecated style of 'extensions(option=value)'.
"""
md3 = version_info[0] > 2
for ext in extensions:
try:
# Make sure we aren't using old form `extension(option=value)`
if isinstance(ext, util.string_type) and ('(' not in ext):
ext = self.build_extension(ext, configs.get(ext, []))
if isinstance(ext, Extension):
if md3:
ext._extendMarkdown(self)
else:
ext.extendMarkdown(self, globals())
elif ext is not None:
raise TypeError(
'Extension "%s.%s" must be of type: "markdown.Extension"'
% (ext.__class__.__module__, ext.__class__.__name__))
except Exception:
print(str(traceback.format_exc()))
return self