Skip to content

Commit

Permalink
Use shelf source in process_book
Browse files Browse the repository at this point in the history
  • Loading branch information
Coi-l committed Mar 26, 2024
1 parent b95aa5c commit c0b83d0
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/cmd/shelf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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}")?;
Expand All @@ -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);
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit c0b83d0

Please sign in to comment.