From 93c0714d473eca8fa934ed53e01abf6c09143e7a Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 14 Mar 2017 19:11:11 +0000 Subject: [PATCH] Output to file --- ymd/main.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ymd/main.py b/ymd/main.py index d200ab2..58252d9 100644 --- a/ymd/main.py +++ b/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()) +