From b95aa5c51128e4c87b1ab3acccca926e4ccf3d10 Mon Sep 17 00:00:00 2001 From: Coil Date: Tue, 26 Mar 2024 17:19:29 +0100 Subject: [PATCH] Use proper defines for files and directories --- src/cmd/shelf.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/cmd/shelf.rs b/src/cmd/shelf.rs index c0c10b3cf8..caab278332 100644 --- a/src/cmd/shelf.rs +++ b/src/cmd/shelf.rs @@ -11,6 +11,12 @@ use mdbook::config::Shelf; use mdbook::errors::Result; use mdbook::MDBook; +const SHELF_DIR: &str = "shelf"; +const REPOS_DIR: &str = "repositories"; +const INDEX_MD_FILE: &str = "index.md"; +const INDEX_HTML_FILE: &str = "index.html"; +const BOOKS_DIR: &str = "books"; + pub fn make_subcommand() -> Command { Command::new("shelf").about("Build a bookshelf from shelf.toml file") } @@ -23,7 +29,7 @@ fn process_book(path: &str, index_file: &mut File, summary: &mut File) -> Result // Build book let mut path = current_dir()?; let title = book.config.book.title.clone().unwrap(); - path.push("books/"); + path.push(BOOKS_DIR); path.push(title); book.config.build.build_dir = path; book.config.book.shelf_url = Some(PathBuf::from(r"../../shelf/book/index.html")); @@ -31,7 +37,10 @@ fn process_book(path: &str, index_file: &mut File, summary: &mut File) -> Result // Create post in index file let title = book.config.book.title.unwrap_or_default(); - writeln!(index_file, "## [{title}](<../../books/{title}/index.html>)")?; + writeln!( + index_file, + "## [{title}](<../../{BOOKS_DIR}/{title}/{INDEX_HTML_FILE}>)" + )?; writeln!(index_file)?; let desc = book.config.book.description.unwrap_or_default(); writeln!(index_file, "{desc}")?; @@ -57,12 +66,12 @@ pub fn execute(_args: &ArgMatches) -> Result<()> { file.read_to_string(&mut contents)?; let shelf: Shelf = toml::from_str(&contents)?; - let _ = std::fs::remove_dir_all("shelf"); - let _ = std::fs::remove_dir_all("repositories"); - let shelf_book = MDBook::init("shelf").create_gitignore(false).build()?; + let _ = std::fs::remove_dir_all(SHELF_DIR); + let _ = std::fs::remove_dir_all(REPOS_DIR); + let shelf_book = MDBook::init(SHELF_DIR).create_gitignore(false).build()?; let mut index_file_name = shelf_book.source_dir(); - index_file_name.push("index.md"); + index_file_name.push(INDEX_MD_FILE); let mut index_file = File::create(index_file_name).unwrap(); writeln!(index_file, "# Bookshelf")?; writeln!(index_file)?; @@ -71,7 +80,7 @@ pub fn execute(_args: &ArgMatches) -> Result<()> { summary_file_name.push("SUMMARY.md"); let mut summary = File::create(summary_file_name).unwrap(); writeln!(summary, "# Summary")?; - writeln!(summary, "- [Index](./index.md)")?; + writeln!(summary, "- [Index](./{INDEX_MD_FILE})")?; for sb in &shelf.book { if let Some(url) = &sb.git_url { @@ -79,7 +88,7 @@ pub fn execute(_args: &ArgMatches) -> Result<()> { let path = sb.path.clone().unwrap_or("root".to_owned()); let repo_raw_name = url.split('/').last().unwrap_or(&path); let repo_name = format!("{repo_raw_name}-{path}"); - let mut checkout_path = PathBuf::from("repositories"); + let mut checkout_path = PathBuf::from(REPOS_DIR); checkout_path.push(repo_name); let book_path = if let Some(path) = &sb.path {