pinout.vvzero.com/api/mkjson.py

76 lines
1.5 KiB
Python

#!/usr/bin/env python
import json
import re
import sys
import unicodedata
try:
import markdown
except ImportError:
exit("This script requires the psutil module\nInstall with: sudo pip install Markdown")
import markjaml
import pinout
reload(sys)
sys.setdefaultencoding('utf8')
lang = "en"
if len(sys.argv) > 1:
lang = sys.argv[1]
pinout.load(lang)
overlays = pinout.settings['overlays']
pages = {}
def cssify(value):
value = slugify(value)
if value[0] == '3' or value[0] == '5':
value = 'pow' + value
return value
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_overlay(overlay):
try:
data = markjaml.load('src/{}/overlay/{}.md'.format(lang, overlay))
loaded = data['data']
except IOError:
return None
return loaded
def load_md(filename):
filename = 'src/{}/{}'.format(lang, filename)
try:
html = markdown.markdown(open(filename).read(), extensions=['fenced_code'])
return html
except IOError:
print('Unable to load markdown from {}'.format(filename))
return ''
overlays = map(load_overlay, overlays)
print(json.dumps(overlays, sort_keys=True))