From cc72c89f0cfcaf976f4322c7756fcbede119336d Mon Sep 17 00:00:00 2001 From: lifegpc Date: Wed, 3 Sep 2025 22:59:22 +0800 Subject: [PATCH] Add a new option --- src/args.rs | 3 +++ src/ext/mod.rs | 1 + src/ext/path.rs | 14 ++++++++++++++ src/main.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/ext/path.rs diff --git a/src/args.rs b/src/args.rs index 8a38ffe..14e4ece 100644 --- a/src/args.rs +++ b/src/args.rs @@ -83,6 +83,9 @@ pub struct Arg { #[arg(short = 'T', long, value_enum, global = true)] /// Output script type pub output_type: Option, + #[arg(short = 'n', long, global = true)] + /// Disable extra extension when locating/export output script + pub output_no_extra_ext: bool, #[cfg(feature = "image")] #[arg(short = 'i', long, value_enum, global = true)] /// Output image type diff --git a/src/ext/mod.rs b/src/ext/mod.rs index d237417..0a318d1 100644 --- a/src/ext/mod.rs +++ b/src/ext/mod.rs @@ -3,6 +3,7 @@ pub mod atomic; #[cfg(feature = "fancy-regex")] pub mod fancy_regex; pub mod io; +pub mod path; #[cfg(feature = "emote-psb")] pub mod psb; #[cfg(feature = "markup5ever_rcdom")] diff --git a/src/ext/path.rs b/src/ext/path.rs new file mode 100644 index 0000000..fc1c585 --- /dev/null +++ b/src/ext/path.rs @@ -0,0 +1,14 @@ +//! Extensions for std::path + +pub trait PathBufExt { + /// Remove all extensions from the path. + fn remove_all_extensions(&mut self); +} + +impl PathBufExt for std::path::PathBuf { + fn remove_all_extensions(&mut self) { + while self.extension().is_some() { + self.set_extension(""); + } + } +} diff --git a/src/main.rs b/src/main.rs index e3a16e3..9caa535 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ pub mod scripts; pub mod types; pub mod utils; +use ext::path::PathBufExt; use scripts::base::ArchiveContent; fn get_encoding( @@ -542,6 +543,9 @@ pub fn export_script( continue; } let mut out_path = std::path::PathBuf::from(&odir).join(f.name()); + if arg.output_no_extra_ext { + out_path.remove_all_extensions(); + } out_path.set_extension(if of.is_custom() { script_file.custom_output_extension() } else { @@ -729,7 +733,11 @@ pub fn export_script( if !arg.image_output_flat { if let Some(fname) = f.file_name() { pb.push(fname); - pb.set_extension(""); + if arg.output_no_extra_ext { + pb.remove_all_extensions(); + } else { + pb.set_extension(""); + } } pb.push(img_data.name); } else { @@ -752,6 +760,11 @@ pub fn export_script( )); } else { pb.push(img_data.name); + if arg.output_no_extra_ext { + pb.remove_all_extensions(); + } else { + pb.set_extension(""); + } } pb.set_extension(out_type.as_ref()); pb.to_string_lossy().into_owned() @@ -767,7 +780,11 @@ pub fn export_script( img_data.name )); } else { - pb.set_extension(""); + if arg.output_no_extra_ext { + pb.remove_all_extensions(); + } else { + pb.set_extension(""); + } pb.push(img_data.name); } pb.set_extension(out_type.as_ref()); @@ -813,6 +830,9 @@ pub fn export_script( if let Some(fname) = f.file_name() { pb.push(fname); } + if arg.output_no_extra_ext { + pb.remove_all_extensions(); + } pb.set_extension(out_type.as_ref()); pb.to_string_lossy().into_owned() } else { @@ -821,6 +841,9 @@ pub fn export_script( } None => { let mut pb = std::path::PathBuf::from(filename); + if arg.output_no_extra_ext { + pb.remove_all_extensions(); + } pb.set_extension(out_type.as_ref()); pb.to_string_lossy().into_owned() } @@ -866,6 +889,9 @@ pub fn export_script( if let Some(fname) = f.file_name() { pb.push(fname); } + if arg.output_no_extra_ext { + pb.remove_all_extensions(); + } pb.set_extension(ext); pb.to_string_lossy().into_owned() } else { @@ -874,6 +900,9 @@ pub fn export_script( } None => { let mut pb = std::path::PathBuf::from(filename); + if arg.output_no_extra_ext { + pb.remove_all_extensions(); + } pb.set_extension(ext); pb.to_string_lossy().into_owned() } @@ -1007,6 +1036,9 @@ pub fn import_script( of = script_file.default_output_script_type(); } let mut out_path = std::path::PathBuf::from(&odir).join(f.name()); + if arg.output_no_extra_ext { + out_path.remove_all_extensions(); + } let ext = if of.is_custom() { script_file.custom_output_extension() } else { @@ -1261,6 +1293,9 @@ pub fn import_script( if let Some(fname) = f.file_name() { pb.push(fname); } + if arg.output_no_extra_ext { + pb.remove_all_extensions(); + } pb.set_extension(out_type.as_ref()); pb.to_string_lossy().into_owned() } else { @@ -1303,6 +1338,9 @@ pub fn import_script( if let Some(fname) = f.file_name() { pb.push(fname); } + if arg.output_no_extra_ext { + pb.remove_all_extensions(); + } pb.set_extension(of.as_ref()); pb.to_string_lossy().into_owned() } else {