-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathpymdownx_md_render.py
71 lines (55 loc) · 1.97 KB
/
pymdownx_md_render.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Generate Markdown isolated from our current document options."""
import markdown
import yaml
import re
from collections import OrderedDict
def yaml_load(stream, loader=yaml.Loader):
"""
Custom YAML loader.
Load all strings as Unicode.
http://stackoverflow.com/a/2967461/3609487
"""
def construct_yaml_str(self, node):
"""Override the default string handling function to always return Unicode objects."""
return self.construct_scalar(node)
class Loader(loader):
"""Custom Loader."""
Loader.add_constructor(
'tag:yaml.org,2002:str',
construct_yaml_str
)
return yaml.load(stream, Loader)
def get_frontmatter(text):
"""Get front matter from string."""
frontmatter = OrderedDict()
if text.startswith("---"):
m = re.search(r'^(-{3}\r?\n(?!\r?\n)(.*?)(?<=\n)(?:-{3}|\.{3})\r?\n)', text, re.DOTALL)
if m:
yaml_okay = True
try:
frontmatter = yaml_load(m.group(2))
if frontmatter is None:
frontmatter = OrderedDict()
# If we didn't get a dictionary, we don't want this as it isn't front matter.
assert isinstance(frontmatter, (dict, OrderedDict)), TypeError
except Exception:
# We had a parsing error. This is not the YAML we are looking for.
yaml_okay = False
frontmatter = OrderedDict()
if yaml_okay:
text = text[m.end(1):]
return frontmatter, text
def md_sub_render(src="", language="", class_name=None, options=None, md="", **kwargs):
"""Formatter wrapper."""
try:
fm, text = get_frontmatter(src)
md = markdown.markdown(
text,
extensions=fm.get('extensions', []),
extension_configs=fm.get('extension_configs', {})
)
return md
except Exception:
import traceback
print(traceback.format_exc())
raise