Skip to content

Commit 70e174d

Browse files
authored
Unrolled build for rust-lang#130585
Rollup merge of rust-lang#130585 - GuillaumeGomez:add-rustdoc-template-tidy-check, r=notriddle Add tidy check for rustdoc templates to ensure the whitespace characters are all stripped Fixes rust-lang#130559. I'm planning to send a follow-up in case a tag at the end of a line isn't needed (if the next line starts with a jinja tag for example). r? `@notriddle`
2 parents 1a5a224 + 8745bcf commit 70e174d

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/tools/tidy/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub mod pal;
8282
pub mod run_make_tests;
8383
pub mod rustdoc_css_themes;
8484
pub mod rustdoc_gui_tests;
85+
pub mod rustdoc_templates;
8586
pub mod style;
8687
pub mod target_policy;
8788
pub mod target_specific_tests;

src/tools/tidy/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ fn main() {
108108
check!(mir_opt_tests, &tests_path, bless);
109109
check!(rustdoc_gui_tests, &tests_path);
110110
check!(rustdoc_css_themes, &librustdoc_path);
111+
check!(rustdoc_templates, &librustdoc_path);
111112
check!(known_bug, &crashes_path);
112113
check!(unknown_revision, &tests_path);
113114

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)