2015-04-11 06:32:08 +08:00
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import unicodedata
|
|
|
|
|
2016-09-14 01:27:55 +08:00
|
|
|
try:
|
|
|
|
import markdown
|
|
|
|
except ImportError:
|
|
|
|
exit("This script requires the psutil module\nInstall with: sudo pip install Markdown")
|
|
|
|
|
|
|
|
try:
|
|
|
|
import yaml
|
|
|
|
except ImportError:
|
|
|
|
exit("This script requires the psutil module\nInstall with: sudo pip install PyYAML")
|
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
|
2015-04-11 06:32:08 +08:00
|
|
|
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)
|
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
|
2015-04-11 06:32:08 +08:00
|
|
|
def load(file):
|
2015-11-18 21:52:15 +08:00
|
|
|
'''
|
|
|
|
Loads and parses JSON-embedded Markdown file, chopping out and
|
|
|
|
parsing any JSON contained therein.
|
2015-04-11 06:32:08 +08:00
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
Returns an object that includes the JSON data, and the parsed HTML.
|
|
|
|
'''
|
|
|
|
markson = open(file).read()
|
2015-04-11 06:32:08 +08:00
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
_data = re.search(re.compile(r'<!--(JSON:|\n---\n)(.*)-->', re.DOTALL), markson)
|
2015-04-11 06:32:08 +08:00
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
_markdown = re.sub(re.compile(r'<!--(JSON:|\n---\n)(.*)-->', re.DOTALL), '', markson)
|
|
|
|
_html = markdown.markdown(_markdown, extensions=['fenced_code'])
|
2015-04-11 06:32:08 +08:00
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
# 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)
|
2015-04-11 06:32:08 +08:00
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
if _title != None:
|
|
|
|
_title = _title.group(0).replace('#', '').strip()
|
2015-04-11 06:32:08 +08:00
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
if _data != None:
|
|
|
|
_type = _data.group(0)[4:8].upper().strip()
|
2015-04-11 06:32:08 +08:00
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
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.load(_data)
|
|
|
|
else:
|
|
|
|
data = {}
|
2015-04-11 06:32:08 +08:00
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
_data['title'] = _title
|
2015-04-11 06:32:08 +08:00
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
elif _title != None:
|
|
|
|
_data = {'title':_title}
|
2015-04-11 06:32:08 +08:00
|
|
|
|
2015-11-18 21:52:15 +08:00
|
|
|
return {'data':_data, 'html':_html}
|