format tables code

This commit is contained in:
Abimael Martell
2026-02-07 19:40:28 -08:00
parent 15981b7a15
commit 0acb3ecc8b
3 changed files with 33 additions and 21 deletions
+28 -12
View File
@@ -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)]
);
}
}
+4 -6
View File
@@ -111,7 +111,9 @@ pub fn to_markdown_from_items(items: Vec<TextItem>, 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<usize> = HashSet::new();
@@ -124,11 +126,7 @@ pub fn to_markdown_from_items(items: Vec<TextItem>, options: MarkdownOptions) ->
pages.dedup();
for page in pages {
let page_items: Vec<TextItem> = items
.iter()
.filter(|i| i.page == page)
.cloned()
.collect();
let page_items: Vec<TextItem> = items.iter().filter(|i| i.page == page).cloned().collect();
let tables = detect_tables(&page_items, base_size);
+1 -3
View File
@@ -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