From 0acb3ecc8b3339d93f3e9c417c0c65be974bb66a Mon Sep 17 00:00:00 2001 From: Abimael Martell Date: Sat, 7 Feb 2026 19:40:28 -0800 Subject: [PATCH] format tables code --- src/bin/debug_order.rs | 40 ++++++++++++++++++++++++++++------------ src/markdown.rs | 10 ++++------ src/tables.rs | 4 +--- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/bin/debug_order.rs b/src/bin/debug_order.rs index 54b6a21..406d85b 100644 --- a/src/bin/debug_order.rs +++ b/src/bin/debug_order.rs @@ -2,29 +2,45 @@ use pdf_inspector::extract_text_with_positions; fn main() { let items = extract_text_with_positions("samples/tables/doc.pdf").unwrap(); - + // Find items containing "Description" or section numbers println!("Items containing section markers:"); for item in items.iter().filter(|i| i.page == 1) { - if item.text.contains("Description") || - item.text.starts_with("3 ") || - item.text.starts_with("3 ") { + if item.text.contains("Description") + || item.text.starts_with("3 ") + || item.text.starts_with("3 ") + { println!(" x={:6.1} y={:6.1} \"{}\"", item.x, item.y, item.text); } } - + // Look at the Y range for right column - let right_col: Vec<_> = items.iter() + let right_col: Vec<_> = items + .iter() .filter(|i| i.page == 1 && i.x > 300.0 && i.x < 400.0) .collect(); - + let y_min = right_col.iter().map(|i| i.y).fold(f32::INFINITY, f32::min); - let y_max = right_col.iter().map(|i| i.y).fold(f32::NEG_INFINITY, f32::max); - println!("\nRight column (x=300-400) Y range: {:.1} to {:.1}", y_min, y_max); - + let y_max = right_col + .iter() + .map(|i| i.y) + .fold(f32::NEG_INFINITY, f32::max); + println!( + "\nRight column (x=300-400) Y range: {:.1} to {:.1}", + y_min, y_max + ); + // Show items near Y=675 (where "3 Description" appears) println!("\nItems near Y=675 (±10):"); - for item in items.iter().filter(|i| i.page == 1 && (i.y - 675.0).abs() < 10.0) { - println!(" x={:6.1} y={:6.1} \"{}\"", item.x, item.y, &item.text[..item.text.len().min(50)]); + for item in items + .iter() + .filter(|i| i.page == 1 && (i.y - 675.0).abs() < 10.0) + { + println!( + " x={:6.1} y={:6.1} \"{}\"", + item.x, + item.y, + &item.text[..item.text.len().min(50)] + ); } } diff --git a/src/markdown.rs b/src/markdown.rs index 2ea1d6b..cb05882 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -111,7 +111,9 @@ pub fn to_markdown_from_items(items: Vec, options: MarkdownOptions) -> // Calculate base font size for table detection let font_stats = calculate_font_stats_from_items(&items); - let base_size = options.base_font_size.unwrap_or(font_stats.most_common_size); + let base_size = options + .base_font_size + .unwrap_or(font_stats.most_common_size); // Detect tables on each page let mut table_items: HashSet = HashSet::new(); @@ -124,11 +126,7 @@ pub fn to_markdown_from_items(items: Vec, options: MarkdownOptions) -> pages.dedup(); for page in pages { - let page_items: Vec = items - .iter() - .filter(|i| i.page == page) - .cloned() - .collect(); + let page_items: Vec = items.iter().filter(|i| i.page == page).cloned().collect(); let tables = detect_tables(&page_items, base_size); diff --git a/src/tables.rs b/src/tables.rs index 83b5af3..76e1141 100644 --- a/src/tables.rs +++ b/src/tables.rs @@ -159,9 +159,7 @@ fn check_column_alignment(items: &[(usize, &TextItem)], columns: &[f32]) -> f32 let tolerance = 40.0; let aligned = items .iter() - .filter(|(_, item)| { - columns.iter().any(|&col| (item.x - col).abs() < tolerance) - }) + .filter(|(_, item)| columns.iter().any(|&col| (item.x - col).abs() < tolerance)) .count(); aligned as f32 / items.len() as f32