kopitiam-document 0.1.0

Structural document reconstruction (paragraphs, headings, tables, columns) for KOPITIAM's Document Engine.
Documentation
mod report;

pub use report::ConversionReport;

use kopitiam_pdf::Page;

use crate::{Block, Document};

/// Compares what was extracted against what was rendered, and tallies the
/// block types found, so every conversion produces an auditable report
/// rather than a silent best-effort guess.
pub fn validate(pages: &[Page], document: &Document, rendered_markdown: &str) -> ConversionReport {
    let extracted_words = pages
        .iter()
        .flat_map(|page| &page.spans)
        .map(|span| word_count(&span.text))
        .sum();

    let mut headings_found = 0;
    let mut lists_found = 0;
    let mut tables_found = 0;

    for block in &document.blocks {
        match block {
            Block::Heading(_) => headings_found += 1,
            Block::List(_) => lists_found += 1,
            Block::Table(_) => tables_found += 1,
            _ => {}
        }
    }

    ConversionReport {
        pages: pages.len(),
        extracted_words,
        rendered_words: word_count(rendered_markdown),
        headings_found,
        lists_found,
        tables_found,
        citations_found: document.citations.len(),
    }
}

fn word_count(text: &str) -> usize {
    text.split_whitespace().count()
}