-
Notifications
You must be signed in to change notification settings - Fork 4
/
yamldown.py
53 lines (35 loc) · 1.54 KB
/
yamldown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import IO, Tuple, Dict, List
import yaml
class Buffer(object):
def __init__(self) -> None:
self.contents = ""
def append(self, contents: str) -> None:
self.contents = "{existing}{end}".format(existing=self.contents, end=contents)
def empty(self) -> bool:
return len(self.contents) == 0
def _load(stream: IO[str]) -> Tuple[Dict, str]:
yml_contents = Buffer()
md_contents = Buffer()
current_buffer = md_contents
for line in stream:
if _is_yaml_start(line, yml_contents):
current_buffer = yml_contents
elif _is_yaml_end(line, yml_contents):
current_buffer = md_contents
# Skip the ending `---` and move on to the next line as markdown
continue
current_buffer.append(line)
yml_dict = yaml.load(yml_contents.contents, Loader=yaml.FullLoader) # type: Dict
return (yml_dict, md_contents.contents.strip("\n"))
def _is_yaml_start(line: str, yml_buffer: Buffer) -> bool:
return line.strip("\n").endswith("---") and yml_buffer.empty()
def _is_yaml_end(line: str, yml_buffer: Buffer) -> bool:
return line.strip("\n").endswith("---") and not yml_buffer.empty()
def _dump(yml: Dict, markdown: str, yamlfirst=True) -> str:
yamlout = yaml.dump(yml, default_flow_style=False, indent=2) # type: str
dump = ""
if yamlfirst:
dump = "---\n{yml}\n---\n{markdown}".format(yml=yamlout, markdown=markdown)
else:
dump = "{markdown}\n---\n{yml}\n---".format(markdown=markdown, yml=yamlout)
return dump