summaryrefslogtreecommitdiff
path: root/parse.py
blob: 4db14d4cb56949f976b039aeabef9e5ce507771b (plain)
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
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)