Skip to content

Commit 72801ce

Browse files
authored
Merge pull request #3421 from victormlg/cf-syntax-checker-2
ENT-12735 Added a way to exclude / skip specific code snippets to markdown-code-checker
2 parents f877eec + 8e03c49 commit 72801ce

File tree

2 files changed

+158
-90
lines changed

2 files changed

+158
-90
lines changed

markdown-code-checker.py

Lines changed: 0 additions & 90 deletions
This file was deleted.

scripts/markdown-code-checker.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import marko as md
2+
import os
3+
import argparse
4+
import sys
5+
6+
7+
def extract_inline_code(file_path, languages):
8+
"""extract inline code, language and filters from markdown"""
9+
10+
with open(file_path, "r") as f:
11+
content = f.read()
12+
13+
parser = md.parser.Parser()
14+
ast = parser.parse(content)
15+
16+
for child in ast.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
27+
28+
29+
ignored_dirs = [".git"]
30+
31+
32+
def get_markdown_files(start, languages):
33+
"""locate all markdown files and call extract_inline_code on them"""
34+
35+
if os.path.isfile(start):
36+
return {start: list(extract_inline_code(start, languages))}
37+
38+
return_dict = {}
39+
for root, dirs, files in os.walk(start):
40+
dirs[:] = [d for d in dirs if d not in ignored_dirs]
41+
42+
for f in files:
43+
if f.endswith(".markdown") or f.endswith(".md"):
44+
path = os.path.join(root, 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:
52+
f.write(code_snippet)
53+
54+
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+
71+
def parse_args():
72+
parser = argparse.ArgumentParser(
73+
prog="Markdown inline code checker",
74+
description="Tool for checking the syntax, the format and the output of markdown inline code",
75+
)
76+
parser.add_argument(
77+
"--path",
78+
"-p",
79+
help="path of file or directory to check syntax on",
80+
default=".",
81+
required=False,
82+
)
83+
parser.add_argument(
84+
"--languages",
85+
"-l",
86+
nargs="+",
87+
help="languages to check syntax of",
88+
default=["cf3", "json", "yaml"],
89+
required=False,
90+
)
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+
)
121+
122+
return parser.parse_args()
123+
124+
125+
if __name__ == "__main__":
126+
supported_languages = {"cf3": "cf", "json": "json", "yaml": "yml"}
127+
args = parse_args()
128+
129+
if not os.path.exists(args.path):
130+
print("[error] This path doesn't exist")
131+
sys.exit(-1)
132+
133+
for language in args.languages:
134+
if language not in supported_languages:
135+
print(
136+
f"[error] Unsupported language '{language}'. The supported languages are: {", ".join(supported_languages.keys())}"
137+
)
138+
sys.exit(-1)
139+
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)