|
1 | 1 | from cfbs.pretty import pretty_file
|
| 2 | +from shutil import which |
2 | 3 | import markdown_it
|
3 | 4 | import os
|
4 | 5 | import argparse
|
5 | 6 | import sys
|
| 7 | +import subprocess |
6 | 8 |
|
7 | 9 |
|
8 | 10 | def extract_inline_code(file_path, languages):
|
@@ -73,8 +75,28 @@ def extract(path, i, language, first_line, last_line):
|
73 | 75 | f.write(code_snippet)
|
74 | 76 |
|
75 | 77 |
|
76 |
| -def check_syntax(): |
77 |
| - pass |
| 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) |
| 81 | + |
| 82 | + if not os.path.exists(file_name): |
| 83 | + print( |
| 84 | + f"[error] Couldn't find the file '{file_name}'. Run --extract to extract the inline code." |
| 85 | + ) |
| 86 | + return |
| 87 | + |
| 88 | + match language: |
| 89 | + case "cf": |
| 90 | + p = subprocess.run( |
| 91 | + ["/var/cfengine/bin/cf-promises", abs_file_name], |
| 92 | + capture_output=True, |
| 93 | + text=True, |
| 94 | + ) |
| 95 | + err = p.stderr |
| 96 | + |
| 97 | + if err: |
| 98 | + err = err.replace(abs_file_name, f"{path}:{first_line}") |
| 99 | + print(err) |
78 | 100 |
|
79 | 101 |
|
80 | 102 | def check_output():
|
@@ -181,6 +203,14 @@ def parse_args():
|
181 | 203 | print("[error] This path doesn't exist")
|
182 | 204 | sys.exit(-1)
|
183 | 205 |
|
| 206 | + if ( |
| 207 | + args.syntax_check |
| 208 | + and "cf3" in args.languages |
| 209 | + and not which("/var/cfengine/bin/cf-promises") |
| 210 | + ): |
| 211 | + print("[error] cf-promises is not installed") |
| 212 | + sys.exit(-1) |
| 213 | + |
184 | 214 | for language in args.languages:
|
185 | 215 | if language not in supported_languages:
|
186 | 216 | print(
|
@@ -209,7 +239,13 @@ def parse_args():
|
209 | 239 | )
|
210 | 240 |
|
211 | 241 | if args.syntax_check and "novalidate" not in code_block["flags"]:
|
212 |
| - check_syntax() |
| 242 | + check_syntax( |
| 243 | + path, |
| 244 | + i + 1, |
| 245 | + supported_languages[code_block["language"]], |
| 246 | + code_block["first_line"], |
| 247 | + code_block["last_line"], |
| 248 | + ) |
213 | 249 |
|
214 | 250 | if args.autoformat and "noautoformat" not in code_block["flags"]:
|
215 | 251 | autoformat(
|
|
0 commit comments