From fe8ab3d36ceb9f730465d49ad82b34841ec7385f Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 14 Mar 2017 18:38:54 +0000 Subject: [PATCH] Upload MVP --- build.sh | 0 requirements.txt | 3 +++ test.yml | 20 ++++++++++++++++++++ ymd/main.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) mode change 100644 => 100755 build.sh create mode 100644 test.yml create mode 100644 ymd/main.py diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 diff --git a/requirements.txt b/requirements.txt index e69de29..449a103 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,3 @@ +Jinja2==2.9.5 +Markdown==2.6.8 +PyYAML==3.12 diff --git a/test.yml b/test.yml new file mode 100644 index 0000000..0c2bdcf --- /dev/null +++ b/test.yml @@ -0,0 +1,20 @@ +--- +foo: bar +--- +foo: bar +{% for i in range(5) %} +bar{{ i }}: baz +{% endfor %} +test: | + test1 + + {% for i in range(10) %} + - test2 + {% endfor %} +food: + test: + - test 2 + test 2: + - test +--- +bar: biz diff --git a/ymd/main.py b/ymd/main.py new file mode 100644 index 0000000..d200ab2 --- /dev/null +++ b/ymd/main.py @@ -0,0 +1,46 @@ +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 { + "{1}".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))