From 869a1b7382bd3c988b8c5618f62145eb4ebcf546 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 25 May 2019 21:07:36 +0300 Subject: Add: helper script --- parse.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 parse.py (limited to 'parse.py') diff --git a/parse.py b/parse.py new file mode 100644 index 0000000..4db14d4 --- /dev/null +++ b/parse.py @@ -0,0 +1,37 @@ +import copy +import sys + +import CommonMark_bkrs as CommonMark + +text = sys.stdin.read() +parser = CommonMark.DocParser() +s = parser.parse(text) + +print(CommonMark.ASTtoJSON(s)) +sys.exit(0) + +def is_code_block(o): + prefix = "```fable\n" + return o.t == 'IndentedCode' and o.string_content.startswith(prefix) + +def is_heading(o): + return o.t =='ATXHeader' + +def walk(o): + if is_code_block(o): + yield { + 'type': 'fable', + 'text': copy.deepcopy(o.strings), + } + elif is_heading(o): + yield { + 'type': 'heading', + 'level': o.level, + 'text': copy.deepcopy(o.strings), + } + for c in o.children: + for oo in walk(c): + yield oo + +for o in walk(s): + print(o) -- cgit v1.2.1