Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
98 lines (74 sloc) 2.48 KB
# coding=utf8
#
# Python implementation for visionmedia's mdconf,
# the markdown driven configuration::
# https://github.com/visionmedia/mdconf
# This module was built on the top of misaka:
# https://github.com/FSX/misaka
import misaka
from misaka import HtmlRenderer
MAP = 11
LIST = 12
class MdconfRenderer(HtmlRenderer):
"""misaka renderer for mdconf"""
def __init__(self, *args, **kwargs):
super(MdconfRenderer, self).__init__(*args, **kwargs)
self.reset_vars()
def reset_vars(self):
"""reset conf retult"""
self.conf = {} # the return var
self.keys = [] # current group's position
def put(self, text, type=MAP):
"""put text to the right position in conf"""
last = None
crt = self.conf
for key in self.keys:
last = crt
crt = crt.setdefault(key, {})
if type == LIST:
if not isinstance(last[key], list):
last[key] = []
last[key].append(self.normalize(text))
elif type == MAP:
index = text.find(":")
assert index > 0 # index should > 0
key, value = (
self.normalize(text[:index]),
self.normalize(text[index+1:]),
)
crt[key] = value
def header(self, text, level):
# polish keys to given depth
while len(self.keys) >= level:
self.keys.pop()
self.keys.append(self.normalize(text))
def list_item(self, text, is_ordered):
if ":" in text:
self.put(text, type=MAP)
else:
self.put(text, type=LIST)
return self.paragraph(text)
def paragraph(self, text):
"""Here returns text no matter wether in list"""
return text
def block_code(self, text, lang):
self.put(text, type=LIST)
def normalize(self, text):
"""util to normalize text to lowercase"""
return text.strip().lower()
class MdconfParser(object):
def __init__(self, extensions=(
misaka.EXT_FENCED_CODE |
misaka.EXT_NO_INTRA_EMPHASIS |
misaka.EXT_AUTOLINK
)
):
self.renderer = MdconfRenderer()
self.markdown = misaka.Markdown(self.renderer, extensions=extensions)
def parse(self, text):
self.renderer.reset_vars()
self.markdown.render(text)
return self.renderer.conf
parser = MdconfParser() # build a quick use parser
def parse(text):
return parser.parse(text)