@@ -78,34 +78,39 @@ def check_output():
78
78
pass
79
79
80
80
81
- def replace ():
82
- pass
81
+ def replace (path , i , language , first_line , last_line ):
82
+ file_name = f"{ path } .snippet-{ i } .{ language } "
83
+
84
+ try :
85
+ with open (file_name , "r" ) as f :
86
+ pretty_content = f .read ()
87
+ except :
88
+ print (
89
+ f"[error] Couldn't find the file '{ file_name } '. Run --extract to extract the inline code."
90
+ )
91
+ return
92
+
93
+ with open (path , "r" ) as f :
94
+ markdown_content = f .read ()
95
+ lines = markdown_content .split ("\n " )
96
+
97
+ lines [first_line + 1 : last_line - 1 ] = pretty_content .split ("\n " )
98
+
99
+ with open (path , "w" ) as f :
100
+ f .write ("\n " .join (lines ))
83
101
84
102
85
103
def autoformat (path , i , language , first_line , last_line ):
104
+ file_name = f"{ path } .snippet-{ i } .{ language } "
86
105
87
106
match language :
88
107
case "json" :
89
- file_name = f"{ path } .snippet-{ i } .{ language } "
90
-
91
108
try :
92
109
pretty_file (file_name )
93
- with open (file_name , "r" ) as f :
94
- pretty_content = f .read ()
95
110
except :
96
111
print (
97
112
f"[error] Couldn't find the file '{ file_name } '. Run --extract to extract the inline code."
98
113
)
99
- return
100
-
101
- with open (path , "r" ) as f :
102
- origin_content = f .read ()
103
-
104
- lines = origin_content .split ("\n " )
105
- lines [first_line + 1 : last_line - 1 ] = pretty_content .split ("\n " )
106
-
107
- with open (path , "w" ) as f :
108
- f .write ("\n " .join (lines ))
109
114
110
115
111
116
def parse_args ():
@@ -202,8 +207,14 @@ def parse_args():
202
207
code_block ["last_line" ],
203
208
)
204
209
205
- if args .replace and "noreplace" not in code_block ["flags" ]:
206
- replace ()
207
-
208
210
if args .output_check and "noexecute" not in code_block ["flags" ]:
209
211
check_output ()
212
+
213
+ if args .replace and "noreplace" not in code_block ["flags" ]:
214
+ replace (
215
+ path ,
216
+ i + 1 ,
217
+ supported_languages [code_block ["language" ]],
218
+ code_block ["first_line" ],
219
+ code_block ["last_line" ],
220
+ )
0 commit comments