|
| 1 | +use std::env; |
| 2 | +use std::error::Error; |
| 3 | +use std::fs; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +macro_rules! style_error { |
| 7 | + ($bad:expr, $path:expr, $($arg:tt)*) => { |
| 8 | + *$bad = true; |
| 9 | + eprint!("error in {}: ", $path.display()); |
| 10 | + eprintln!("{}", format_args!($($arg)*)); |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +fn main() { |
| 15 | + let arg = env::args().nth(1).unwrap_or_else(|| { |
| 16 | + eprintln!("Please pass a src directory as the first argument"); |
| 17 | + std::process::exit(1); |
| 18 | + }); |
| 19 | + |
| 20 | + let mut bad = false; |
| 21 | + if let Err(e) = check_directory(&Path::new(&arg), &mut bad) { |
| 22 | + eprintln!("error: {}", e); |
| 23 | + std::process::exit(1); |
| 24 | + } |
| 25 | + if bad { |
| 26 | + eprintln!("some style checks failed"); |
| 27 | + std::process::exit(1); |
| 28 | + } |
| 29 | + eprintln!("passed!"); |
| 30 | +} |
| 31 | + |
| 32 | +fn check_directory(dir: &Path, bad: &mut bool) -> Result<(), Box<dyn Error>> { |
| 33 | + for entry in fs::read_dir(dir)? { |
| 34 | + let entry = entry?; |
| 35 | + let path = entry.path(); |
| 36 | + |
| 37 | + if path.is_dir() { |
| 38 | + check_directory(&path, bad)?; |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + if !matches!( |
| 43 | + path.extension().and_then(|p| p.to_str()), |
| 44 | + Some("md") | Some("html") |
| 45 | + ) { |
| 46 | + // This may be extended in the future if other file types are needed. |
| 47 | + style_error!(bad, path, "expected only md or html in src"); |
| 48 | + } |
| 49 | + |
| 50 | + let contents = fs::read_to_string(&path)?; |
| 51 | + if contents.contains("#![feature") { |
| 52 | + style_error!(bad, path, "#![feature] attributes are not allowed"); |
| 53 | + } |
| 54 | + if contents.contains('\r') { |
| 55 | + style_error!( |
| 56 | + bad, |
| 57 | + path, |
| 58 | + "CR characters not allowed, must use LF line endings" |
| 59 | + ); |
| 60 | + } |
| 61 | + if contents.contains('\t') { |
| 62 | + style_error!(bad, path, "tab characters not allowed, use spaces"); |
| 63 | + } |
| 64 | + if !contents.ends_with('\n') { |
| 65 | + style_error!(bad, path, "file must end with a newline"); |
| 66 | + } |
| 67 | + for line in contents.lines() { |
| 68 | + if line.ends_with(' ') { |
| 69 | + style_error!(bad, path, "lines must not end with spaces"); |
| 70 | + } |
| 71 | + } |
| 72 | + cmark_check(&path, bad, &contents)?; |
| 73 | + } |
| 74 | + Ok(()) |
| 75 | +} |
| 76 | + |
| 77 | +fn cmark_check(path: &Path, bad: &mut bool, contents: &str) -> Result<(), Box<dyn Error>> { |
| 78 | + use pulldown_cmark::{BrokenLink, CodeBlockKind, Event, Options, Parser, Tag}; |
| 79 | + |
| 80 | + macro_rules! cmark_error { |
| 81 | + ($bad:expr, $path:expr, $range:expr, $($arg:tt)*) => { |
| 82 | + *$bad = true; |
| 83 | + let lineno = contents[..$range.start].chars().filter(|&ch| ch == '\n').count() + 1; |
| 84 | + eprint!("error in {} (line {}): ", $path.display(), lineno); |
| 85 | + eprintln!("{}", format_args!($($arg)*)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + let options = Options::all(); |
| 90 | + // Can't use `bad` because it would get captured in closure. |
| 91 | + let mut link_err = false; |
| 92 | + let mut cb = |link: BrokenLink<'_>| { |
| 93 | + cmark_error!( |
| 94 | + &mut link_err, |
| 95 | + path, |
| 96 | + link.span, |
| 97 | + "broken {:?} link (reference `{}`)", |
| 98 | + link.link_type, |
| 99 | + link.reference |
| 100 | + ); |
| 101 | + None |
| 102 | + }; |
| 103 | + let parser = Parser::new_with_broken_link_callback(contents, options, Some(&mut cb)); |
| 104 | + |
| 105 | + for (event, range) in parser.into_offset_iter() { |
| 106 | + match event { |
| 107 | + Event::Start(Tag::CodeBlock(CodeBlockKind::Indented)) => { |
| 108 | + cmark_error!( |
| 109 | + bad, |
| 110 | + path, |
| 111 | + range, |
| 112 | + "indented code blocks should use triple backtick-style \ |
| 113 | + with a language identifier" |
| 114 | + ); |
| 115 | + } |
| 116 | + Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(languages))) => { |
| 117 | + if languages.is_empty() { |
| 118 | + cmark_error!( |
| 119 | + bad, |
| 120 | + path, |
| 121 | + range, |
| 122 | + "code block should include an explicit language", |
| 123 | + ); |
| 124 | + } |
| 125 | + } |
| 126 | + _ => {} |
| 127 | + } |
| 128 | + } |
| 129 | + *bad |= link_err; |
| 130 | + Ok(()) |
| 131 | +} |
0 commit comments