1- // SPDX-FileCopyrightText: Lukas Neubert <lukas.neubert@proton.me>
1+ // SPDX-FileCopyrightText: Lukas Neubert
22// SPDX-License-Identifier: MIT
33package main
44
5+ import cli.options
56import os
67
78struct CodeBlock {
@@ -11,35 +12,52 @@ struct CodeBlock {
1112}
1213
1314fun main() {
14- if os.ARGS.length <= 2 {
15- eprintln('check-md error: please provide a file to be checked')
15+ mut opt := options.new_option_parser(os.user_args())
16+
17+ backend := opt.string('backend', `b`, 'js')
18+ if backend != 'js' and backend != 'c' {
19+ eprintln('check-md: invalid backend')
20+ exit(1)
21+ }
22+ args := opt.remaining()
23+
24+ if args.length == 0 {
25+ eprintln('check-md: provide a file to be checked')
1626 exit(1)
1727 }
18- file := os.ARGS[2]
28+
29+ file := args[0]
1930 if not file.ends_with('.md') {
2031 eprintln('check-md error: file must be a markdown file')
2132 exit(1)
2233 }
34+
2335 println('Checking code blocks in ${file}...')
2436 blocks := collect_code_blocks(file)
25- mut has_errors := false
37+
38+ mut nr_errors := 0
2639 for block in blocks {
27- res := check_block_runs(block)
40+ res := check_block_runs(block, backend )
2841 if not res {
29- has_errors = true
42+ nr_errors += 1
3043 }
3144 }
32- if has_errors {
45+
46+ println('ok: ${blocks.length - nr_errors}, failed: ${nr_errors}, total: ${blocks.length}')
47+
48+ if nr_errors > 0 {
3349 exit(1)
3450 }
3551}
3652
3753fun collect_code_blocks(path string) []CodeBlock{
3854 lines := os.read_lines(path)
55+
3956 mut blocks := []CodeBlock
4057 mut inside_block := false
4158 mut start := 0
4259 mut text := ''
60+
4361 for i, line in lines {
4462 if line.starts_with('```bait') {
4563 inside_block = true
@@ -56,13 +74,17 @@ fun collect_code_blocks(path string) []CodeBlock{
5674 text += line + '\n'
5775 }
5876 }
77+
5978 return blocks
6079}
6180
62- fun check_block_runs(block CodeBlock) bool {
63- tmp_file := os.join_path(os.tmp_dir(), ['tmp.bt'])
81+ fun check_block_runs(block CodeBlock, backend string) bool {
82+ tmp_name := 'check-md-${block.start_idx}.bt'
83+ tmp_file := os.join_path(os.tmp_dir(), [tmp_name])
6484 os.write_file(tmp_file, block.text)
65- res := os.exec('bun ${$BAITEXE} ${tmp_file} --script')
85+
86+ res := os.exec('bun ${$BAITEXE} "${tmp_file}" --script --backend ${backend}')
87+
6688 if res.code != 0 {
6789 println('Error in block ${block.start_idx + 1}:${block.end_idx + 1}')
6890 println(res.stderr)
0 commit comments