adjust api

This commit is contained in:
Maverick Liu 2025-02-23 17:50:20 +08:00
parent b183e611e8
commit 7deafd0f81
4 changed files with 13 additions and 16 deletions

2
Cargo.lock generated
View file

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

View file

@ -1,6 +1,6 @@
[package] [package]
name = "epub2mdbook" name = "epub2mdbook"
version = "0.11.0" version = "0.12.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

@ -15,15 +15,15 @@ use std::{fs, io};
/// # Arguments /// # Arguments
/// ///
/// * `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
/// * `with_file_name` - Whether to use the file name as the output directory /// * `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: impl AsRef<Path>,
with_file_name: bool, with_file_name: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
let epub_path = epub_path.as_ref(); let epub_path = epub_path.as_ref();
let output_dir = output_dir.as_ref();
if !epub_path.is_file() { if !epub_path.is_file() {
return Err(Error::NotAFile(epub_path.display().to_string())); return Err(Error::NotAFile(epub_path.display().to_string()));
} }
@ -33,13 +33,11 @@ pub fn convert_epub_to_mdbook(
.expect("unreachable") .expect("unreachable")
.to_string_lossy() .to_string_lossy()
.to_string(); .to_string();
let mut output_dir = match output_dir { let output_dir = if with_file_name {
Some(output_dir) => output_dir.as_ref().to_owned(), output_dir.join(&book_name)
None => PathBuf::from("."), } else {
output_dir.to_owned()
}; };
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)?;
@ -118,8 +116,7 @@ fn extract_chapters_and_resources<R: Read + Seek>(
.iter() .iter()
.filter_map(|(k, v)| Some((k.file_name()?, v.file_name()?))) .filter_map(|(k, v)| Some((k.file_name()?, v.file_name()?)))
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
let output_dir = output_dir.as_ref(); let src_dir = output_dir.as_ref().join("src");
let src_dir = output_dir.join("src");
for (_, (path, _)) in epub_doc.resources.clone().into_iter() { for (_, (path, _)) in epub_doc.resources.clone().into_iter() {
let mut content = match epub_doc.get_resource_by_path(&path) { let mut content = match epub_doc.get_resource_by_path(&path) {
Some(content) => content, Some(content) => content,

View file

@ -9,8 +9,8 @@ struct Args {
#[clap(short, long)] #[clap(short, long)]
input_epub: PathBuf, input_epub: PathBuf,
/// The path to the output directory, working directory by default /// The path to the output directory, working directory by default
#[clap(short, long)] #[clap(short, long, default_value = ".")]
output_dir: Option<PathBuf>, output_dir: PathBuf,
} }
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {