Skip to content

Commit f24221a

Browse files
authored
Merge pull request #2855 from ehuss/move-get_404_output_file
Move get_404_output_file to HtmlConfig
2 parents 73aeed4 + 6223189 commit f24221a

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

crates/mdbook-core/src/config.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,14 @@ impl HtmlConfig {
539539
None => root.join("theme"),
540540
}
541541
}
542+
543+
/// Returns the name of the file used for HTTP 404 "not found" with the `.html` extension.
544+
pub fn get_404_output_file(&self) -> String {
545+
self.input_404
546+
.as_ref()
547+
.unwrap_or(&"404.md".to_string())
548+
.replace(".md", ".html")
549+
}
542550
}
543551

544552
/// Configuration for how to render the print icon, print.html, and print.css.
@@ -706,7 +714,6 @@ impl<'de, T> Updateable<'de> for T where T: Serialize + Deserialize<'de> {}
706714
#[cfg(test)]
707715
mod tests {
708716
use super::*;
709-
use crate::utils::fs::get_404_output_file;
710717

711718
const COMPLEX_CONFIG: &str = r#"
712719
[book]
@@ -973,7 +980,7 @@ mod tests {
973980
let got = Config::from_str(src).unwrap();
974981
let html_config = got.html_config().unwrap();
975982
assert_eq!(html_config.input_404, None);
976-
assert_eq!(&get_404_output_file(&html_config.input_404), "404.html");
983+
assert_eq!(html_config.get_404_output_file(), "404.html");
977984
}
978985

979986
#[test]
@@ -986,7 +993,7 @@ mod tests {
986993
let got = Config::from_str(src).unwrap();
987994
let html_config = got.html_config().unwrap();
988995
assert_eq!(html_config.input_404, Some("missing.md".to_string()));
989-
assert_eq!(&get_404_output_file(&html_config.input_404), "missing.html");
996+
assert_eq!(html_config.get_404_output_file(), "missing.html");
990997
}
991998

992999
#[test]

crates/mdbook-core/src/utils/fs.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,6 @@ fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()> {
196196
}
197197
}
198198

199-
/// Returns the name of the file used for HTTP 404 "not found" with the `.html` extension.
200-
pub fn get_404_output_file(input_404: &Option<String>) -> String {
201-
input_404
202-
.as_ref()
203-
.unwrap_or(&"404.md".to_string())
204-
.replace(".md", ".html")
205-
}
206-
207199
#[cfg(test)]
208200
mod tests {
209201
use super::copy_files_except_ext;

crates/mdbook-html/src/html_handlebars/hbs_renderer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl HtmlHandlebars {
195195
data_404.insert("title".to_owned(), json!(title));
196196
let rendered = handlebars.render("index", &data_404)?;
197197

198-
let output_file = utils::fs::get_404_output_file(&html_config.input_404);
198+
let output_file = html_config.get_404_output_file();
199199
utils::fs::write_file(destination, output_file, rendered.as_bytes())?;
200200
debug!("Creating 404.html ✓");
201201
Ok(())

src/cmd/serve.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use axum::routing::get;
99
use clap::builder::NonEmptyStringValueParser;
1010
use futures_util::StreamExt;
1111
use futures_util::sink::SinkExt;
12-
use mdbook_core::utils::fs::get_404_output_file;
1312
use mdbook_driver::MDBook;
1413
use std::net::{SocketAddr, ToSocketAddrs};
1514
use std::path::PathBuf;
@@ -75,9 +74,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
7574
.next()
7675
.ok_or_else(|| anyhow::anyhow!("no address found for {}", address))?;
7776
let build_dir = book.build_dir_for("html");
78-
let html_config = book.config.html_config();
79-
let input_404 = html_config.and_then(|c| c.input_404);
80-
let file_404 = get_404_output_file(&input_404);
77+
let html_config = book.config.html_config().unwrap_or_default();
78+
let file_404 = html_config.get_404_output_file();
8179

8280
// A channel used to broadcast to any websockets to reload when a file changes.
8381
let (tx, _rx) = tokio::sync::broadcast::channel::<Message>(100);

0 commit comments

Comments
 (0)