1
- import marko as md
1
+ import markdown_it
2
2
import os
3
3
import argparse
4
4
import sys
@@ -10,20 +10,25 @@ def extract_inline_code(file_path, languages):
10
10
with open (file_path , "r" ) as f :
11
11
content = f .read ()
12
12
13
- parser = md . parser . Parser ( )
14
- ast = parser .parse (content )
13
+ md = markdown_it . MarkdownIt ( "commonmark" )
14
+ ast = md .parse (content )
15
15
16
- for child in ast . children :
16
+ for child in ast :
17
17
18
- if not isinstance ( child , md . block . FencedCode ) :
18
+ if child . type != "fence" :
19
19
continue
20
20
21
- info_string = child .lang .split ("|" )
21
+ info_string = child .info .split ()
22
22
language = info_string [0 ]
23
23
flags = info_string [1 :]
24
24
25
25
if language in languages :
26
- yield language , child .children [0 ].children , flags
26
+ yield {
27
+ "language" : language ,
28
+ "flags" : flags ,
29
+ "first_line" : child .map [0 ],
30
+ "last_line" : child .map [1 ],
31
+ }
27
32
28
33
29
34
ignored_dirs = [".git" ]
@@ -33,21 +38,33 @@ def get_markdown_files(start, languages):
33
38
"""locate all markdown files and call extract_inline_code on them"""
34
39
35
40
if os .path .isfile (start ):
36
- return {start : list (extract_inline_code (start , languages ))}
41
+ return {
42
+ "files" : {
43
+ start : {"code-blocks" : list (extract_inline_code (start , languages ))}
44
+ }
45
+ }
37
46
38
- return_dict = {}
47
+ return_dict = {"files" : {} }
39
48
for root , dirs , files in os .walk (start ):
40
49
dirs [:] = [d for d in dirs if d not in ignored_dirs ]
41
50
42
51
for f in files :
43
52
if f .endswith (".markdown" ) or f .endswith (".md" ):
44
53
path = os .path .join (root , f )
45
- return_dict [path ] = list (extract_inline_code (path , languages ))
54
+ return_dict ["files" ][path ] = {
55
+ "code-blocks" : list (extract_inline_code (path , languages ))
56
+ }
46
57
47
58
return return_dict
48
59
49
60
50
- def extract (path , i , language , code_snippet ):
61
+ def extract (path , i , language , first_line , last_line ):
62
+
63
+ with open (path , "r" ) as f :
64
+ content = f .read ()
65
+
66
+ code_snippet = "\n " .join (content .split ("\n " )[first_line + 1 :last_line - 1 ])
67
+
51
68
with open (f"{ path } .snippet-{ i } .{ language } " , "w" ) as f :
52
69
f .write (code_snippet )
53
70
@@ -74,11 +91,10 @@ def parse_args():
74
91
description = "Tool for checking the syntax, the format and the output of markdown inline code" ,
75
92
)
76
93
parser .add_argument (
77
- "--path" ,
78
- "-p" ,
94
+ "path" ,
79
95
help = "path of file or directory to check syntax on" ,
96
+ nargs = "?" ,
80
97
default = "." ,
81
- required = False ,
82
98
)
83
99
parser .add_argument (
84
100
"--languages" ,
@@ -137,22 +153,28 @@ def parse_args():
137
153
)
138
154
sys .exit (- 1 )
139
155
140
- parsed_markdown = get_markdown_files (args .path , args .languages )
156
+ parsed_markdowns = get_markdown_files (args .path , args .languages )
141
157
142
- for path , inline_code_list in parsed_markdown . items ():
143
- for i , ( language , code_snippet , flags ) in enumerate (inline_code_list ):
158
+ for path in parsed_markdowns [ "files" ]. keys ():
159
+ for i , code_block in enumerate (parsed_markdowns [ "files" ][ path ][ "code-blocks" ] ):
144
160
145
- if args .extract and "noextract" not in flags :
146
- extract (path , i + 1 , supported_languages [language ], code_snippet )
161
+ if args .extract and "noextract" not in code_block ["flags" ]:
162
+ extract (
163
+ path ,
164
+ i + 1 ,
165
+ supported_languages [code_block ["language" ]],
166
+ code_block ["first_line" ],
167
+ code_block ["last_line" ],
168
+ )
147
169
148
- if args .syntax_check and "novalidate" not in flags :
170
+ if args .syntax_check and "novalidate" not in code_block [ " flags" ] :
149
171
check_syntax ()
150
172
151
- if args .autoformat and "noautoformat" not in flags :
173
+ if args .autoformat and "noautoformat" not in code_block [ " flags" ] :
152
174
autoformat ()
153
175
154
- if args .replace and "noreplace" not in flags :
176
+ if args .replace and "noreplace" not in code_block [ " flags" ] :
155
177
replace ()
156
178
157
- if args .output_check and "noexecute" not in flags :
179
+ if args .output_check and "noexecute" not in code_block [ " flags" ] :
158
180
check_output ()
0 commit comments