Skip to content

Commit 8e03c49

Browse files
committed
Added a way to exclude / skip specific code snippets to markdown-code-checker
Refactored markdown-code-checker to add flags in the future Ticket: ENT-12735 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent a1e957e commit 8e03c49

File tree

1 file changed

+90
-41
lines changed

1 file changed

+90
-41
lines changed

scripts/markdown-code-checker.py

Lines changed: 90 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,73 @@
55

66

77
def extract_inline_code(file_path, languages):
8-
"""extract inline code, language from markdown"""
8+
"""extract inline code, language and filters from markdown"""
99

1010
with open(file_path, "r") as f:
1111
content = f.read()
1212

1313
parser = md.parser.Parser()
1414
ast = parser.parse(content)
1515

16-
code_snippet_count = 0
1716
for child in ast.children:
18-
# TODO: add a way to exclude a code snippet
19-
if isinstance(child, md.block.FencedCode) and child.lang in languages:
20-
code_snippet_count += 1
21-
yield (code_snippet_count, child.lang, child.children[0].children)
17+
18+
if not isinstance(child, md.block.FencedCode):
19+
continue
20+
21+
info_string = child.lang.split("|")
22+
language = info_string[0]
23+
flags = info_string[1:]
24+
25+
if language in languages:
26+
yield language, child.children[0].children, flags
2227

2328

2429
ignored_dirs = [".git"]
2530

2631

2732
def get_markdown_files(start, languages):
28-
"""locate all markdown files and call check_code_syntax on them"""
33+
"""locate all markdown files and call extract_inline_code on them"""
2934

3035
if os.path.isfile(start):
31-
check_code_syntax(start, languages)
36+
return {start: list(extract_inline_code(start, languages))}
3237

38+
return_dict = {}
3339
for root, dirs, files in os.walk(start):
3440
dirs[:] = [d for d in dirs if d not in ignored_dirs]
3541

3642
for f in files:
3743
if f.endswith(".markdown") or f.endswith(".md"):
3844
path = os.path.join(root, f)
39-
check_code_syntax(path, languages)
40-
41-
42-
def check_code_syntax(path, languages):
43-
"""check syntax of every code snippet of one specific markdown file"""
44-
45-
iter = extract_inline_code(path, languages)
46-
for i, language, code_snippet in iter:
47-
48-
file_name = f"{path}.snippet-{i}"
49-
match language:
50-
case "cf3":
51-
write_file(file_name, "cf", code_snippet)
52-
# TODO: run cf-promises on the file
53-
# maybe also check run cf-agent and check if output is correct
54-
break
55-
case "json":
56-
write_file(file_name, "json", code_snippet)
57-
# TODO: check json syntax and run cfbs pretty
58-
break
59-
case "yaml":
60-
write_file(file_name, "yml", code_snippet)
61-
# TODO: check yaml syntax
62-
break
63-
64-
65-
def write_file(file_name, extension, code_snippet):
66-
with open(f"{file_name}.{extension}", "w") as f:
45+
return_dict[path] = list(extract_inline_code(path, languages))
46+
47+
return return_dict
48+
49+
50+
def extract(path, i, language, code_snippet):
51+
with open(f"{path}.snippet-{i}.{language}", "w") as f:
6752
f.write(code_snippet)
6853

6954

55+
def check_syntax():
56+
pass
57+
58+
59+
def check_output():
60+
pass
61+
62+
63+
def replace():
64+
pass
65+
66+
67+
def autoformat():
68+
pass
69+
70+
7071
def parse_args():
7172
parser = argparse.ArgumentParser(
72-
prog="Markdown inline code syntax checker",
73-
description="checks the syntax of documentation inline code",
73+
prog="Markdown inline code checker",
74+
description="Tool for checking the syntax, the format and the output of markdown inline code",
7475
)
7576
parser.add_argument(
7677
"--path",
@@ -87,12 +88,42 @@ def parse_args():
8788
default=["cf3", "json", "yaml"],
8889
required=False,
8990
)
91+
parser.add_argument(
92+
"--extract",
93+
help="extract the inline code into their own files",
94+
action="store_true",
95+
required=False,
96+
)
97+
parser.add_argument(
98+
"--autoformat",
99+
help="automatically format all inline code",
100+
action="store_true",
101+
required=False,
102+
)
103+
parser.add_argument(
104+
"--syntax-check",
105+
help="check syntax of all inline code",
106+
action="store_true",
107+
required=False,
108+
)
109+
parser.add_argument(
110+
"--replace",
111+
help="replace inline code",
112+
action="store_true",
113+
required=False,
114+
)
115+
parser.add_argument(
116+
"--output-check",
117+
help="check output of all inline code",
118+
action="store_true",
119+
required=False,
120+
)
90121

91122
return parser.parse_args()
92123

93124

94125
if __name__ == "__main__":
95-
supported_languages = ["cf3", "json", "yaml"]
126+
supported_languages = {"cf3": "cf", "json": "json", "yaml": "yml"}
96127
args = parse_args()
97128

98129
if not os.path.exists(args.path):
@@ -102,8 +133,26 @@ def parse_args():
102133
for language in args.languages:
103134
if language not in supported_languages:
104135
print(
105-
f"[error] Unsupported language '{language}'. The supported languages are: {", ".join(supported_languages)}"
136+
f"[error] Unsupported language '{language}'. The supported languages are: {", ".join(supported_languages.keys())}"
106137
)
107138
sys.exit(-1)
108139

109-
get_markdown_files(args.path, args.languages)
140+
parsed_markdown = get_markdown_files(args.path, args.languages)
141+
142+
for path, inline_code_list in parsed_markdown.items():
143+
for i, (language, code_snippet, flags) in enumerate(inline_code_list):
144+
145+
if args.extract and "noextract" not in flags:
146+
extract(path, i + 1, supported_languages[language], code_snippet)
147+
148+
if args.syntax_check and "novalidate" not in flags:
149+
check_syntax()
150+
151+
if args.autoformat and "noautoformat" not in flags:
152+
autoformat()
153+
154+
if args.replace and "noreplace" not in flags:
155+
replace()
156+
157+
if args.output_check and "noexecute" not in flags:
158+
check_output()

0 commit comments

Comments
 (0)