-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
markjaml.py
66 lines (50 loc) · 2.02 KB
/
markjaml.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
import json
import re
import unicodedata
try:
import markdown
except ImportError:
exit("This script requires the Markdown module\nInstall with: sudo pip install Markdown")
try:
import yaml
except ImportError:
exit("This script requires the yaml module\nInstall with: sudo pip install PyYAML")
def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
value = unicode(value)
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub('[^\w\s-]', '-', value).strip().lower()
return re.sub('[-\s]+', '-', value)
def load(file):
'''
Loads and parses JSON-embedded Markdown file, chopping out and
parsing any JSON contained therein.
Returns an object that includes the JSON data, and the parsed HTML.
'''
markson = open(file).read()
markson = markson.replace('\r', '')
_data = re.search(re.compile(r'<!--(JSON:|\n---\n)(.*?)-->', re.DOTALL), markson)
_markdown = re.sub(re.compile(r'<!--(JSON:|\n---\n)(.*?)-->', re.DOTALL), '', markson)
_html = markdown.markdown(_markdown, extensions=['fenced_code'])
# Scan for the Title in the Markdown file, this is always assumed
# to be the first string starting with a single hash/pound ( # ) sign
_title = re.search(re.compile(r'^#[^\#](.*)$', re.MULTILINE), markson)
if _title is not None:
_title = _title.group(0).replace('#', '').strip()
if _data is not None:
_type = _data.group(0)[4:8].upper().strip()
if _type == 'JSON':
_data = re.search('\{(.*)\}', _data.group(0), re.DOTALL).group(0)
_data = json.loads(_data)
elif _type == '---':
_data = re.search('\n(.*)\n', _data.group(0), re.DOTALL).group(0)
_data = yaml.safe_load(_data)
else:
data = {}
_data['title'] = _title
elif _title is not None:
_data = {'title': _title}
return {'data': _data, 'html': _html}