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
|
||||
import yaml
|
||||
import markdown
|
||||
from io import StringIO
|
||||
|
||||
|
||||
def parse_item(item, h):
|
||||
|
@ -19,17 +20,17 @@ def parse_item(item, h):
|
|||
return item
|
||||
return markdown.markdown(str(item))
|
||||
|
||||
def output(item):
|
||||
def output(item, f):
|
||||
item_type = type(item)
|
||||
if item_type is list:
|
||||
[output(sub_item) for sub_item in item]
|
||||
[output(sub_item, f) for sub_item in item]
|
||||
return
|
||||
if item_type is dict:
|
||||
for key, value in item.items():
|
||||
output(key)
|
||||
output(value)
|
||||
output(key, f)
|
||||
output(value, f)
|
||||
return
|
||||
print(item)
|
||||
f.write(str(item))
|
||||
|
||||
|
||||
with open('test.yml') as f:
|
||||
|
@ -38,9 +39,8 @@ with open('test.yml') as f:
|
|||
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))
|
||||
with StringIO() as rendered:
|
||||
output(parse_item(formatted[1], 1), rendered)
|
||||
with open('test.html', 'w') as f:
|
||||
f.write(rendered.getvalue())
|
||||
|
||||
|
|
Reference in a new issue