|
| 1 | +//! Tidy check to ensure that rustdoc templates didn't forget a `{# #}` to strip extra whitespace |
| 2 | +//! characters. |
| 3 | +
|
| 4 | +use std::ffi::OsStr; |
| 5 | +use std::path::Path; |
| 6 | + |
| 7 | +use ignore::DirEntry; |
| 8 | + |
| 9 | +use crate::walk::walk; |
| 10 | + |
| 11 | +// Array containing `("beginning of tag", "end of tag")`. |
| 12 | +const TAGS: &[(&str, &str)] = &[("{#", "#}"), ("{%", "%}"), ("{{", "}}")]; |
| 13 | + |
| 14 | +pub fn check(librustdoc_path: &Path, bad: &mut bool) { |
| 15 | + walk( |
| 16 | + &librustdoc_path.join("html/templates"), |
| 17 | + |path, is_dir| is_dir || !path.extension().is_some_and(|ext| ext == OsStr::new("html")), |
| 18 | + &mut |path: &DirEntry, file_content: &str| { |
| 19 | + let mut lines = file_content.lines().enumerate().peekable(); |
| 20 | + |
| 21 | + while let Some((pos, line)) = lines.next() { |
| 22 | + let line = line.trim(); |
| 23 | + if TAGS.iter().any(|(_, tag)| line.ends_with(tag)) { |
| 24 | + continue; |
| 25 | + } |
| 26 | + let Some(next_line) = lines.peek().map(|(_, next_line)| next_line.trim()) else { |
| 27 | + continue; |
| 28 | + }; |
| 29 | + if TAGS.iter().any(|(tag, _)| next_line.starts_with(tag)) { |
| 30 | + continue; |
| 31 | + } |
| 32 | + // Maybe this is a multi-line tag, let's filter it out then. |
| 33 | + match TAGS.iter().find_map(|(tag, end_tag)| { |
| 34 | + if line.rfind(tag).is_some() { Some(end_tag) } else { None } |
| 35 | + }) { |
| 36 | + None => { |
| 37 | + // No it's not, let's error. |
| 38 | + tidy_error!( |
| 39 | + bad, |
| 40 | + "`{}` at line {}: missing `{{# #}}` at the end of the line", |
| 41 | + path.path().display(), |
| 42 | + pos + 1, |
| 43 | + ); |
| 44 | + } |
| 45 | + Some(end_tag) => { |
| 46 | + // We skip the tag. |
| 47 | + while let Some((_, next_line)) = lines.peek() { |
| 48 | + if next_line.contains(end_tag) { |
| 49 | + break; |
| 50 | + } |
| 51 | + lines.next(); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + }, |
| 57 | + ); |
| 58 | +} |
0 commit comments