Skip to content

Commit 2dc0483

Browse files
committed
Changed variable names and parameters
Ticket: ENT-12737 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent e426651 commit 2dc0483

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

scripts/markdown-code-checker.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import subprocess
88

99

10-
def extract_inline_code(file_path, languages):
10+
def extract_inline_code(path, languages):
1111
"""extract inline code, language and filters from markdown"""
1212

13-
with open(file_path, "r") as f:
13+
with open(path, "r") as f:
1414
content = f.read()
1515

1616
md = markdown_it.MarkdownIt("commonmark")
@@ -64,81 +64,78 @@ def get_markdown_files(start, languages):
6464
return return_dict
6565

6666

67-
def extract(path, i, language, first_line, last_line):
67+
def extract(origin_path, snippet_path, _language, first_line, last_line):
6868

69-
with open(path, "r") as f:
69+
with open(origin_path, "r") as f:
7070
content = f.read()
7171

7272
code_snippet = "\n".join(content.split("\n")[first_line + 1 : last_line - 1])
7373

74-
with open(f"{path}.snippet-{i}.{language}", "w") as f:
74+
with open(snippet_path, "w") as f:
7575
f.write(code_snippet)
7676

7777

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

82-
if not os.path.exists(file_name):
81+
if not os.path.exists(snippet_path):
8382
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."
8584
)
8685
return
8786

8887
match language:
8988
case "cf":
9089
p = subprocess.run(
91-
["/var/cfengine/bin/cf-promises", abs_file_name],
90+
["/var/cfengine/bin/cf-promises", snippet_abs_path],
9291
capture_output=True,
9392
text=True,
9493
)
9594
err = p.stderr
9695

9796
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}")
9998
print(err)
10099

101100

102101
def check_output():
103102
pass
104103

105104

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):
108106

109107
try:
110-
with open(file_name, "r") as f:
108+
with open(snippet_path, "r") as f:
111109
pretty_content = f.read()
112110
except:
113111
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."
115113
)
116114
return
117115

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")
120118
pretty_lines = pretty_content.split("\n")
121119

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])
123121

124-
lines[first_line + 1 : last_line - 1] = pretty_lines
122+
origin_lines[first_line + 1 : last_line - 1] = pretty_lines
125123

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

129127
return offset
130128

131129

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):
134131

135132
match language:
136133
case "json":
137134
try:
138-
pretty_file(file_name)
135+
pretty_file(snippet_path)
139136
except:
140137
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."
142139
)
143140

144141

@@ -220,38 +217,43 @@ def parse_args():
220217

221218
parsed_markdowns = get_markdown_files(args.path, args.languages)
222219

223-
for path in parsed_markdowns["files"].keys():
220+
for origin_path in parsed_markdowns["files"].keys():
224221
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+
):
226225

227226
# 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:]:
229228
cb["first_line"] += offset
230229
cb["last_line"] += offset
231230

231+
language = supported_languages[code_block["language"]]
232+
snippet_path = f"{origin_path}.snippet-{i+1}.{language}"
233+
232234
if args.extract and "noextract" not in code_block["flags"]:
233235
extract(
234-
path,
235-
i + 1,
236-
supported_languages[code_block["language"]],
236+
origin_path,
237+
snippet_path,
238+
language,
237239
code_block["first_line"],
238240
code_block["last_line"],
239241
)
240242

241243
if args.syntax_check and "novalidate" not in code_block["flags"]:
242244
check_syntax(
243-
path,
244-
i + 1,
245-
supported_languages[code_block["language"]],
245+
origin_path,
246+
snippet_path,
247+
language,
246248
code_block["first_line"],
247249
code_block["last_line"],
248250
)
249251

250252
if args.autoformat and "noautoformat" not in code_block["flags"]:
251253
autoformat(
252-
path,
253-
i + 1,
254-
supported_languages[code_block["language"]],
254+
origin_path,
255+
snippet_path,
256+
language,
255257
code_block["first_line"],
256258
code_block["last_line"],
257259
)
@@ -261,9 +263,9 @@ def parse_args():
261263

262264
if args.replace and "noreplace" not in code_block["flags"]:
263265
offset = replace(
264-
path,
265-
i + 1,
266-
supported_languages[code_block["language"]],
266+
origin_path,
267+
snippet_path,
268+
language,
267269
code_block["first_line"],
268270
code_block["last_line"],
269271
)

0 commit comments

Comments
 (0)