Skip to content

Commit 25acfde

Browse files
authored
Merge pull request #2192
Fix community redirect loop
2 parents 49998f0 + b56fd83 commit 25acfde

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/main.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::teams::{encode_zulip_stream, load_rust_teams};
77
use anyhow::Context;
88
use handlebars::{DirectorySourceOptions, Handlebars};
99
use handlebars_fluent::FluentHelper;
10+
use std::ffi::OsStr;
1011
use std::path::{Path, PathBuf};
1112

1213
mod assets;
@@ -94,7 +95,7 @@ fn main() -> anyhow::Result<()> {
9495
rust_version,
9596
teams,
9697
handlebars,
97-
output_dir,
98+
output_dir: output_dir.clone(),
9899
base_url,
99100
};
100101
ctx.copy_asset_dir("static", "static")?;
@@ -113,5 +114,42 @@ fn main() -> anyhow::Result<()> {
113114
ctx.page("404", "", &(), ENGLISH).render("404.html")?;
114115
create_redirects(&ctx)?;
115116

117+
sanity_check_index_pages(&output_dir)?;
118+
119+
Ok(())
120+
}
121+
122+
/// Make sure that there are no instances where we would have both `<page>.html` and
123+
/// a `<page>` directory.
124+
fn sanity_check_index_pages(directory: &Path) -> anyhow::Result<()> {
125+
// Find all .html files
126+
let mut html_files = vec![];
127+
gather_html_files(directory, &mut html_files)?;
128+
129+
for file in html_files {
130+
let basename = file.file_stem().unwrap();
131+
let dir = file.parent().unwrap().join(basename);
132+
if dir.is_dir() {
133+
return Err(anyhow::anyhow!(
134+
"Both the `{file}` file and the `{dir}` directory exist, move `{file}` to `{dir_index}` instead",
135+
file = file.display(),
136+
dir = dir.display(),
137+
dir_index = dir.join("index.html").display()
138+
));
139+
}
140+
}
141+
142+
Ok(())
143+
}
144+
145+
fn gather_html_files(path: &Path, files: &mut Vec<PathBuf>) -> anyhow::Result<()> {
146+
if path.is_file() && path.extension() == Some(OsStr::new("html")) {
147+
files.push(path.to_path_buf());
148+
} else if path.is_dir() {
149+
for entry in path.read_dir()? {
150+
let entry = entry?;
151+
gather_html_files(&entry.path(), files)?;
152+
}
153+
}
116154
Ok(())
117155
}

src/redirect.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::render::{RenderCtx, render_redirect};
22

33
pub static PAGE_REDIRECTS: &[(&str, &str)] = &[
44
// Pre-2018 website pages
5-
("community.html", "community"),
65
("conduct.html", "policies/code-of-conduct"),
76
("contribute-bugs.html", "community"),
87
("contribute-community.html", "community"),

src/render.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ pub fn render_directory(render_ctx: &RenderCtx, category: &str) -> anyhow::Resul
261261
}
262262

263263
pub fn render_redirect(render_ctx: &RenderCtx, from: &str, to: &str) -> anyhow::Result<()> {
264+
if from == format!("{to}.html") || format!("{from}.html") == to {
265+
return Err(anyhow::anyhow!(
266+
"Trying to setup redirect from {from} to {to}, which would alias"
267+
));
268+
}
269+
264270
#[derive(Serialize)]
265271
struct Data {
266272
url: String,

0 commit comments

Comments
 (0)