Skip to content

A fast, helpful, and open-source document parser

fix: extract word boxes when extract_blocks is on

Christopher Atlan08eb97465380
diff --git a/crates/liteparse/src/config.rs b/crates/liteparse/src/config.rs
index 65ba55e..5929c44 100644
--- a/crates/liteparse/src/config.rs
+++ b/crates/liteparse/src/config.rs
@@ -253,16 +253,16 @@ impl LiteParseConfig {
}
/// Whether per-word boxes are needed for this run, whichever stage
- /// produces the text. True when `emit_word_boxes` is set, or when the
- /// markdown output format is selected: the markdown table detector splits
- /// PDFium's merged multi-cell runs on real word geometry, so it needs
- /// word boxes even when the caller didn't ask for them.
+ /// produces the text. True when `emit_word_boxes` is set, or when blocks
+ /// are classified (markdown output or `extract_blocks`): the table
+ /// detector splits PDFium's merged multi-cell runs on real word geometry,
+ /// so it needs word boxes even when the caller didn't ask for them.
///
/// Every stage that fills `TextItem.words` must gate on this, so a run
/// with word boxes off stays allocation-free on the whole path — including
/// the OCR merge, where native text and scanned pages must agree.
pub fn effective_emit_word_boxes(&self) -> bool {
- self.emit_word_boxes || self.output_format == OutputFormat::Markdown
+ self.emit_word_boxes || self.output_format == OutputFormat::Markdown || self.extract_blocks
}
}
@@ -453,6 +453,15 @@ mod tests {
assert!(!c.extract_structure_tree);
}
+ #[test]
+ fn extract_blocks_extracts_word_boxes() {
+ let c = LiteParseConfig {
+ extract_blocks: true,
+ ..LiteParseConfig::default()
+ };
+ assert!(c.effective_emit_word_boxes());
+ }
+
#[test]
fn test_output_format_lowercase_serde() {
let s = serde_json::to_string(&OutputFormat::Json).unwrap();
diff --git a/crates/liteparse/src/stages.rs b/crates/liteparse/src/stages.rs
index 7b46a32..de1b62a 100644
--- a/crates/liteparse/src/stages.rs
+++ b/crates/liteparse/src/stages.rs
@@ -344,6 +344,10 @@ pub struct BlockOptions<'a> {
/// decomposition [`render_page_markdown`] renders and
/// `ParsedPage::blocks` reports.
///
+/// Table detection splits merged cells on `TextItem.words`, so extract with
+/// `LiteParseConfig::effective_emit_word_boxes` to get the blocks `parse()`
+/// reports.
+///
/// Returns `None` for a page with no projected lines (blank, or fully OCR
/// without font metadata): there is no structure to report, and rendering
/// falls back to the projection text in a fence.
diff --git a/crates/liteparse/tests/block_word_boxes.rs b/crates/liteparse/tests/block_word_boxes.rs
new file mode 100644
index 0000000..4c5b326
--- /dev/null
+++ b/crates/liteparse/tests/block_word_boxes.rs
@@ -0,0 +1,38 @@
+//! `extract_blocks` must report the same tables under every output format.
+use liteparse::config::OutputFormat;
+use liteparse::types::PdfInput;
+use liteparse::{LiteParse, LiteParseConfig};
+
+async fn tables(output_format: OutputFormat) -> serde_json::Value {
+ let parser = LiteParse::new(LiteParseConfig {
+ ocr_enabled: false,
+ quiet: true,
+ extract_blocks: true,
+ output_format,
+ target_pages: Some("39".into()),
+ ..LiteParseConfig::default()
+ });
+ let result = parser
+ .parse_input(PdfInput::Path(
+ "../../demo/docs/apple-10k-2024.pdf".to_string(),
+ ))
+ .await
+ .unwrap();
+ let blocks = result.pages[0].blocks.as_ref().unwrap();
+ serde_json::to_value(
+ blocks
+ .iter()
+ .filter(|b| b.kind.contains("table"))
+ .collect::<Vec<_>>(),
+ )
+ .unwrap()
+}
+
+/// Page 39's fair-value table packs several amounts into single PDFium runs;
+/// splitting them right needs word boxes, which markdown output always had.
+#[tokio::test]
+async fn json_and_markdown_report_the_same_tables() {
+ let json = tables(OutputFormat::Json).await;
+ assert!(!json.as_array().unwrap().is_empty());
+ assert_eq!(json, tables(OutputFormat::Markdown).await);
+}