Skip to content

A fast, helpful, and open-source document parser

fix: preserve batch output directory

Jean-Claude3fc80e39ba8f
diff --git a/crates/liteparse-python/src/cli.rs b/crates/liteparse-python/src/cli.rs
index 1323d3e..a0475ad 100644
--- a/crates/liteparse-python/src/cli.rs
+++ b/crates/liteparse-python/src/cli.rs
@@ -307,10 +307,8 @@ pub fn run_cli(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
for file_path in &files {
let t0 = std::time::Instant::now();
- let rel = file_path.strip_prefix(&cmd.input_dir).unwrap_or(file_path);
- let out_path = std::path::Path::new(&cmd.output_dir)
- .join(rel)
- .with_extension(out_ext);
+ let out_path =
+ batch_output_path(file_path, &cmd.input_dir, &cmd.output_dir, out_ext);
if let Some(parent) = out_path.parent() {
std::fs::create_dir_all(parent)?;
@@ -377,6 +375,42 @@ fn collect_files(
Ok(files)
}
+fn batch_output_path(
+ file_path: &str,
+ input_dir: &str,
+ output_dir: &str,
+ out_ext: &str,
+) -> std::path::PathBuf {
+ let file_path = std::path::Path::new(file_path);
+ let rel = file_path
+ .strip_prefix(std::path::Path::new(input_dir))
+ .unwrap_or(file_path);
+
+ std::path::Path::new(output_dir)
+ .join(rel)
+ .with_extension(out_ext)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::batch_output_path;
+ use std::path::Path;
+
+ #[test]
+ fn batch_output_path_preserves_output_dir_without_trailing_slash() {
+ let out_path = batch_output_path("docs/report.pdf", "docs", "out", "txt");
+
+ assert_eq!(out_path, Path::new("out/report.txt"));
+ }
+
+ #[test]
+ fn batch_output_path_mirrors_nested_files_without_trailing_slash() {
+ let out_path = batch_output_path("docs/nested/report.pdf", "docs", "out", "md");
+
+ assert_eq!(out_path, Path::new("out/nested/report.md"));
+ }
+}
+
fn collect_files_inner(
dir: &std::path::Path,
recursive: bool,
diff --git a/crates/liteparse/src/main.rs b/crates/liteparse/src/main.rs
index a4085a0..daa2a83 100644
--- a/crates/liteparse/src/main.rs
+++ b/crates/liteparse/src/main.rs
@@ -410,11 +410,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
for file_path in &files {
let t0 = web_time::Instant::now();
- // Build output path: mirror directory structure
- let rel = file_path.strip_prefix(&cmd.input_dir).unwrap_or(file_path);
- let out_path = std::path::Path::new(&cmd.output_dir)
- .join(rel)
- .with_extension(out_ext);
+ let out_path =
+ batch_output_path(file_path, &cmd.input_dir, &cmd.output_dir, out_ext);
if let Some(parent) = out_path.parent() {
std::fs::create_dir_all(parent)?;
@@ -491,6 +488,42 @@ fn collect_files(
Ok(files)
}
+fn batch_output_path(
+ file_path: &str,
+ input_dir: &str,
+ output_dir: &str,
+ out_ext: &str,
+) -> std::path::PathBuf {
+ let file_path = std::path::Path::new(file_path);
+ let rel = file_path
+ .strip_prefix(std::path::Path::new(input_dir))
+ .unwrap_or(file_path);
+
+ std::path::Path::new(output_dir)
+ .join(rel)
+ .with_extension(out_ext)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::batch_output_path;
+ use std::path::Path;
+
+ #[test]
+ fn batch_output_path_preserves_output_dir_without_trailing_slash() {
+ let out_path = batch_output_path("docs/report.pdf", "docs", "out", "txt");
+
+ assert_eq!(out_path, Path::new("out/report.txt"));
+ }
+
+ #[test]
+ fn batch_output_path_mirrors_nested_files_without_trailing_slash() {
+ let out_path = batch_output_path("docs/nested/report.pdf", "docs", "out", "md");
+
+ assert_eq!(out_path, Path::new("out/nested/report.md"));
+ }
+}
+
fn collect_files_inner(
dir: &std::path::Path,
recursive: bool,