47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
from jinja2 import Template
|
|
import yaml
|
|
import markdown
|
|
|
|
|
|
def parse_item(item, h):
|
|
item_type = type(item)
|
|
if item_type is list:
|
|
if len(item) == 1:
|
|
return parse_item(item[0], h+1)
|
|
if len(item) == 0:
|
|
return ''
|
|
return parse_item('\n'.join(['- ' + str(sub_item) for sub_item in item]), h+1)
|
|
if item_type is dict:
|
|
return {
|
|
"<h{0}>{1}</h{0}>".format(h, key): parse_item(value, h+1) for key, value in item.items()
|
|
}
|
|
if item_type in [int, float]:
|
|
return item
|
|
return markdown.markdown(str(item))
|
|
|
|
def output(item):
|
|
item_type = type(item)
|
|
if item_type is list:
|
|
[output(sub_item) for sub_item in item]
|
|
return
|
|
if item_type is dict:
|
|
for key, value in item.items():
|
|
output(key)
|
|
output(value)
|
|
return
|
|
print(item)
|
|
|
|
|
|
with open('test.yml') as f:
|
|
data = ''.join(f.read())
|
|
template = Template(data, trim_blocks=True, lstrip_blocks=True)
|
|
raw = template.render()
|
|
formatted = list(yaml.load_all(raw))
|
|
|
|
if len(formatted) > 1:
|
|
meta = formatted[0]
|
|
for doc in formatted[1:]:
|
|
output(parse_item(doc, 1))
|
|
else:
|
|
output(parse_item(formatted, 1))
|