3
3
import argparse
4
4
import sys
5
5
6
+
6
7
def extract_inline_code (file_path , languages ):
7
8
"""extract inline code, language from markdown"""
8
-
9
+
9
10
with open (file_path , "r" ) as f :
10
11
content = f .read ()
11
12
@@ -16,22 +17,24 @@ def extract_inline_code(file_path, languages):
16
17
for child in ast .children :
17
18
# TODO: add a way to exclude a code snippet
18
19
if isinstance (child , md .block .FencedCode ) and child .lang in languages :
19
- code_snippet_count += 1
20
+ code_snippet_count += 1
20
21
yield (code_snippet_count , child .lang , child .children [0 ].children )
21
22
23
+
22
24
ignored_dirs = [".git" ]
23
25
26
+
24
27
def get_markdown_files (start , languages ):
25
28
"""locate all markdown files and call check_code_syntax on them"""
26
29
27
30
if os .path .isfile (start ):
28
31
check_code_syntax (start , languages )
29
32
30
33
for root , dirs , files in os .walk (start ):
31
- dirs [:] = [d for d in dirs if d not in ignored_dirs ]
34
+ dirs [:] = [d for d in dirs if d not in ignored_dirs ]
32
35
33
36
for f in files :
34
- if f .endswith (' .markdown' ) or f .endswith (' .md' ):
37
+ if f .endswith (" .markdown" ) or f .endswith (" .md" ):
35
38
path = os .path .join (root , f )
36
39
check_code_syntax (path , languages )
37
40
@@ -60,17 +63,31 @@ def check_code_syntax(path, languages):
60
63
61
64
62
65
def write_file (file_name , extension , code_snippet ):
63
- with open (f"{ file_name } .{ extension } " , 'w' ) as f :
66
+ with open (f"{ file_name } .{ extension } " , "w" ) as f :
64
67
f .write (code_snippet )
65
68
66
69
67
70
def parse_args ():
68
- parser = argparse .ArgumentParser (prog = "Markdown inline code syntax checker" ,
69
- description = "checks the syntax of documentation inline code"
70
- )
71
- parser .add_argument ("--path" , "-p" , help = "path of file or directory to check syntax on" , default = "." , required = False )
72
- parser .add_argument ("--languages" , "-l" , nargs = '+' , help = "languages to check syntax of" , default = ["cf3" , "json" , "yaml" ], required = False )
73
-
71
+ parser = argparse .ArgumentParser (
72
+ prog = "Markdown inline code syntax checker" ,
73
+ description = "checks the syntax of documentation inline code" ,
74
+ )
75
+ parser .add_argument (
76
+ "--path" ,
77
+ "-p" ,
78
+ help = "path of file or directory to check syntax on" ,
79
+ default = "." ,
80
+ required = False ,
81
+ )
82
+ parser .add_argument (
83
+ "--languages" ,
84
+ "-l" ,
85
+ nargs = "+" ,
86
+ help = "languages to check syntax of" ,
87
+ default = ["cf3" , "json" , "yaml" ],
88
+ required = False ,
89
+ )
90
+
74
91
return parser .parse_args ()
75
92
76
93
@@ -84,7 +101,9 @@ def parse_args():
84
101
85
102
for language in args .languages :
86
103
if language not in supported_languages :
87
- print (f"[error] Unsupported language '{ language } '. The supported languages are: { ", " .join (supported_languages )} " )
104
+ print (
105
+ f"[error] Unsupported language '{ language } '. The supported languages are: { ", " .join (supported_languages )} "
106
+ )
88
107
sys .exit (- 1 )
89
108
90
109
get_markdown_files (args .path , args .languages )
0 commit comments