Skip to content

Commit 25b53fa

Browse files
committed
Split autoformat into autoformat and replace
Ticket: ENT-12737 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 2b582d5 commit 25b53fa

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

scripts/markdown-code-checker.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,34 +78,39 @@ def check_output():
7878
pass
7979

8080

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))
83101

84102

85103
def autoformat(path, i, language, first_line, last_line):
104+
file_name = f"{path}.snippet-{i}.{language}"
86105

87106
match language:
88107
case "json":
89-
file_name = f"{path}.snippet-{i}.{language}"
90-
91108
try:
92109
pretty_file(file_name)
93-
with open(file_name, "r") as f:
94-
pretty_content = f.read()
95110
except:
96111
print(
97112
f"[error] Couldn't find the file '{file_name}'. Run --extract to extract the inline code."
98113
)
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))
109114

110115

111116
def parse_args():
@@ -202,8 +207,14 @@ def parse_args():
202207
code_block["last_line"],
203208
)
204209

205-
if args.replace and "noreplace" not in code_block["flags"]:
206-
replace()
207-
208210
if args.output_check and "noexecute" not in code_block["flags"]:
209211
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

Comments
 (0)