Skip to content

Commit dce5e96

Browse files
committed
Changed markdown parsed dict and parsing library
Ticket: ENT-12736 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent dd22b8c commit dce5e96

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

scripts/markdown-code-checker.py

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import marko as md
1+
import markdown_it
22
import os
33
import argparse
44
import sys
@@ -10,20 +10,25 @@ def extract_inline_code(file_path, languages):
1010
with open(file_path, "r") as f:
1111
content = f.read()
1212

13-
parser = md.parser.Parser()
14-
ast = parser.parse(content)
13+
md = markdown_it.MarkdownIt("commonmark")
14+
ast = md.parse(content)
1515

16-
for child in ast.children:
16+
for child in ast:
1717

18-
if not isinstance(child, md.block.FencedCode):
18+
if child.type != "fence":
1919
continue
2020

21-
info_string = child.lang.split("|")
21+
info_string = child.info.split()
2222
language = info_string[0]
2323
flags = info_string[1:]
2424

2525
if language in languages:
26-
yield language, child.children[0].children, flags
26+
yield {
27+
"language": language,
28+
"flags": flags,
29+
"first_line": child.map[0],
30+
"last_line": child.map[1],
31+
}
2732

2833

2934
ignored_dirs = [".git"]
@@ -33,21 +38,33 @@ def get_markdown_files(start, languages):
3338
"""locate all markdown files and call extract_inline_code on them"""
3439

3540
if os.path.isfile(start):
36-
return {start: list(extract_inline_code(start, languages))}
41+
return {
42+
"files": {
43+
start: {"code-blocks": list(extract_inline_code(start, languages))}
44+
}
45+
}
3746

38-
return_dict = {}
47+
return_dict = {"files": {}}
3948
for root, dirs, files in os.walk(start):
4049
dirs[:] = [d for d in dirs if d not in ignored_dirs]
4150

4251
for f in files:
4352
if f.endswith(".markdown") or f.endswith(".md"):
4453
path = os.path.join(root, f)
45-
return_dict[path] = list(extract_inline_code(path, languages))
54+
return_dict["files"][path] = {
55+
"code-blocks": list(extract_inline_code(path, languages))
56+
}
4657

4758
return return_dict
4859

4960

50-
def extract(path, i, language, code_snippet):
61+
def extract(path, i, language, first_line, last_line):
62+
63+
with open(path, "r") as f:
64+
content = f.read()
65+
66+
code_snippet = "\n".join(content.split("\n")[first_line+1:last_line-1])
67+
5168
with open(f"{path}.snippet-{i}.{language}", "w") as f:
5269
f.write(code_snippet)
5370

@@ -74,11 +91,10 @@ def parse_args():
7491
description="Tool for checking the syntax, the format and the output of markdown inline code",
7592
)
7693
parser.add_argument(
77-
"--path",
78-
"-p",
94+
"path",
7995
help="path of file or directory to check syntax on",
96+
nargs="?",
8097
default=".",
81-
required=False,
8298
)
8399
parser.add_argument(
84100
"--languages",
@@ -137,22 +153,28 @@ def parse_args():
137153
)
138154
sys.exit(-1)
139155

140-
parsed_markdown = get_markdown_files(args.path, args.languages)
156+
parsed_markdowns = get_markdown_files(args.path, args.languages)
141157

142-
for path, inline_code_list in parsed_markdown.items():
143-
for i, (language, code_snippet, flags) in enumerate(inline_code_list):
158+
for path in parsed_markdowns["files"].keys():
159+
for i, code_block in enumerate(parsed_markdowns["files"][path]["code-blocks"]):
144160

145-
if args.extract and "noextract" not in flags:
146-
extract(path, i + 1, supported_languages[language], code_snippet)
161+
if args.extract and "noextract" not in code_block["flags"]:
162+
extract(
163+
path,
164+
i + 1,
165+
supported_languages[code_block["language"]],
166+
code_block["first_line"],
167+
code_block["last_line"],
168+
)
147169

148-
if args.syntax_check and "novalidate" not in flags:
170+
if args.syntax_check and "novalidate" not in code_block["flags"]:
149171
check_syntax()
150172

151-
if args.autoformat and "noautoformat" not in flags:
173+
if args.autoformat and "noautoformat" not in code_block["flags"]:
152174
autoformat()
153175

154-
if args.replace and "noreplace" not in flags:
176+
if args.replace and "noreplace" not in code_block["flags"]:
155177
replace()
156178

157-
if args.output_check and "noexecute" not in flags:
179+
if args.output_check and "noexecute" not in code_block["flags"]:
158180
check_output()

0 commit comments

Comments
 (0)