summaryrefslogtreecommitdiff
path: root/parse.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2019-05-25 21:07:36 +0300
committerLars Wirzenius <liw@liw.fi>2019-05-25 21:07:36 +0300
commit869a1b7382bd3c988b8c5618f62145eb4ebcf546 (patch)
tree6309f97b527b0893b1338d6e5250364b730ab863 /parse.py
parentfcff3188b6525a162cab4e88fdc4828c4a29c10b (diff)
downloadfable-poc-869a1b7382bd3c988b8c5618f62145eb4ebcf546.tar.gz
Add: helper script
Diffstat (limited to 'parse.py')
-rw-r--r--parse.py37
1 files changed, 37 insertions, 0 deletions
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)