7
7
import subprocess
8
8
9
9
10
- def extract_inline_code (file_path , languages ):
10
+ def extract_inline_code (path , languages ):
11
11
"""extract inline code, language and filters from markdown"""
12
12
13
- with open (file_path , "r" ) as f :
13
+ with open (path , "r" ) as f :
14
14
content = f .read ()
15
15
16
16
md = markdown_it .MarkdownIt ("commonmark" )
@@ -64,81 +64,78 @@ def get_markdown_files(start, languages):
64
64
return return_dict
65
65
66
66
67
- def extract (path , i , language , first_line , last_line ):
67
+ def extract (origin_path , snippet_path , _language , first_line , last_line ):
68
68
69
- with open (path , "r" ) as f :
69
+ with open (origin_path , "r" ) as f :
70
70
content = f .read ()
71
71
72
72
code_snippet = "\n " .join (content .split ("\n " )[first_line + 1 : last_line - 1 ])
73
73
74
- with open (f" { path } .snippet- { i } . { language } " , "w" ) as f :
74
+ with open (snippet_path , "w" ) as f :
75
75
f .write (code_snippet )
76
76
77
77
78
- def check_syntax (path , i , language , first_line , last_line ):
79
- file_name = f"{ path } .snippet-{ i } .{ language } "
80
- abs_file_name = os .path .abspath (file_name )
78
+ def check_syntax (origin_path , snippet_path , language , first_line , _last_line ):
79
+ snippet_abs_path = os .path .abspath (snippet_path )
81
80
82
- if not os .path .exists (file_name ):
81
+ if not os .path .exists (snippet_path ):
83
82
print (
84
- f"[error] Couldn't find the file '{ file_name } '. Run --extract to extract the inline code."
83
+ f"[error] Couldn't find the file '{ snippet_path } '. Run --extract to extract the inline code."
85
84
)
86
85
return
87
86
88
87
match language :
89
88
case "cf" :
90
89
p = subprocess .run (
91
- ["/var/cfengine/bin/cf-promises" , abs_file_name ],
90
+ ["/var/cfengine/bin/cf-promises" , snippet_abs_path ],
92
91
capture_output = True ,
93
92
text = True ,
94
93
)
95
94
err = p .stderr
96
95
97
96
if err :
98
- err = err .replace (abs_file_name , f"{ path } :{ first_line } " )
97
+ err = err .replace (snippet_abs_path , f"{ origin_path } :{ first_line } " )
99
98
print (err )
100
99
101
100
102
101
def check_output ():
103
102
pass
104
103
105
104
106
- def replace (path , i , language , first_line , last_line ):
107
- file_name = f"{ path } .snippet-{ i } .{ language } "
105
+ def replace (origin_path , snippet_path , _language , first_line , last_line ):
108
106
109
107
try :
110
- with open (file_name , "r" ) as f :
108
+ with open (snippet_path , "r" ) as f :
111
109
pretty_content = f .read ()
112
110
except :
113
111
print (
114
- f"[error] Couldn't find the file '{ file_name } '. Run --extract to extract the inline code."
112
+ f"[error] Couldn't find the file '{ snippet_path } '. Run --extract to extract the inline code."
115
113
)
116
114
return
117
115
118
- with open (path , "r" ) as f :
119
- lines = f .read ().split ("\n " )
116
+ with open (origin_path , "r" ) as f :
117
+ origin_lines = f .read ().split ("\n " )
120
118
pretty_lines = pretty_content .split ("\n " )
121
119
122
- offset = len (pretty_lines ) - len (lines [first_line + 1 : last_line - 1 ])
120
+ offset = len (pretty_lines ) - len (origin_lines [first_line + 1 : last_line - 1 ])
123
121
124
- lines [first_line + 1 : last_line - 1 ] = pretty_lines
122
+ origin_lines [first_line + 1 : last_line - 1 ] = pretty_lines
125
123
126
- with open (path , "w" ) as f :
127
- f .write ("\n " .join (lines ))
124
+ with open (origin_path , "w" ) as f :
125
+ f .write ("\n " .join (origin_lines ))
128
126
129
127
return offset
130
128
131
129
132
- def autoformat (path , i , language , first_line , last_line ):
133
- file_name = f"{ path } .snippet-{ i } .{ language } "
130
+ def autoformat (_origin_path , snippet_path , language , _first_line , _last_line ):
134
131
135
132
match language :
136
133
case "json" :
137
134
try :
138
- pretty_file (file_name )
135
+ pretty_file (snippet_path )
139
136
except :
140
137
print (
141
- f"[error] Couldn't find the file '{ file_name } '. Run --extract to extract the inline code."
138
+ f"[error] Couldn't find the file '{ snippet_path } '. Run --extract to extract the inline code."
142
139
)
143
140
144
141
@@ -220,38 +217,43 @@ def parse_args():
220
217
221
218
parsed_markdowns = get_markdown_files (args .path , args .languages )
222
219
223
- for path in parsed_markdowns ["files" ].keys ():
220
+ for origin_path in parsed_markdowns ["files" ].keys ():
224
221
offset = 0
225
- for i , code_block in enumerate (parsed_markdowns ["files" ][path ]["code-blocks" ]):
222
+ for i , code_block in enumerate (
223
+ parsed_markdowns ["files" ][origin_path ]["code-blocks" ]
224
+ ):
226
225
227
226
# adjust line numbers after replace
228
- for cb in parsed_markdowns ["files" ][path ]["code-blocks" ][i :]:
227
+ for cb in parsed_markdowns ["files" ][origin_path ]["code-blocks" ][i :]:
229
228
cb ["first_line" ] += offset
230
229
cb ["last_line" ] += offset
231
230
231
+ language = supported_languages [code_block ["language" ]]
232
+ snippet_path = f"{ origin_path } .snippet-{ i + 1 } .{ language } "
233
+
232
234
if args .extract and "noextract" not in code_block ["flags" ]:
233
235
extract (
234
- path ,
235
- i + 1 ,
236
- supported_languages [ code_block [ " language" ]] ,
236
+ origin_path ,
237
+ snippet_path ,
238
+ language ,
237
239
code_block ["first_line" ],
238
240
code_block ["last_line" ],
239
241
)
240
242
241
243
if args .syntax_check and "novalidate" not in code_block ["flags" ]:
242
244
check_syntax (
243
- path ,
244
- i + 1 ,
245
- supported_languages [ code_block [ " language" ]] ,
245
+ origin_path ,
246
+ snippet_path ,
247
+ language ,
246
248
code_block ["first_line" ],
247
249
code_block ["last_line" ],
248
250
)
249
251
250
252
if args .autoformat and "noautoformat" not in code_block ["flags" ]:
251
253
autoformat (
252
- path ,
253
- i + 1 ,
254
- supported_languages [ code_block [ " language" ]] ,
254
+ origin_path ,
255
+ snippet_path ,
256
+ language ,
255
257
code_block ["first_line" ],
256
258
code_block ["last_line" ],
257
259
)
@@ -261,9 +263,9 @@ def parse_args():
261
263
262
264
if args .replace and "noreplace" not in code_block ["flags" ]:
263
265
offset = replace (
264
- path ,
265
- i + 1 ,
266
- supported_languages [ code_block [ " language" ]] ,
266
+ origin_path ,
267
+ snippet_path ,
268
+ language ,
267
269
code_block ["first_line" ],
268
270
code_block ["last_line" ],
269
271
)
0 commit comments