Changed redirect mapping to HashMap<String, String> and improved error handling
This commit is contained in:
parent
a6ab4d8402
commit
da55cf273f
|
@ -517,7 +517,7 @@ pub struct HtmlConfig {
|
||||||
pub livereload_url: Option<String>,
|
pub livereload_url: Option<String>,
|
||||||
/// The mapping from old pages to new pages/URLs to use when generating
|
/// The mapping from old pages to new pages/URLs to use when generating
|
||||||
/// redirects.
|
/// redirects.
|
||||||
pub redirect: HashMap<PathBuf, String>,
|
pub redirect: HashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for HtmlConfig {
|
impl Default for HtmlConfig {
|
||||||
|
@ -741,9 +741,9 @@ mod tests {
|
||||||
git_repository_url: Some(String::from("https://foo.com/")),
|
git_repository_url: Some(String::from("https://foo.com/")),
|
||||||
git_repository_icon: Some(String::from("fa-code-fork")),
|
git_repository_icon: Some(String::from("fa-code-fork")),
|
||||||
redirect: vec![
|
redirect: vec![
|
||||||
(PathBuf::from("index.html"), String::from("overview.html")),
|
(String::from("index.html"), String::from("overview.html")),
|
||||||
(
|
(
|
||||||
PathBuf::from("nexted/page.md"),
|
String::from("nexted/page.md"),
|
||||||
String::from("https://rust-lang.org/"),
|
String::from("https://rust-lang.org/"),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -284,7 +284,7 @@ impl HtmlHandlebars {
|
||||||
&self,
|
&self,
|
||||||
root: &Path,
|
root: &Path,
|
||||||
handlebars: &Handlebars<'_>,
|
handlebars: &Handlebars<'_>,
|
||||||
redirects: &HashMap<PathBuf, String>,
|
redirects: &HashMap<String, String>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
if redirects.is_empty() {
|
if redirects.is_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -293,7 +293,11 @@ impl HtmlHandlebars {
|
||||||
log::debug!("Emitting redirects");
|
log::debug!("Emitting redirects");
|
||||||
|
|
||||||
for (original, new) in redirects {
|
for (original, new) in redirects {
|
||||||
log::debug!("Redirecting \"{}\" → \"{}\"", original.display(), new);
|
log::debug!("Redirecting \"{}\" → \"{}\"", original, new);
|
||||||
|
// Note: all paths are relative to the build directory, so the
|
||||||
|
// leading slash in an absolute path means nothing (and would mess
|
||||||
|
// up `root.join(original)`).
|
||||||
|
let original = original.trim_start_matches("/");
|
||||||
let filename = root.join(original);
|
let filename = root.join(original);
|
||||||
self.emit_redirect(handlebars, &filename, new)?;
|
self.emit_redirect(handlebars, &filename, new)?;
|
||||||
}
|
}
|
||||||
|
@ -307,15 +311,33 @@ impl HtmlHandlebars {
|
||||||
original: &Path,
|
original: &Path,
|
||||||
destination: &str,
|
destination: &str,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
if original.exists() {
|
||||||
|
// sanity check to avoid accidentally overwriting a real file.
|
||||||
|
let msg = format!(
|
||||||
|
"Not redirecting \"{}\" to \"{}\" because it already exists. Are you sure it needs to be redirected?",
|
||||||
|
original.display(),
|
||||||
|
destination,
|
||||||
|
);
|
||||||
|
return Err(Error::msg(msg));
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(parent) = original.parent() {
|
if let Some(parent) = original.parent() {
|
||||||
std::fs::create_dir_all(parent)?;
|
std::fs::create_dir_all(parent)
|
||||||
|
.with_context(|| format!("Unable to ensure \"{}\" exists", parent.display()))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let ctx = json!({
|
let ctx = json!({
|
||||||
"url": destination,
|
"url": destination,
|
||||||
});
|
});
|
||||||
let f = File::create(original)?;
|
let f = File::create(original)?;
|
||||||
handlebars.render_to_write("redirect", &ctx, f)?;
|
handlebars
|
||||||
|
.render_to_write("redirect", &ctx, f)
|
||||||
|
.with_context(|| {
|
||||||
|
format!(
|
||||||
|
"Unable to create a redirect file at \"{}\"",
|
||||||
|
original.display()
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -445,7 +467,8 @@ impl Renderer for HtmlHandlebars {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.emit_redirects(&ctx.destination, &handlebars, &html_config.redirect)?;
|
self.emit_redirects(&ctx.destination, &handlebars, &html_config.redirect)
|
||||||
|
.context("Unable to emit redirects")?;
|
||||||
|
|
||||||
// Copy all remaining files, avoid a recursive copy from/to the book build dir
|
// Copy all remaining files, avoid a recursive copy from/to the book build dir
|
||||||
utils::fs::copy_files_except_ext(&src_dir, &destination, true, Some(&build_dir), &["md"])?;
|
utils::fs::copy_files_except_ext(&src_dir, &destination, true, Some(&build_dir), &["md"])?;
|
||||||
|
|
Loading…
Reference in New Issue