5
5
6
6
7
7
def extract_inline_code (file_path , languages ):
8
- """extract inline code, language from markdown"""
8
+ """extract inline code, language and filters from markdown"""
9
9
10
10
with open (file_path , "r" ) as f :
11
11
content = f .read ()
12
12
13
13
parser = md .parser .Parser ()
14
14
ast = parser .parse (content )
15
15
16
- code_snippet_count = 0
17
16
for child in ast .children :
18
- # TODO: add a way to exclude a code snippet
19
- if isinstance (child , md .block .FencedCode ) and child .lang in languages :
20
- code_snippet_count += 1
21
- yield (code_snippet_count , child .lang , child .children [0 ].children )
17
+
18
+ if not isinstance (child , md .block .FencedCode ):
19
+ continue
20
+
21
+ info_string = child .lang .split ("|" )
22
+ language = info_string [0 ]
23
+ flags = info_string [1 :]
24
+
25
+ if language in languages :
26
+ yield language , child .children [0 ].children , flags
22
27
23
28
24
29
ignored_dirs = [".git" ]
25
30
26
31
27
32
def get_markdown_files (start , languages ):
28
- """locate all markdown files and call check_code_syntax on them"""
33
+ """locate all markdown files and call extract_inline_code on them"""
29
34
30
35
if os .path .isfile (start ):
31
- check_code_syntax ( start , languages )
36
+ return { start : list ( extract_inline_code ( start , languages ))}
32
37
38
+ return_dict = {}
33
39
for root , dirs , files in os .walk (start ):
34
40
dirs [:] = [d for d in dirs if d not in ignored_dirs ]
35
41
36
42
for f in files :
37
43
if f .endswith (".markdown" ) or f .endswith (".md" ):
38
44
path = os .path .join (root , f )
39
- check_code_syntax (path , languages )
40
-
41
-
42
- def check_code_syntax (path , languages ):
43
- """check syntax of every code snippet of one specific markdown file"""
44
-
45
- iter = extract_inline_code (path , languages )
46
- for i , language , code_snippet in iter :
47
-
48
- file_name = f"{ path } .snippet-{ i } "
49
- match language :
50
- case "cf3" :
51
- write_file (file_name , "cf" , code_snippet )
52
- # TODO: run cf-promises on the file
53
- # maybe also check run cf-agent and check if output is correct
54
- break
55
- case "json" :
56
- write_file (file_name , "json" , code_snippet )
57
- # TODO: check json syntax and run cfbs pretty
58
- break
59
- case "yaml" :
60
- write_file (file_name , "yml" , code_snippet )
61
- # TODO: check yaml syntax
62
- break
63
-
64
-
65
- def write_file (file_name , extension , code_snippet ):
66
- with open (f"{ file_name } .{ extension } " , "w" ) as f :
45
+ return_dict [path ] = list (extract_inline_code (path , languages ))
46
+
47
+ return return_dict
48
+
49
+
50
+ def extract (path , i , language , code_snippet ):
51
+ with open (f"{ path } .snippet-{ i } .{ language } " , "w" ) as f :
67
52
f .write (code_snippet )
68
53
69
54
55
+ def check_syntax ():
56
+ pass
57
+
58
+
59
+ def check_output ():
60
+ pass
61
+
62
+
63
+ def replace ():
64
+ pass
65
+
66
+
67
+ def autoformat ():
68
+ pass
69
+
70
+
70
71
def parse_args ():
71
72
parser = argparse .ArgumentParser (
72
- prog = "Markdown inline code syntax checker" ,
73
- description = "checks the syntax of documentation inline code" ,
73
+ prog = "Markdown inline code checker" ,
74
+ description = "Tool for checking the syntax, the format and the output of markdown inline code" ,
74
75
)
75
76
parser .add_argument (
76
77
"--path" ,
@@ -87,12 +88,42 @@ def parse_args():
87
88
default = ["cf3" , "json" , "yaml" ],
88
89
required = False ,
89
90
)
91
+ parser .add_argument (
92
+ "--extract" ,
93
+ help = "extract the inline code into their own files" ,
94
+ action = "store_true" ,
95
+ required = False ,
96
+ )
97
+ parser .add_argument (
98
+ "--autoformat" ,
99
+ help = "automatically format all inline code" ,
100
+ action = "store_true" ,
101
+ required = False ,
102
+ )
103
+ parser .add_argument (
104
+ "--syntax-check" ,
105
+ help = "check syntax of all inline code" ,
106
+ action = "store_true" ,
107
+ required = False ,
108
+ )
109
+ parser .add_argument (
110
+ "--replace" ,
111
+ help = "replace inline code" ,
112
+ action = "store_true" ,
113
+ required = False ,
114
+ )
115
+ parser .add_argument (
116
+ "--output-check" ,
117
+ help = "check output of all inline code" ,
118
+ action = "store_true" ,
119
+ required = False ,
120
+ )
90
121
91
122
return parser .parse_args ()
92
123
93
124
94
125
if __name__ == "__main__" :
95
- supported_languages = [ "cf3" , "json" , "yaml" ]
126
+ supported_languages = { "cf3" : "cf" , "json" : "json" , "yaml" : "yml" }
96
127
args = parse_args ()
97
128
98
129
if not os .path .exists (args .path ):
@@ -102,8 +133,26 @@ def parse_args():
102
133
for language in args .languages :
103
134
if language not in supported_languages :
104
135
print (
105
- f"[error] Unsupported language '{ language } '. The supported languages are: { ", " .join (supported_languages )} "
136
+ f"[error] Unsupported language '{ language } '. The supported languages are: { ", " .join (supported_languages . keys () )} "
106
137
)
107
138
sys .exit (- 1 )
108
139
109
- get_markdown_files (args .path , args .languages )
140
+ parsed_markdown = get_markdown_files (args .path , args .languages )
141
+
142
+ for path , inline_code_list in parsed_markdown .items ():
143
+ for i , (language , code_snippet , flags ) in enumerate (inline_code_list ):
144
+
145
+ if args .extract and "noextract" not in flags :
146
+ extract (path , i + 1 , supported_languages [language ], code_snippet )
147
+
148
+ if args .syntax_check and "novalidate" not in flags :
149
+ check_syntax ()
150
+
151
+ if args .autoformat and "noautoformat" not in flags :
152
+ autoformat ()
153
+
154
+ if args .replace and "noreplace" not in flags :
155
+ replace ()
156
+
157
+ if args .output_check and "noexecute" not in flags :
158
+ check_output ()
0 commit comments