Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::teams::{encode_zulip_stream, load_rust_teams};
use anyhow::Context;
use handlebars::{DirectorySourceOptions, Handlebars};
use handlebars_fluent::FluentHelper;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};

mod assets;
Expand Down Expand Up @@ -94,7 +95,7 @@ fn main() -> anyhow::Result<()> {
rust_version,
teams,
handlebars,
output_dir,
output_dir: output_dir.clone(),
base_url,
};
ctx.copy_asset_dir("static", "static")?;
Expand All @@ -113,5 +114,42 @@ fn main() -> anyhow::Result<()> {
ctx.page("404", "", &(), ENGLISH).render("404.html")?;
create_redirects(&ctx)?;

sanity_check_index_pages(&output_dir)?;

Ok(())
}

/// Make sure that there are no instances where we would have both `<page>.html` and
/// a `<page>` directory.
fn sanity_check_index_pages(directory: &Path) -> anyhow::Result<()> {
// Find all .html files
let mut html_files = vec![];
gather_html_files(directory, &mut html_files)?;

for file in html_files {
let basename = file.file_stem().unwrap();
let dir = file.parent().unwrap().join(basename);
if dir.is_dir() {
return Err(anyhow::anyhow!(
"Both the `{file}` file and the `{dir}` directory exist, move `{file}` to `{dir_index}` instead",
file = file.display(),
dir = dir.display(),
dir_index = dir.join("index.html").display()
));
}
}

Ok(())
}

fn gather_html_files(path: &Path, files: &mut Vec<PathBuf>) -> anyhow::Result<()> {
if path.is_file() && path.extension() == Some(OsStr::new("html")) {
files.push(path.to_path_buf());
} else if path.is_dir() {
for entry in path.read_dir()? {
let entry = entry?;
gather_html_files(&entry.path(), files)?;
}
}
Ok(())
}
1 change: 0 additions & 1 deletion src/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::render::{RenderCtx, render_redirect};

pub static PAGE_REDIRECTS: &[(&str, &str)] = &[
// Pre-2018 website pages
("community.html", "community"),
("conduct.html", "policies/code-of-conduct"),
("contribute-bugs.html", "community"),
("contribute-community.html", "community"),
Expand Down
6 changes: 6 additions & 0 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ pub fn render_directory(render_ctx: &RenderCtx, category: &str) -> anyhow::Resul
}

pub fn render_redirect(render_ctx: &RenderCtx, from: &str, to: &str) -> anyhow::Result<()> {
if from == format!("{to}.html") || format!("{from}.html") == to {
return Err(anyhow::anyhow!(
"Trying to setup redirect from {from} to {to}, which would alias"
));
}

#[derive(Serialize)]
struct Data {
url: String,
Expand Down