Skip to content

Commit a718645

Browse files
committed
Added autoformat for inline json with cfbs pretty
Ticket: ENT-12736 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent dce5e96 commit a718645

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

scripts/markdown-code-checker.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from cfbs.pretty import pretty_file
12
import markdown_it
23
import os
34
import argparse
@@ -63,7 +64,7 @@ def extract(path, i, language, first_line, last_line):
6364
with open(path, "r") as f:
6465
content = f.read()
6566

66-
code_snippet = "\n".join(content.split("\n")[first_line+1:last_line-1])
67+
code_snippet = "\n".join(content.split("\n")[first_line + 1 : last_line - 1])
6768

6869
with open(f"{path}.snippet-{i}.{language}", "w") as f:
6970
f.write(code_snippet)
@@ -81,8 +82,30 @@ def replace():
8182
pass
8283

8384

84-
def autoformat():
85-
pass
85+
def autoformat(path, i, language, first_line, last_line):
86+
87+
match language:
88+
case "json":
89+
file_name = f"{path}.snippet-{i}.{language}"
90+
91+
try:
92+
pretty_file(file_name)
93+
with open(file_name, "r") as f:
94+
pretty_content = f.read()
95+
except:
96+
print(
97+
f"[error] Couldn't find the file '{file_name}'. Run --extract to extract the inline code."
98+
)
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))
86109

87110

88111
def parse_args():
@@ -171,7 +194,13 @@ def parse_args():
171194
check_syntax()
172195

173196
if args.autoformat and "noautoformat" not in code_block["flags"]:
174-
autoformat()
197+
autoformat(
198+
path,
199+
i + 1,
200+
supported_languages[code_block["language"]],
201+
code_block["first_line"],
202+
code_block["last_line"],
203+
)
175204

176205
if args.replace and "noreplace" not in code_block["flags"]:
177206
replace()

0 commit comments

Comments
 (0)