Skip to content
This repository was archived by the owner on Mar 26, 2022. It is now read-only.

Commit 7e24733

Browse files
committed
Merge pull request #72 from rtfd/cmark-main
Add main() function to cmark.py
2 parents 737ee62 + 5ff539f commit 7e24733

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed

CommonMark/cmark.py

+46-38
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,51 @@
33
import argparse
44
import sys
55
import CommonMark
6-
parser = argparse.ArgumentParser(
7-
description="Process Markdown according to the CommonMark specification.")
8-
if sys.version_info < (3, 0):
9-
reload(sys) # noqa
10-
sys.setdefaultencoding('utf-8')
11-
parser.add_argument(
12-
'infile',
13-
nargs="?",
14-
type=argparse.FileType('r'),
15-
default=sys.stdin,
16-
help="Input Markdown file to parse, defaults to STDIN")
17-
parser.add_argument(
18-
'-o',
19-
nargs="?",
20-
type=argparse.FileType('w'),
21-
default=sys.stdout,
22-
help="Output HTML/JSON file, defaults to STDOUT")
23-
parser.add_argument('-a', action="store_true", help="Print formatted AST")
24-
parser.add_argument('-aj', action="store_true", help="Output JSON AST")
25-
args = parser.parse_args()
26-
parser = CommonMark.Parser()
27-
f = args.infile
28-
o = args.o
29-
lines = []
30-
for line in f:
31-
lines.append(line)
32-
data = "".join(lines)
33-
ast = parser.parse(data)
34-
if not args.a and not args.aj:
35-
renderer = CommonMark.HtmlRenderer()
36-
o.write(renderer.render(ast))
37-
exit()
38-
if args.a:
39-
# print ast
40-
CommonMark.dumpAST(ast)
6+
7+
8+
def main():
9+
parser = argparse.ArgumentParser(
10+
description="Process Markdown according to "
11+
"the CommonMark specification.")
12+
if sys.version_info < (3, 0):
13+
reload(sys) # noqa
14+
sys.setdefaultencoding('utf-8')
15+
parser.add_argument(
16+
'infile',
17+
nargs="?",
18+
type=argparse.FileType('r'),
19+
default=sys.stdin,
20+
help="Input Markdown file to parse, defaults to STDIN")
21+
parser.add_argument(
22+
'-o',
23+
nargs="?",
24+
type=argparse.FileType('w'),
25+
default=sys.stdout,
26+
help="Output HTML/JSON file, defaults to STDOUT")
27+
parser.add_argument('-a', action="store_true", help="Print formatted AST")
28+
parser.add_argument('-aj', action="store_true", help="Output JSON AST")
29+
args = parser.parse_args()
30+
parser = CommonMark.Parser()
31+
f = args.infile
32+
o = args.o
33+
lines = []
34+
for line in f:
35+
lines.append(line)
36+
data = "".join(lines)
37+
ast = parser.parse(data)
38+
if not args.a and not args.aj:
39+
renderer = CommonMark.HtmlRenderer()
40+
o.write(renderer.render(ast))
41+
exit()
42+
if args.a:
43+
# print ast
44+
CommonMark.dumpAST(ast)
45+
exit()
46+
47+
# o.write(ast.to_JSON())
48+
o.write(CommonMark.ASTtoJSON(ast))
4149
exit()
4250

43-
# o.write(ast.to_JSON())
44-
o.write(CommonMark.ASTtoJSON(ast))
45-
exit()
51+
52+
if __name__ == '__main__':
53+
main()

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def run(self):
3333
keywords=["markup", "markdown", "commonmark"],
3434
entry_points={
3535
'console_scripts': [
36-
'cmark = CommonMark.cmark:cmark',
36+
'cmark = CommonMark.cmark:main',
3737
]
3838
},
3939
cmdclass={'test': Test},

0 commit comments

Comments
 (0)