Output to file
This commit is contained in:
parent
fe8ab3d36c
commit
93c0714d47
1 changed files with 11 additions and 11 deletions
22
ymd/main.py
22
ymd/main.py
|
@ -1,6 +1,7 @@
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
import yaml
|
import yaml
|
||||||
import markdown
|
import markdown
|
||||||
|
from io import StringIO
|
||||||
|
|
||||||
|
|
||||||
def parse_item(item, h):
|
def parse_item(item, h):
|
||||||
|
@ -19,17 +20,17 @@ def parse_item(item, h):
|
||||||
return item
|
return item
|
||||||
return markdown.markdown(str(item))
|
return markdown.markdown(str(item))
|
||||||
|
|
||||||
def output(item):
|
def output(item, f):
|
||||||
item_type = type(item)
|
item_type = type(item)
|
||||||
if item_type is list:
|
if item_type is list:
|
||||||
[output(sub_item) for sub_item in item]
|
[output(sub_item, f) for sub_item in item]
|
||||||
return
|
return
|
||||||
if item_type is dict:
|
if item_type is dict:
|
||||||
for key, value in item.items():
|
for key, value in item.items():
|
||||||
output(key)
|
output(key, f)
|
||||||
output(value)
|
output(value, f)
|
||||||
return
|
return
|
||||||
print(item)
|
f.write(str(item))
|
||||||
|
|
||||||
|
|
||||||
with open('test.yml') as f:
|
with open('test.yml') as f:
|
||||||
|
@ -38,9 +39,8 @@ with open('test.yml') as f:
|
||||||
raw = template.render()
|
raw = template.render()
|
||||||
formatted = list(yaml.load_all(raw))
|
formatted = list(yaml.load_all(raw))
|
||||||
|
|
||||||
if len(formatted) > 1:
|
with StringIO() as rendered:
|
||||||
meta = formatted[0]
|
output(parse_item(formatted[1], 1), rendered)
|
||||||
for doc in formatted[1:]:
|
with open('test.html', 'w') as f:
|
||||||
output(parse_item(doc, 1))
|
f.write(rendered.getvalue())
|
||||||
else:
|
|
||||||
output(parse_item(formatted, 1))
|
|
||||||
|
|
Reference in a new issue