diff --git a/src/cmd/shelf.rs b/src/cmd/shelf.rs index caab278332..919b6f95be 100644 --- a/src/cmd/shelf.rs +++ b/src/cmd/shelf.rs @@ -21,7 +21,12 @@ pub fn make_subcommand() -> Command { Command::new("shelf").about("Build a bookshelf from shelf.toml file") } -fn process_book(path: &str, index_file: &mut File, summary: &mut File) -> Result<()> { +fn process_book( + path: &str, + index_file: &mut File, + summary: &mut File, + shelf_source: &PathBuf, +) -> Result<()> { let book_dir = path.try_resolve()?; let book_dir = std::fs::canonicalize(book_dir)?; let mut book = MDBook::load(book_dir)?; @@ -48,7 +53,9 @@ fn process_book(path: &str, index_file: &mut File, summary: &mut File) -> Result // Create a separate chapter file for the book let fixed_title = title.replace(' ', "_"); let file_name = format!("{fixed_title}.md"); - let mut bf = File::create(format!("shelf/src/{file_name}"))?; + let mut file_path = shelf_source.clone(); + file_path.push(&file_name); + let mut bf = File::create(file_path)?; writeln!(bf, "# {title}")?; writeln!(bf)?; writeln!(bf, "{desc}")?; @@ -69,6 +76,7 @@ pub fn execute(_args: &ArgMatches) -> Result<()> { 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 shelf_source = shelf_book.source_dir(); let mut index_file_name = shelf_book.source_dir(); index_file_name.push(INDEX_MD_FILE); @@ -132,9 +140,14 @@ pub fn execute(_args: &ArgMatches) -> Result<()> { .expect("Failed to set HEAD"); } - process_book(&book_path.to_str().unwrap(), &mut index_file, &mut summary)? + process_book( + &book_path.to_str().unwrap(), + &mut index_file, + &mut summary, + &shelf_source, + )? } else if let Some(path) = &sb.path { - process_book(path, &mut index_file, &mut summary)? + process_book(path, &mut index_file, &mut summary, &shelf_source)? } else { warn!("Neither path or git specified. Invalid book"); }