with file name as output dir

This commit is contained in:
Maverick Liu 2025-02-23 16:34:48 +08:00
parent f8189b5d6d
commit b183e611e8
5 changed files with 12 additions and 7 deletions

2
Cargo.lock generated
View file

@ -192,7 +192,7 @@ dependencies = [
[[package]] [[package]]
name = "epub2mdbook" name = "epub2mdbook"
version = "0.10.0" version = "0.11.0"
dependencies = [ dependencies = [
"clap", "clap",
"epub", "epub",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "epub2mdbook" name = "epub2mdbook"
version = "0.10.0" version = "0.11.0"
edition = "2024" edition = "2024"
description = "A tool to convert EPUB files to MDBook format" description = "A tool to convert EPUB files to MDBook format"
authors = ["Maverick Liu <maverick.liu42@gmail.com>"] authors = ["Maverick Liu <maverick.liu42@gmail.com>"]

View file

@ -16,5 +16,5 @@ epub2mdbook --input-epub path/to/input.epub --output-dir path/to/output
```rust ```rust
use epub2mdbook::convert_epub_to_mdbook; use epub2mdbook::convert_epub_to_mdbook;
convert_epub_to_mdbook("path/to/input.epub", Some("path/to/output")); convert_epub_to_mdbook("path/to/input.epub", Some("path/to/output"), true);
``` ```

View file

@ -16,10 +16,12 @@ use std::{fs, io};
/// ///
/// * `epub_path` - The path to the EPUB file /// * `epub_path` - The path to the EPUB file
/// * `output_dir` - The path to the output directory, working directory by default /// * `output_dir` - The path to the output directory, working directory by default
/// * `with_file_name` - Whether to use the file name as the output directory
/// ///
pub fn convert_epub_to_mdbook( pub fn convert_epub_to_mdbook(
epub_path: impl AsRef<Path>, epub_path: impl AsRef<Path>,
output_dir: Option<impl AsRef<Path>>, output_dir: Option<impl AsRef<Path>>,
with_file_name: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
let epub_path = epub_path.as_ref(); let epub_path = epub_path.as_ref();
if !epub_path.is_file() { if !epub_path.is_file() {
@ -31,10 +33,13 @@ pub fn convert_epub_to_mdbook(
.expect("unreachable") .expect("unreachable")
.to_string_lossy() .to_string_lossy()
.to_string(); .to_string();
let output_dir = match output_dir { let mut output_dir = match output_dir {
Some(output_dir) => output_dir.as_ref().join(&book_name), Some(output_dir) => output_dir.as_ref().to_owned(),
None => PathBuf::from(".").join(&book_name), None => PathBuf::from("."),
}; };
if with_file_name {
output_dir.push(&book_name);
}
fs::create_dir_all(output_dir.join("src"))?; fs::create_dir_all(output_dir.join("src"))?;
let mut epub_doc = EpubDoc::new(epub_path)?; let mut epub_doc = EpubDoc::new(epub_path)?;

View file

@ -15,7 +15,7 @@ struct Args {
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
let args = Args::parse(); let args = Args::parse();
convert_epub_to_mdbook(args.input_epub, args.output_dir)?; convert_epub_to_mdbook(args.input_epub, args.output_dir, true)?;
println!("Conversion completed successfully!"); println!("Conversion completed successfully!");
Ok(()) Ok(())
} }