[go: up one dir, main page]

File: cmark.py

package info (click to toggle)
commonmark 0.9.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 532 kB
  • sloc: python: 4,971; makefile: 8
file content (53 lines) | stat: -rwxr-xr-x 1,430 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
from __future__ import unicode_literals
import argparse
import sys
import commonmark


def main():
    parser = argparse.ArgumentParser(
        description="Process Markdown according to "
        "the CommonMark specification.")
    if sys.version_info < (3, 0):
        reload(sys)  # noqa
        sys.setdefaultencoding('utf-8')
    parser.add_argument(
        'infile',
        nargs="?",
        type=argparse.FileType('r'),
        default=sys.stdin,
        help="Input Markdown file to parse, defaults to STDIN")
    parser.add_argument(
        '-o',
        nargs="?",
        type=argparse.FileType('w'),
        default=sys.stdout,
        help="Output HTML/JSON file, defaults to STDOUT")
    parser.add_argument('-a', action="store_true", help="Print formatted AST")
    parser.add_argument('-aj', action="store_true", help="Output JSON AST")
    args = parser.parse_args()
    parser = commonmark.Parser()
    f = args.infile
    o = args.o
    lines = []
    for line in f:
        lines.append(line)
    data = "".join(lines)
    ast = parser.parse(data)
    if not args.a and not args.aj:
        renderer = commonmark.HtmlRenderer()
        o.write(renderer.render(ast))
        exit()
    if args.a:
        # print ast
        commonmark.dumpAST(ast)
        exit()

    # o.write(ast.to_JSON())
    o.write(commonmark.dumpJSON(ast))
    exit()


if __name__ == '__main__':
    main()