// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. module main import os import os.cmdline import rand import term import v.help import regex const too_long_line_length_example = 120 const too_long_line_length_codeblock = 120 const too_long_line_length_table = 120 const too_long_line_length_link = 150 const too_long_line_length_other = 100 const term_colors = term.can_show_color_on_stderr() const hide_warnings = '-hide-warnings' in os.args || '-w' in os.args const show_progress = os.getenv('GITHUB_JOB') == '' && '-silent' !in os.args const non_option_args = cmdline.only_non_options(os.args[2..]) const is_verbose = os.getenv('VERBOSE') != '' const vcheckfolder = os.join_path(os.vtmp_dir(), 'vcheck_${os.getuid()}') const should_autofix = os.getenv('VAUTOFIX') != '' const vexe = @VEXE struct CheckResult { pub mut: warnings int errors int oks int } fn (v1 CheckResult) + (v2 CheckResult) CheckResult { return CheckResult{ warnings: v1.warnings + v2.warnings errors: v1.errors + v2.errors oks: v1.oks + v2.oks } } fn main() { if non_option_args.len == 0 || '-help' in os.args { help.print_and_exit('check-md') } if '-all' in os.args { println('´-all´ flag is deprecated. Please use ´v check-md .´ instead.') exit(1) } mut skip_line_length_check := '-skip-line-length-check' in os.args if show_progress { // this is intended to be replaced by the progress lines println('') } mut files_paths := non_option_args.clone() mut res := CheckResult{} if term_colors { os.setenv('VCOLORS', 'always', true) } os.mkdir_all(vcheckfolder, mode: 0o700) or {} // keep directory private defer { os.rmdir_all(vcheckfolder) or {} } for i := 0; i < files_paths.len; i++ { file_path := files_paths[i] if os.is_dir(file_path) { files_paths << md_file_paths(file_path) continue } real_path := os.real_path(file_path) lines := os.read_lines(real_path) or { println('"${file_path}" does not exist') res.warnings++ continue } mut mdfile := MDFile{ skip_line_length_check: skip_line_length_check path: file_path lines: lines } res += mdfile.check() } if res.errors == 0 && show_progress { clear_previous_line() } if res.warnings > 0 || res.errors > 0 || res.oks > 0 { println('\nWarnings: ${res.warnings} | Errors: ${res.errors} | OKs: ${res.oks}') } if res.errors > 0 { exit(1) } } fn md_file_paths(dir string) []string { mut files_to_check := []string{} md_files := os.walk_ext(dir, '.md') for file in md_files { nfile := file.replace('\\', '/') if nfile.contains_any_substr(['/thirdparty/', 'CHANGELOG']) { continue } files_to_check << file } return files_to_check } fn wprintln(s string) { if !hide_warnings { println(s) } } fn ftext(s string, cb fn (string) string) string { if term_colors { return cb(s) } return s } fn btext(s string) string { return ftext(s, term.bold) } fn mtext(s string) string { return ftext(s, term.magenta) } fn rtext(s string) string { return ftext(s, term.red) } fn wline(file_path string, lnumber int, column int, message string) string { return btext('${file_path}:${lnumber + 1}:${column + 1}:') + btext(mtext(' warn:')) + rtext(' ${message}') } fn eline(file_path string, lnumber int, column int, message string) string { return btext('${file_path}:${lnumber + 1}:${column + 1}:') + btext(rtext(' error: ${message}')) } const default_command = 'compile' struct VCodeExample { mut: text []string command string sline int eline int } enum MDFileParserState { markdown vexample codeblock } struct MDFile { path string skip_line_length_check bool mut: lines []string examples []VCodeExample current VCodeExample state MDFileParserState = .markdown } fn (mut f MDFile) progress(message string) { if show_progress { clear_previous_line() println('File: ${f.path:-30s}, Lines: ${f.lines.len:5}, ${message}') } } fn (mut f MDFile) check() CheckResult { mut res := CheckResult{} mut anchor_data := AnchorData{} for j, line in f.lines { // f.progress('line: $j') if !f.skip_line_length_check { if f.state == .vexample { if line.len > too_long_line_length_example { wprintln(wline(f.path, j, line.len, 'example lines must be less than ${too_long_line_length_example} characters')) wprintln(line) res.warnings++ } } else if f.state == .codeblock { if line.len > too_long_line_length_codeblock { wprintln(wline(f.path, j, line.len, 'code lines must be less than ${too_long_line_length_codeblock} characters')) wprintln(line) res.warnings++ } } else if line.starts_with('|') { if line.len > too_long_line_length_table { wprintln(wline(f.path, j, line.len, 'table lines must be less than ${too_long_line_length_table} characters')) wprintln(line) res.warnings++ } } else if line.contains('http') { if line.all_after('https').len > too_long_line_length_link { wprintln(wline(f.path, j, line.len, 'link lines must be less than ${too_long_line_length_link} characters')) wprintln(line) res.warnings++ } } else if line.len > too_long_line_length_other { eprintln(eline(f.path, j, line.len, 'must be less than ${too_long_line_length_other} characters')) eprintln(line) res.errors++ } } if f.state == .markdown { anchor_data.add_links(j, line) anchor_data.add_link_targets(j, line) } f.parse_line(j, line) } anchor_data.check_link_target_match(f.path, mut res) res += f.check_examples() return res } fn (mut f MDFile) parse_line(lnumber int, line string) { if line.starts_with('```v') { if f.state == .markdown { f.state = .vexample mut command := line.replace('```v', '').trim_space() if command == '' { command = default_command } else if command == 'nofmt' { command += ' ${default_command}' } f.current = VCodeExample{ sline: lnumber command: command } } return } if line.starts_with('```') { match f.state { .vexample { f.state = .markdown f.current.eline = lnumber f.examples << f.current f.current = VCodeExample{} return } .codeblock { f.state = .markdown return } .markdown { f.state = .codeblock return } } } if f.state == .vexample { f.current.text << line } } struct Headline { line int label string level int } struct Anchor { line int } type AnchorTarget = Anchor | Headline struct AnchorLink { line int label string } struct AnchorData { mut: links map[string][]AnchorLink anchors map[string][]AnchorTarget } fn (mut ad AnchorData) add_links(line_number int, line string) { query := r'\[(?P