From cda28bb618275cfc5e442f4c6b97afd7a1c00404 Mon Sep 17 00:00:00 2001 From: Manuel Woelker Date: Wed, 13 May 2020 14:45:35 +0200 Subject: [PATCH 1/6] Generate 404.html page (#539) --- book-example/src/404.md | 3 ++ src/cmd/serve.rs | 8 +++- src/config.rs | 33 ++++++++++++++++ src/renderer/html_handlebars/hbs_renderer.rs | 41 ++++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 book-example/src/404.md diff --git a/book-example/src/404.md b/book-example/src/404.md new file mode 100644 index 0000000000..a55db44eec --- /dev/null +++ b/book-example/src/404.md @@ -0,0 +1,3 @@ +# Document not found (404) + +This URL is invalid, sorry. Try the search instead! \ No newline at end of file diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 4e865f615c..843b4e9f64 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -142,7 +142,11 @@ async fn serve(build_dir: PathBuf, address: SocketAddr, reload_tx: broadcast::Se }) }); // A warp Filter that serves from the filesystem. - let book_route = warp::fs::dir(build_dir); - let routes = livereload.or(book_route); + let book_route = warp::fs::dir(build_dir.clone()); + // The fallback route for 404 errors + let fallback_file = "404.html"; + let fallback_route = warp::fs::file(build_dir.join(fallback_file)) + .map(|reply| warp::reply::with_status(reply, warp::http::StatusCode::NOT_FOUND)); + let routes = livereload.or(book_route).or(fallback_route); warp::serve(routes).run(address).await; } diff --git a/src/config.rs b/src/config.rs index a426199e8a..8a8f6ce5f2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -507,6 +507,10 @@ pub struct HtmlConfig { /// FontAwesome icon class to use for the Git repository link. /// Defaults to `fa-github` if `None`. pub git_repository_icon: Option, + /// Input path for the 404 file, defaults to 404.md + pub input_404: Option, + /// Output path for 404.html file, defaults to 404.html, set to "" to disable 404 file output + pub output_404: Option, /// This is used as a bit of a workaround for the `mdbook serve` command. /// Basically, because you set the websocket port from the command line, the /// `mdbook serve` command needs a way to let the HTML renderer know where @@ -538,6 +542,8 @@ impl Default for HtmlConfig { search: None, git_repository_url: None, git_repository_icon: None, + input_404: None, + output_404: None, livereload_url: None, redirect: HashMap::new(), } @@ -1004,4 +1010,31 @@ mod tests { assert_eq!(cfg.book.title, Some(should_be)); } + + #[test] + fn file_404_default() { + let src = r#" + [output.html] + destination = "my-book" + "#; + + let got = Config::from_str(src).unwrap(); + let html_config = got.html_config().unwrap(); + assert_eq!(html_config.input_404, None); + assert_eq!(html_config.output_404, None); + } + + #[test] + fn file_404_custom() { + let src = r#" + [output.html] + input-404= "missing.md" + output-404= "missing.html" + "#; + + let got = Config::from_str(src).unwrap(); + let html_config = got.html_config().unwrap(); + assert_eq!(html_config.input_404, Some("missing.md".to_string())); + assert_eq!(html_config.output_404, Some("missing.html".to_string())); + } } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 4d3e95f2d4..b99b83a0c6 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -437,6 +437,47 @@ impl Renderer for HtmlHandlebars { is_index = false; } + // Render 404 page + if html_config.output_404 != Some("".to_string()) { + let default_404_location = src_dir.join("404.md"); + let content_404 = if let Some(ref filename) = html_config.input_404 { + let path = src_dir.join(filename); + std::fs::read_to_string(&path).map_err(|failure| { + std::io::Error::new( + failure.kind(), + format!("Unable to open 404 input file {:?}", &path), + ) + })? + } else if default_404_location.exists() { + std::fs::read_to_string(&default_404_location).map_err(|failure| { + std::io::Error::new( + failure.kind(), + format!( + "Unable to open default 404 input file {:?}", + &default_404_location + ), + ) + })? + } else { + "# 404 - Document not found\n\nUnfortunately, this URL is no longer valid, please use the navigation bar or search to continue.".to_string() + }; + let html_content_404 = utils::render_markdown(&content_404, html_config.curly_quotes); + + let mut data_404 = data.clone(); + data_404.insert("path".to_owned(), json!("404.md")); + data_404.insert("content".to_owned(), json!(html_content_404)); + let rendered = handlebars.render("index", &data_404)?; + + let rendered = + self.post_process(rendered, &html_config.playpen, ctx.config.rust.edition); + let output_file = match &html_config.output_404 { + None => "404.html", + Some(file) => &file, + }; + utils::fs::write_file(&destination, output_file, rendered.as_bytes())?; + debug!("Creating 404.html ✓"); + } + // Print version self.configure_print_version(&mut data, &print_content); if let Some(ref title) = ctx.config.book.title { From bff36e722958d28e3e8c991283250d1e3b50434f Mon Sep 17 00:00:00 2001 From: Manuel Woelker Date: Sun, 7 Jun 2020 14:14:35 +0200 Subject: [PATCH 2/6] Add the config parameter output.html.site-url to set base url of the 404 page, making links and relative script/css loads behave correctly even in subdirectory paths --- book-example/book.toml | 1 + src/config.rs | 3 +++ src/renderer/html_handlebars/hbs_renderer.rs | 7 +++++++ src/theme/index.hbs | 4 ++++ 4 files changed, 15 insertions(+) diff --git a/book-example/book.toml b/book-example/book.toml index d21c84873e..6bb796cafc 100644 --- a/book-example/book.toml +++ b/book-example/book.toml @@ -9,6 +9,7 @@ edition = "2018" [output.html] mathjax-support = true +site-url = "/" [output.html.playpen] editable = true diff --git a/src/config.rs b/src/config.rs index 8a8f6ce5f2..b955e352d9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -511,6 +511,8 @@ pub struct HtmlConfig { pub input_404: Option, /// Output path for 404.html file, defaults to 404.html, set to "" to disable 404 file output pub output_404: Option, + /// Absolute url to site, used to emit correct paths for the 404 page, which might be accessed in a deeply nested directory + pub site_url: Option, /// This is used as a bit of a workaround for the `mdbook serve` command. /// Basically, because you set the websocket port from the command line, the /// `mdbook serve` command needs a way to let the HTML renderer know where @@ -544,6 +546,7 @@ impl Default for HtmlConfig { git_repository_icon: None, input_404: None, output_404: None, + site_url: None, livereload_url: None, redirect: HashMap::new(), } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index b99b83a0c6..610882188b 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -464,6 +464,13 @@ impl Renderer for HtmlHandlebars { let html_content_404 = utils::render_markdown(&content_404, html_config.curly_quotes); let mut data_404 = data.clone(); + let base_url = if let Some(site_url) = &html_config.site_url { + site_url + } else { + warn!("HTML 'site-url' parameter not set, defaulting to '/'. Please configure this to ensure the 404 page work correctly, especially if your site is hosted in a subdirectory on the HTTP server."); + "/" + }; + data_404.insert("base_url".to_owned(), json!(base_url)); data_404.insert("path".to_owned(), json!("404.md")); data_404.insert("content".to_owned(), json!(html_content_404)); let rendered = handlebars.render("index", &data_404)?; diff --git a/src/theme/index.hbs b/src/theme/index.hbs index e8a92dd098..d6e5ce47af 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -7,6 +7,10 @@ {{#if is_print }} {{/if}} + {{#if base_url}} + + {{/if}} + {{> head}} From 06efa7a6757d00394a31ca2f4960d90e9aca3a0b Mon Sep 17 00:00:00 2001 From: Manuel Woelker Date: Wed, 10 Jun 2020 12:31:34 +0200 Subject: [PATCH 3/6] additional changes to the 404 mechanism based on feedback: - removed config output_404 - ensure serve overrides the site url, and hosts the correct 404 file - refactor 404 rendering into separate fn - formatting --- book-example/book.toml | 2 +- src/cmd/serve.rs | 21 +++- src/config.rs | 16 +-- src/renderer/html_handlebars/hbs_renderer.rs | 100 ++++++++++--------- src/utils/fs.rs | 4 + 5 files changed, 87 insertions(+), 56 deletions(-) diff --git a/book-example/book.toml b/book-example/book.toml index 6bb796cafc..e73e1a54d5 100644 --- a/book-example/book.toml +++ b/book-example/book.toml @@ -9,7 +9,7 @@ edition = "2018" [output.html] mathjax-support = true -site-url = "/" +site-url = "/mdBook/" [output.html.playpen] editable = true diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 843b4e9f64..97de19f51b 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -6,6 +6,7 @@ use futures_util::sink::SinkExt; use futures_util::StreamExt; use mdbook::errors::*; use mdbook::utils; +use mdbook::utils::fs::get_404_output_file; use mdbook::MDBook; use std::net::{SocketAddr, ToSocketAddrs}; use std::path::PathBuf; @@ -68,6 +69,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> { if let Some(dest_dir) = args.value_of("dest-dir") { book.config.build.build_dir = dest_dir.into(); } + // Override site-url for local serving of the 404 file + book.config.set("output.html.site-url", "/")?; book.build()?; @@ -76,13 +79,19 @@ pub fn execute(args: &ArgMatches) -> Result<()> { .next() .ok_or_else(|| anyhow::anyhow!("no address found for {}", address))?; let build_dir = book.build_dir_for("html"); + let input_404 = book + .config + .get("output.html.input-404") + .map(toml::Value::as_str) + .flatten(); + let file_404 = get_404_output_file(input_404); // A channel used to broadcast to any websockets to reload when a file changes. let (tx, _rx) = tokio::sync::broadcast::channel::(100); let reload_tx = tx.clone(); let thread_handle = std::thread::spawn(move || { - serve(build_dir, sockaddr, reload_tx); + serve(build_dir, sockaddr, reload_tx, &file_404); }); let serving_url = format!("http://{}", address); @@ -120,7 +129,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> { } #[tokio::main] -async fn serve(build_dir: PathBuf, address: SocketAddr, reload_tx: broadcast::Sender) { +async fn serve( + build_dir: PathBuf, + address: SocketAddr, + reload_tx: broadcast::Sender, + file_404: &str, +) { // A warp Filter which captures `reload_tx` and provides an `rx` copy to // receive reload messages. let sender = warp::any().map(move || reload_tx.subscribe()); @@ -144,8 +158,7 @@ async fn serve(build_dir: PathBuf, address: SocketAddr, reload_tx: broadcast::Se // A warp Filter that serves from the filesystem. let book_route = warp::fs::dir(build_dir.clone()); // The fallback route for 404 errors - let fallback_file = "404.html"; - let fallback_route = warp::fs::file(build_dir.join(fallback_file)) + let fallback_route = warp::fs::file(build_dir.join(file_404)) .map(|reply| warp::reply::with_status(reply, warp::http::StatusCode::NOT_FOUND)); let routes = livereload.or(book_route).or(fallback_route); warp::serve(routes).run(address).await; diff --git a/src/config.rs b/src/config.rs index b955e352d9..f6825c3981 100644 --- a/src/config.rs +++ b/src/config.rs @@ -507,10 +507,8 @@ pub struct HtmlConfig { /// FontAwesome icon class to use for the Git repository link. /// Defaults to `fa-github` if `None`. pub git_repository_icon: Option, - /// Input path for the 404 file, defaults to 404.md + /// Input path for the 404 file, defaults to 404.md, set to "" to disable 404 file output pub input_404: Option, - /// Output path for 404.html file, defaults to 404.html, set to "" to disable 404 file output - pub output_404: Option, /// Absolute url to site, used to emit correct paths for the 404 page, which might be accessed in a deeply nested directory pub site_url: Option, /// This is used as a bit of a workaround for the `mdbook serve` command. @@ -545,7 +543,6 @@ impl Default for HtmlConfig { git_repository_url: None, git_repository_icon: None, input_404: None, - output_404: None, site_url: None, livereload_url: None, redirect: HashMap::new(), @@ -679,6 +676,7 @@ impl<'de, T> Updateable<'de> for T where T: Serialize + Deserialize<'de> {} #[cfg(test)] mod tests { use super::*; + use crate::utils::fs::get_404_output_file; const COMPLEX_CONFIG: &str = r#" [book] @@ -1024,7 +1022,10 @@ mod tests { let got = Config::from_str(src).unwrap(); let html_config = got.html_config().unwrap(); assert_eq!(html_config.input_404, None); - assert_eq!(html_config.output_404, None); + assert_eq!( + &get_404_output_file(html_config.input_404.as_deref()), + "404.html" + ); } #[test] @@ -1038,6 +1039,9 @@ mod tests { let got = Config::from_str(src).unwrap(); let html_config = got.html_config().unwrap(); assert_eq!(html_config.input_404, Some("missing.md".to_string())); - assert_eq!(html_config.output_404, Some("missing.html".to_string())); + assert_eq!( + &get_404_output_file(html_config.input_404.as_deref()), + "missing.html" + ); } } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 610882188b..dcc1257902 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -12,6 +12,7 @@ use std::collections::HashMap; use std::fs::{self, File}; use std::path::{Path, PathBuf}; +use crate::utils::fs::get_404_output_file; use handlebars::Handlebars; use regex::{Captures, Regex}; @@ -105,6 +106,58 @@ impl HtmlHandlebars { Ok(()) } + fn render_404( + &self, + ctx: &RenderContext, + html_config: &HtmlConfig, + src_dir: &PathBuf, + handlebars: &mut Handlebars<'_>, + data: &mut serde_json::Map, + ) -> Result<()> { + let destination = &ctx.destination; + let content_404 = if let Some(ref filename) = html_config.input_404 { + let path = src_dir.join(filename); + std::fs::read_to_string(&path) + .with_context(|| format!("unable to open 404 input file {:?}", path))? + } else { + // 404 input not explicitly configured try the default file 404.md + let default_404_location = src_dir.join("404.md"); + if default_404_location.exists() { + std::fs::read_to_string(&default_404_location).with_context(|| { + format!("unable to open 404 input file {:?}", default_404_location) + })? + } else { + "# Document not found (404)\n\nThis URL is invalid, sorry. Please use the \ + navigation bar or search to continue." + .to_string() + } + }; + let html_content_404 = utils::render_markdown(&content_404, html_config.curly_quotes); + + let mut data_404 = data.clone(); + let base_url = if let Some(site_url) = &html_config.site_url { + site_url + } else { + debug!( + "HTML 'site-url' parameter not set, defaulting to '/'. Please configure \ + this to ensure the 404 page work correctly, especially if your site is hosted in a \ + subdirectory on the HTTP server." + ); + "/" + }; + data_404.insert("base_url".to_owned(), json!(base_url)); + // Set a dummy path to ensure other paths (e.g. in the TOC) are generated correctly + data_404.insert("path".to_owned(), json!("404.md")); + data_404.insert("content".to_owned(), json!(html_content_404)); + let rendered = handlebars.render("index", &data_404)?; + + let rendered = self.post_process(rendered, &html_config.playpen, ctx.config.rust.edition); + let output_file = get_404_output_file(html_config.input_404.as_deref()); + utils::fs::write_file(&destination, output_file, rendered.as_bytes())?; + debug!("Creating 404.html ✓"); + Ok(()) + } + #[cfg_attr(feature = "cargo-clippy", allow(clippy::let_and_return))] fn post_process( &self, @@ -438,51 +491,8 @@ impl Renderer for HtmlHandlebars { } // Render 404 page - if html_config.output_404 != Some("".to_string()) { - let default_404_location = src_dir.join("404.md"); - let content_404 = if let Some(ref filename) = html_config.input_404 { - let path = src_dir.join(filename); - std::fs::read_to_string(&path).map_err(|failure| { - std::io::Error::new( - failure.kind(), - format!("Unable to open 404 input file {:?}", &path), - ) - })? - } else if default_404_location.exists() { - std::fs::read_to_string(&default_404_location).map_err(|failure| { - std::io::Error::new( - failure.kind(), - format!( - "Unable to open default 404 input file {:?}", - &default_404_location - ), - ) - })? - } else { - "# 404 - Document not found\n\nUnfortunately, this URL is no longer valid, please use the navigation bar or search to continue.".to_string() - }; - let html_content_404 = utils::render_markdown(&content_404, html_config.curly_quotes); - - let mut data_404 = data.clone(); - let base_url = if let Some(site_url) = &html_config.site_url { - site_url - } else { - warn!("HTML 'site-url' parameter not set, defaulting to '/'. Please configure this to ensure the 404 page work correctly, especially if your site is hosted in a subdirectory on the HTTP server."); - "/" - }; - data_404.insert("base_url".to_owned(), json!(base_url)); - data_404.insert("path".to_owned(), json!("404.md")); - data_404.insert("content".to_owned(), json!(html_content_404)); - let rendered = handlebars.render("index", &data_404)?; - - let rendered = - self.post_process(rendered, &html_config.playpen, ctx.config.rust.edition); - let output_file = match &html_config.output_404 { - None => "404.html", - Some(file) => &file, - }; - utils::fs::write_file(&destination, output_file, rendered.as_bytes())?; - debug!("Creating 404.html ✓"); + if html_config.input_404 != Some("".to_string()) { + self.render_404(ctx, &html_config, &src_dir, &mut handlebars, &mut data)?; } // Print version diff --git a/src/utils/fs.rs b/src/utils/fs.rs index d4c0cb5f16..6b7529db3d 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -177,6 +177,10 @@ pub fn copy_files_except_ext( Ok(()) } +pub fn get_404_output_file(input_404: Option<&str>) -> String { + input_404.unwrap_or("404.md").replace(".md", ".html") +} + #[cfg(test)] mod tests { use super::copy_files_except_ext; From 6d6e5407a3a46addea126199f262e57ccdc65dbf Mon Sep 17 00:00:00 2001 From: Manuel Woelker Date: Wed, 10 Jun 2020 12:46:05 +0200 Subject: [PATCH 4/6] document: config 404 config parameters `output.html.site-url` and `output.html.site-url` --- book-example/src/format/config.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index 4178660b01..ea2c2c3b54 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -204,6 +204,12 @@ The following configuration options are available: `/appendices/bibliography.html`). The value can be any valid URI the browser should navigate to (e.g. `https://rust-lang.org/`, `/overview.html`, or `../bibliography.html`). +- **input-404:** The name of the markdown file used for misssing files. + The corresponding output file will be the same, with the extension replaced with `html`. + Defaults to `404.md`. +- **site-url:** The url where the book will be hosted. This is required to ensure + navigation links and script/css imports in the 404 file work correctly, even when accessing + urls in subdirectories. Defaults to `/`. Available configuration options for the `[output.html.fold]` table: @@ -266,6 +272,8 @@ additional-js = ["custom.js"] no-section-label = false git-repository-url = "https://github.com/rust-lang/mdBook" git-repository-icon = "fa-github" +site-url = "/example-book/" +input-404 = "not-found.md" [output.html.fold] enable = false From 406b325c5477444db44541964cea130f44f11a7f Mon Sep 17 00:00:00 2001 From: Manuel Woelker Date: Wed, 10 Jun 2020 13:09:18 +0200 Subject: [PATCH 5/6] fix usage of newly stablized inner_deref/as_deref --- src/cmd/serve.rs | 5 +++-- src/config.rs | 4 ++-- src/renderer/html_handlebars/hbs_renderer.rs | 2 +- src/utils/fs.rs | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 97de19f51b..e9209b638e 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -83,8 +83,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> { .config .get("output.html.input-404") .map(toml::Value::as_str) - .flatten(); - let file_404 = get_404_output_file(input_404); + .flatten() + .map(ToString::to_string); + let file_404 = get_404_output_file(&input_404); // A channel used to broadcast to any websockets to reload when a file changes. let (tx, _rx) = tokio::sync::broadcast::channel::(100); diff --git a/src/config.rs b/src/config.rs index f6825c3981..cbcf3e91d6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1023,7 +1023,7 @@ mod tests { let html_config = got.html_config().unwrap(); assert_eq!(html_config.input_404, None); assert_eq!( - &get_404_output_file(html_config.input_404.as_deref()), + &get_404_output_file(&html_config.input_404), "404.html" ); } @@ -1040,7 +1040,7 @@ mod tests { let html_config = got.html_config().unwrap(); assert_eq!(html_config.input_404, Some("missing.md".to_string())); assert_eq!( - &get_404_output_file(html_config.input_404.as_deref()), + &get_404_output_file(&html_config.input_404), "missing.html" ); } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index dcc1257902..e1ca9a15d6 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -152,7 +152,7 @@ impl HtmlHandlebars { let rendered = handlebars.render("index", &data_404)?; let rendered = self.post_process(rendered, &html_config.playpen, ctx.config.rust.edition); - let output_file = get_404_output_file(html_config.input_404.as_deref()); + let output_file = get_404_output_file(&html_config.input_404); utils::fs::write_file(&destination, output_file, rendered.as_bytes())?; debug!("Creating 404.html ✓"); Ok(()) diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 6b7529db3d..a1bea13304 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -177,8 +177,8 @@ pub fn copy_files_except_ext( Ok(()) } -pub fn get_404_output_file(input_404: Option<&str>) -> String { - input_404.unwrap_or("404.md").replace(".md", ".html") +pub fn get_404_output_file(input_404: &Option) -> String { + input_404.as_ref().unwrap_or(&"404.md".to_string()).replace(".md", ".html") } #[cfg(test)] From d7df832cceaaa07d1546d05b5474b3cb70190e77 Mon Sep 17 00:00:00 2001 From: Manuel Woelker Date: Wed, 10 Jun 2020 15:33:09 +0200 Subject: [PATCH 6/6] fix test and formatting --- src/cmd/serve.rs | 2 +- src/config.rs | 10 ++-------- src/utils/fs.rs | 5 ++++- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index e9209b638e..0940938fe2 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -83,7 +83,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> { .config .get("output.html.input-404") .map(toml::Value::as_str) - .flatten() + .and_then(std::convert::identity) // flatten .map(ToString::to_string); let file_404 = get_404_output_file(&input_404); diff --git a/src/config.rs b/src/config.rs index cbcf3e91d6..8a7744ebcc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1022,10 +1022,7 @@ mod tests { let got = Config::from_str(src).unwrap(); let html_config = got.html_config().unwrap(); assert_eq!(html_config.input_404, None); - assert_eq!( - &get_404_output_file(&html_config.input_404), - "404.html" - ); + assert_eq!(&get_404_output_file(&html_config.input_404), "404.html"); } #[test] @@ -1039,9 +1036,6 @@ mod tests { let got = Config::from_str(src).unwrap(); let html_config = got.html_config().unwrap(); assert_eq!(html_config.input_404, Some("missing.md".to_string())); - assert_eq!( - &get_404_output_file(&html_config.input_404), - "missing.html" - ); + assert_eq!(&get_404_output_file(&html_config.input_404), "missing.html"); } } diff --git a/src/utils/fs.rs b/src/utils/fs.rs index a1bea13304..7a7aa63a4c 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -178,7 +178,10 @@ pub fn copy_files_except_ext( } pub fn get_404_output_file(input_404: &Option) -> String { - input_404.as_ref().unwrap_or(&"404.md".to_string()).replace(".md", ".html") + input_404 + .as_ref() + .unwrap_or(&"404.md".to_string()) + .replace(".md", ".html") } #[cfg(test)]