undoc 0.2.0

High-performance Microsoft Office document extraction to Markdown
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Quality Report Generator for undoc
//!
//! This test module generates a comprehensive quality report for all test files,
//! measuring extraction success, document statistics, and identifying issues.
//!
//! Run with: cargo test --test quality_report -- --nocapture

use std::collections::HashMap;
use std::fs;
use std::path::Path;
use undoc::{parse_bytes, Block, Document};

/// Statistics for a single document
#[derive(Debug, Default)]
struct DocumentStats {
    /// File path
    path: String,
    /// File format (docx, xlsx, pptx)
    format: String,
    /// Parse result
    success: bool,
    /// Error message if failed
    error: Option<String>,
    /// Number of sections (sheets/slides)
    section_count: usize,
    /// Number of paragraphs
    paragraph_count: usize,
    /// Number of tables
    table_count: usize,
    /// Total number of cells across all tables
    cell_count: usize,
    /// Number of merged cells (col_span > 1 or row_span > 1)
    merged_cell_count: usize,
    /// Number of hyperlinks
    hyperlink_count: usize,
    /// Number of images/resources
    image_count: usize,
    /// Number of headings
    heading_count: usize,
    /// Total text length
    text_length: usize,
    /// Warnings (partial failures, missing elements, etc.)
    warnings: Vec<String>,
}

impl DocumentStats {
    fn from_document(path: &str, doc: &Document) -> Self {
        let format = Path::new(path)
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("unknown")
            .to_lowercase();

        let mut stats = Self {
            path: path.to_string(),
            format,
            success: true,
            error: None,
            section_count: doc.sections.len(),
            image_count: doc.resources.len(),
            text_length: doc.plain_text().len(),
            ..Default::default()
        };

        for section in &doc.sections {
            for block in &section.content {
                match block {
                    Block::Paragraph(para) => {
                        stats.paragraph_count += 1;
                        if para.heading != undoc::HeadingLevel::None {
                            stats.heading_count += 1;
                        }
                        for run in &para.runs {
                            if run.hyperlink.is_some() {
                                stats.hyperlink_count += 1;
                            }
                        }
                    }
                    Block::Table(table) => {
                        stats.table_count += 1;
                        for row in &table.rows {
                            for cell in &row.cells {
                                stats.cell_count += 1;
                                if cell.col_span > 1 || cell.row_span > 1 {
                                    stats.merged_cell_count += 1;
                                }
                                // Check hyperlinks in table cells
                                for para in &cell.content {
                                    for run in &para.runs {
                                        if run.hyperlink.is_some() {
                                            stats.hyperlink_count += 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }
        }

        // Add warnings for potential issues
        if stats.section_count == 0 {
            stats
                .warnings
                .push("Empty document (no sections)".to_string());
        }
        if stats.format == "xlsx" && stats.table_count == 0 {
            stats.warnings.push("XLSX with no tables".to_string());
        }
        if stats.format == "pptx" && stats.paragraph_count == 0 {
            stats.warnings.push("PPTX with no text content".to_string());
        }

        stats
    }

    fn from_error(path: &str, error: &str) -> Self {
        let format = Path::new(path)
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("unknown")
            .to_lowercase();

        Self {
            path: path.to_string(),
            format,
            success: false,
            error: Some(error.to_string()),
            ..Default::default()
        }
    }
}

/// Aggregate statistics by format
#[derive(Debug, Default)]
struct FormatStats {
    total: usize,
    success: usize,
    failed: usize,
    total_paragraphs: usize,
    total_tables: usize,
    total_cells: usize,
    total_merged_cells: usize,
    total_hyperlinks: usize,
    total_images: usize,
    total_headings: usize,
}

/// Scan a directory recursively for Office files
fn scan_directory(dir: &Path, stats: &mut Vec<DocumentStats>) {
    if let Ok(entries) = fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                scan_directory(&path, stats);
            } else {
                let ext = path.extension().and_then(|e| e.to_str());
                if matches!(ext, Some("docx" | "xlsx" | "pptx")) {
                    let path_str = path.to_string_lossy().to_string();

                    // Known expected failures
                    let filename = path.file_name().unwrap().to_str().unwrap();
                    let expected_failure = matches!(
                        filename,
                        "badcrc.docx" | "testascii.docx" | "testutf16.docx"
                    );

                    match fs::read(&path) {
                        Ok(data) => match parse_bytes(&data) {
                            Ok(doc) => {
                                stats.push(DocumentStats::from_document(&path_str, &doc));
                            }
                            Err(e) => {
                                let mut doc_stats =
                                    DocumentStats::from_error(&path_str, &e.to_string());
                                if expected_failure {
                                    doc_stats.warnings.push("Expected failure".to_string());
                                }
                                stats.push(doc_stats);
                            }
                        },
                        Err(e) => {
                            stats.push(DocumentStats::from_error(
                                &path_str,
                                &format!("IO error: {}", e),
                            ));
                        }
                    }
                }
            }
        }
    }
}

/// Generate quality report
#[test]
fn generate_quality_report() {
    let test_dirs = ["test-files", "test-files/officedissector/test"];

    let mut all_stats: Vec<DocumentStats> = Vec::new();

    for dir in test_dirs {
        if Path::new(dir).exists() {
            scan_directory(Path::new(dir), &mut all_stats);
        }
    }

    if all_stats.is_empty() {
        println!("No test files found. Please ensure test-files directory exists.");
        return;
    }

    // Sort by path for consistent output
    all_stats.sort_by(|a, b| a.path.cmp(&b.path));

    // Calculate aggregate stats by format
    let mut by_format: HashMap<String, FormatStats> = HashMap::new();

    for stat in &all_stats {
        let format_stats = by_format.entry(stat.format.clone()).or_default();
        format_stats.total += 1;
        if stat.success {
            format_stats.success += 1;
            format_stats.total_paragraphs += stat.paragraph_count;
            format_stats.total_tables += stat.table_count;
            format_stats.total_cells += stat.cell_count;
            format_stats.total_merged_cells += stat.merged_cell_count;
            format_stats.total_hyperlinks += stat.hyperlink_count;
            format_stats.total_images += stat.image_count;
            format_stats.total_headings += stat.heading_count;
        } else {
            format_stats.failed += 1;
        }
    }

    // Print report
    println!("\n{}", "=".repeat(80));
    println!("{:^80}", "UNDOC QUALITY REPORT");
    println!("{}\n", "=".repeat(80));

    // Summary by format
    println!("## Summary by Format\n");
    println!(
        "{:<8} {:>8} {:>8} {:>8} {:>10}",
        "Format", "Total", "Success", "Failed", "Rate"
    );
    println!("{:-<50}", "");

    let total_files: usize = all_stats.len();
    let total_success: usize = all_stats.iter().filter(|s| s.success).count();

    for (format, stats) in by_format.iter() {
        let rate = if stats.total > 0 {
            (stats.success as f64 / stats.total as f64) * 100.0
        } else {
            0.0
        };
        println!(
            "{:<8} {:>8} {:>8} {:>8} {:>9.1}%",
            format.to_uppercase(),
            stats.total,
            stats.success,
            stats.failed,
            rate
        );
    }

    println!("{:-<50}", "");
    let total_rate = (total_success as f64 / total_files as f64) * 100.0;
    println!(
        "{:<8} {:>8} {:>8} {:>8} {:>9.1}%\n",
        "TOTAL",
        total_files,
        total_success,
        total_files - total_success,
        total_rate
    );

    // Statistics summary
    println!("## Content Statistics\n");
    println!(
        "{:<10} {:>12} {:>10} {:>10} {:>12} {:>10} {:>10}",
        "Format", "Paragraphs", "Tables", "Cells", "MergedCells", "Links", "Images"
    );
    println!("{:-<80}", "");

    for (format, stats) in by_format.iter() {
        println!(
            "{:<10} {:>12} {:>10} {:>10} {:>12} {:>10} {:>10}",
            format.to_uppercase(),
            stats.total_paragraphs,
            stats.total_tables,
            stats.total_cells,
            stats.total_merged_cells,
            stats.total_hyperlinks,
            stats.total_images
        );
    }
    println!();

    // Failures
    let failures: Vec<_> = all_stats.iter().filter(|s| !s.success).collect();
    if !failures.is_empty() {
        println!("## Failed Files ({})\n", failures.len());
        for stat in failures {
            let expected = stat.warnings.iter().any(|w| w.contains("Expected"));
            let marker = if expected { "[expected]" } else { "" };
            println!("- {} {}", stat.path, marker);
            if let Some(ref err) = stat.error {
                println!("  Error: {}", err);
            }
        }
        println!();
    }

    // Warnings
    let with_warnings: Vec<_> = all_stats
        .iter()
        .filter(|s| s.success && !s.warnings.is_empty())
        .collect();
    if !with_warnings.is_empty() {
        println!("## Warnings ({})\n", with_warnings.len());
        for stat in with_warnings {
            println!("- {}", stat.path);
            for warning in &stat.warnings {
                println!("  âš  {}", warning);
            }
        }
        println!();
    }

    // Feature coverage analysis
    println!("## Feature Coverage Analysis\n");

    // Check merged cells
    let xlsx_with_merged: usize = all_stats
        .iter()
        .filter(|s| s.format == "xlsx" && s.merged_cell_count > 0)
        .count();
    let xlsx_total: usize = all_stats
        .iter()
        .filter(|s| s.format == "xlsx" && s.success)
        .count();
    println!(
        "- XLSX merged cells: {}/{} files have detected merged cells",
        xlsx_with_merged, xlsx_total
    );

    // Check hyperlinks
    let with_hyperlinks: usize = all_stats
        .iter()
        .filter(|s| s.success && s.hyperlink_count > 0)
        .count();
    println!(
        "- Hyperlinks: {}/{} files have detected hyperlinks",
        with_hyperlinks, total_success
    );

    // Check headings
    let with_headings: usize = all_stats
        .iter()
        .filter(|s| s.success && s.heading_count > 0)
        .count();
    println!(
        "- Headings: {}/{} files have detected headings",
        with_headings, total_success
    );

    // PPTX headings specifically
    let pptx_with_headings: usize = all_stats
        .iter()
        .filter(|s| s.format == "pptx" && s.heading_count > 0)
        .count();
    let pptx_total: usize = all_stats
        .iter()
        .filter(|s| s.format == "pptx" && s.success)
        .count();
    println!(
        "- PPTX headings: {}/{} files have detected headings",
        pptx_with_headings, pptx_total
    );

    println!();

    // Detailed file list (optional, can be verbose)
    println!("## Detailed Results\n");
    println!(
        "{:<60} {:>6} {:>8} {:>8} {:>6} {:>6}",
        "File", "Status", "Sections", "Tables", "Links", "Imgs"
    );
    println!("{:-<100}", "");

    for stat in &all_stats {
        // Use char indices to avoid breaking UTF-8 boundaries
        let short_path = if stat.path.chars().count() > 58 {
            let chars: Vec<char> = stat.path.chars().collect();
            let start = chars.len().saturating_sub(55);
            format!("...{}", chars[start..].iter().collect::<String>())
        } else {
            stat.path.clone()
        };

        let status = if stat.success { "OK" } else { "FAIL" };
        println!(
            "{:<60} {:>6} {:>8} {:>8} {:>6} {:>6}",
            short_path,
            status,
            stat.section_count,
            stat.table_count,
            stat.hyperlink_count,
            stat.image_count
        );
    }

    // Assert minimum success rate
    assert!(
        total_rate >= 95.0,
        "Success rate should be at least 95%, got {:.1}%",
        total_rate
    );
}

/// Individual file quality check for debugging
#[test]
fn check_specific_file() {
    let files = [
        "test-files/Basic Invoice.xlsx",
        "test-files/file_example_PPT_1MB.pptx",
        "test-files/officedissector/test/unit_test/testdocs/testutf16.docx",
    ];

    for path in files {
        if !Path::new(path).exists() {
            continue;
        }

        println!("\n=== {} ===\n", path);

        match fs::read(path) {
            Ok(data) => match parse_bytes(&data) {
                Ok(doc) => {
                    let stats = DocumentStats::from_document(path, &doc);
                    println!("Sections: {}", stats.section_count);
                    println!("Paragraphs: {}", stats.paragraph_count);
                    println!("Tables: {}", stats.table_count);
                    println!("Cells: {}", stats.cell_count);
                    println!("Merged cells: {}", stats.merged_cell_count);
                    println!("Hyperlinks: {}", stats.hyperlink_count);
                    println!("Images: {}", stats.image_count);
                    println!("Headings: {}", stats.heading_count);
                    println!("Text length: {} chars", stats.text_length);

                    if !stats.warnings.is_empty() {
                        println!("\nWarnings:");
                        for w in &stats.warnings {
                            println!("  - {}", w);
                        }
                    }
                }
                Err(e) => {
                    println!("Parse error: {}", e);
                }
            },
            Err(e) => {
                println!("Read error: {}", e);
            }
        }
    }
}