A fast, helpful, and open-source document parser
nits
Logan Markewich535a2caf153d
diff --git a/crates/liteparse/src/output/markdown.rs b/crates/liteparse/src/output/markdown.rsdeleted file mode 100644index 2224bef..0000000--- a/crates/liteparse/src/output/markdown.rs+++ /dev/null@@ -1,288 +0,0 @@-use crate::config::ImageMode;-use crate::markdown_layout::PositionedBlock;-use crate::stages;-use crate::types::{OutlineTarget, ParsedPage};--/// Format parsed pages as markdown.-///-/// Whole-document signals (body font size, heading-level map, repeating-/// header/footer set) are computed once up front, then each page is classified-/// into blocks ([`classify_page_with_filters`]) and rendered. Block classes-/// cover headings, paragraphs (with de-hyphenation and inline emphasis), lists,-/// code blocks, ruled and borderless tables, horizontal rules, and figures.-///-/// Pages are emitted in order, separated by `\n\n-----\n\n`.-/// Pages that contain no projected lines (e.g. blank-/// or fully-OCR pages without font-size info) fall back to the projected text-/// wrapped in a fenced block so we never silently drop content.-pub fn format_markdown(- pages: &[ParsedPage],- outline: &[OutlineTarget],- image_mode: ImageMode,-) -> String {- format_markdown_pages(pages, outline, image_mode, false).join("\n\n-----\n\n")-}--/// Render each page to its own markdown string, returning one entry per input-/// page (in order). [`format_markdown`] joins these with page separators to-/// build the document output; callers wanting per-page markdown (e.g. to-/// populate `ParsedPage.markdown`) use this directly. Doc-level context-/// (body size, heading map, header/footer set) is still computed across all-/// pages so a single page renders identically whether requested alone or as-/// part of the document.-///-/// `keep_headers_footers` disables running-chrome suppression entirely (both-/// the cross-page repetition set and the single-page chrome detector), so-/// repeated headers/footers and page markers stay in the output.-pub fn format_markdown_pages(- pages: &[ParsedPage],- outline: &[OutlineTarget],- image_mode: ImageMode,- keep_headers_footers: bool,-) -> Vec<String> {- let classified = classify_document(pages, outline, image_mode, keep_headers_footers);- render_classified(pages, &classified)-}--/// Classify every page into its final block sequence — the same decomposition-/// [`format_markdown_pages`] renders, after chrome suppression, rule dedup, and-/// soft-hyphen splicing.-///-/// A page yields `None` when it has no projected lines (blank, or fully-OCR-/// without font metadata): there is no structural decomposition to report, and-/// rendering falls back to fenced projection text.-///-/// Split out from rendering so callers that want the blocks *and* the markdown-/// pay for classification once, and so blocks are available in JSON and text-/// output modes, which never render markdown at all.-pub fn classify_document(- pages: &[ParsedPage],- outline: &[OutlineTarget],- image_mode: ImageMode,- keep_headers_footers: bool,-) -> Vec<Option<Vec<PositionedBlock>>> {- if pages.is_empty() {- return Vec::new();- }-- let signals = stages::document_signals(pages, keep_headers_footers);- let options = stages::BlockOptions {- outline,- image_mode,- keep_headers_footers,- };- pages- .iter()- .map(|page| stages::extract_blocks(page, &signals, &options))- .collect()-}--/// Render pre-classified pages to one markdown string per page. Pages that-/// classified to `None` fall back to their projection text inside a fence so-/// nothing is silently dropped.-pub fn render_classified(- pages: &[ParsedPage],- classified: &[Option<Vec<PositionedBlock>>],-) -> Vec<String> {- pages- .iter()- .zip(classified)- .map(|(page, blocks)| stages::render_page_markdown(page, blocks.as_deref()))- .collect()-}--/// Collapse cosmetic horizontal-rule noise on a single page's block stream:-/// drop leading/trailing rules (which would otherwise abut the `-----` page-/// separator) and collapse runs of consecutive rules to one. Rules come from-/// two sources — vector-graphics detection and decorative divider text — and-/// doubling up reads as sloppy output to a human, while carrying no extra-/// structure for an LLM.-pub(crate) fn dedupe_rules(blocks: &mut Vec<crate::markdown_layout::PositionedBlock>) {- use crate::markdown_layout::Block::HorizontalRule;- while matches!(blocks.first().map(|b| &b.block), Some(HorizontalRule)) {- blocks.remove(0);- }- while matches!(blocks.last().map(|b| &b.block), Some(HorizontalRule)) {- blocks.pop();- }- blocks.dedup_by(|a, b| matches!((&a.block, &b.block), (HorizontalRule, HorizontalRule)));-}--#[cfg(test)]-mod tests {- use super::*;- use crate::types::{Anchor, ProjectedLine, Rect, TextItem};-- fn line(text: &str, x: f32, y: f32, h: f32, size: f32) -> ProjectedLine {- ProjectedLine {- text: text.into(),- rtl: crate::bidi::is_rtl_text(text),- bbox: Rect {- x,- y,- width: text.chars().count() as f32 * (size * 0.5),- height: h,- },- anchor: Anchor::Left,- indent_x: x,- dominant_font_size: size,- font_size_is_estimated: false,- heading_font_size: None,- dominant_font_name: Some("Arial".into()),- all_bold: false,- all_italic: false,- all_mono: false,- all_strike: false,- spans: vec![TextItem::default()],- region_path: Vec::new(),- mcid: None,- in_figure: false,- }- }-- fn page_with(n: usize, lines: Vec<ProjectedLine>) -> ParsedPage {- ParsedPage {- page_number: n,- page_label: None,- page_width: 612.0,- page_height: 792.0,- content_bounds: None,- text: "fallback".into(),- markdown: String::new(),- text_items: vec![],- projected_lines: lines,- regions: crate::types::Region::default(),- graphics: vec![],- vector_graphics: None,- figures: vec![],- projected_item_frames: vec![],- struct_nodes: vec![],- image_refs: vec![],- complexity: None,- annotations: None,- form_fields: None,- structure_tree: None,- blocks: None,- }- }-- #[test]- fn test_empty() {- assert_eq!(format_markdown(&[], &[], ImageMode::Placeholder), "");- }-- #[test]- fn dedupe_rules_drops_edges_and_collapses_runs() {- use crate::markdown_layout::Block::{self, HorizontalRule, Paragraph};- let p = |t: &str| Paragraph {- text: t.into(),- bold: false,- italic: false,- };- let mut blocks: Vec<crate::markdown_layout::PositionedBlock> = [- HorizontalRule,- p("a"),- HorizontalRule,- HorizontalRule,- p("b"),- HorizontalRule,- ]- .into_iter()- .map(crate::markdown_layout::PositionedBlock::unlocated)- .collect();- dedupe_rules(&mut blocks);- let kinds: Vec<bool> = blocks- .iter()- .map(|b| matches!(b.block, Block::HorizontalRule))- .collect();- // Leading + trailing rules gone; the doubled interior run collapsed to one.- assert_eq!(kinds, vec![false, true, false]);- }-- #[test]- fn test_fallback_when_no_projected_lines() {- let p = ParsedPage {- page_number: 1,- page_label: None,- page_width: 0.0,- page_height: 0.0,- content_bounds: None,- text: "hello".into(),- markdown: String::new(),- text_items: vec![],- projected_lines: vec![],- regions: crate::types::Region::default(),- graphics: vec![],- vector_graphics: None,- figures: vec![],- projected_item_frames: vec![],- struct_nodes: vec![],- image_refs: vec![],- complexity: None,- annotations: None,- form_fields: None,- structure_tree: None,- blocks: None,- };- let out = format_markdown(&[p], &[], ImageMode::Placeholder);- assert!(out.contains("```text"));- assert!(out.contains("hello"));- }-- #[test]- fn test_heading_and_paragraph() {- let p = page_with(- 1,- vec![- line("My Title For This Test Document", 50.0, 50.0, 18.0, 18.0),- // Enough body text to dominate the char-weighted body-size- // mode so the title at 18pt registers as larger-than-body.- line("First sentence of body prose here.", 50.0, 80.0, 10.0, 10.0),- line(- "Second sentence of body prose here.",- 50.0,- 92.0,- 10.0,- 10.0,- ),- line(- "Third sentence of body prose here.",- 50.0,- 104.0,- 10.0,- 10.0,- ),- ],- );- let out = format_markdown(&[p], &[], ImageMode::Placeholder);- assert!(out.contains("# My Title For This Test Document"));- assert!(out.contains("First sentence of body prose here."));- }-- #[test]- fn test_multi_page_separator() {- let a = page_with(1, vec![line("A page.", 50.0, 80.0, 10.0, 10.0)]);- let b = page_with(2, vec![line("B page.", 50.0, 80.0, 10.0, 10.0)]);- let out = format_markdown(&[a, b], &[], ImageMode::Placeholder);- assert!(out.contains("-----"));- assert!(out.find("A page.").unwrap() < out.find("B page.").unwrap());- }-- #[test]- fn test_per_page_matches_joined_document() {- let a = page_with(1, vec![line("A page.", 50.0, 80.0, 10.0, 10.0)]);- let b = page_with(2, vec![line("B page.", 50.0, 80.0, 10.0, 10.0)]);- let pages = [a, b];- let per_page = format_markdown_pages(&pages, &[], ImageMode::Placeholder, false);- assert_eq!(per_page.len(), 2);- assert!(per_page[0].contains("A page."));- assert!(per_page[1].contains("B page."));- // The per-page strings carry no separator on their own; the document- // form is exactly the join.- assert!(!per_page[0].contains("-----"));- assert_eq!(- format_markdown(&pages, &[], ImageMode::Placeholder),- per_page.join("\n\n-----\n\n")- );- }-}diff --git a/crates/liteparse/src/stages.rs b/crates/liteparse/src/stages.rsindex b9006b5..08d4c47 100644--- a/crates/liteparse/src/stages.rs+++ b/crates/liteparse/src/stages.rs@@ -8,18 +8,6 @@//! itself and insert its own inputs between stages — its own OCR engine, a//! page selection, extra blocks — or put a process boundary between any two.//!-//! Two invariants hold this together:-//!-//! * `parse()` is implemented *only* through these functions (see-//! `parser.rs`), and an integration test composes them by hand and asserts-//! the result equals `parse()`. A step cannot be added to `parse()` without-//! becoming a public stage.-//! * Every type crossing a stage boundary is `Clone + Serialize +-//! Deserialize`, and the serialization is lossless for everything a later-//! stage reads. The two exceptions are payloads that are better carried-//! out of band: `ExtractedImage::bytes` is skipped (key it by image id),-//! and `OcrRaster::pixels` is base64 so JSON stays ~1.3× the raw size.-//!//! Stages split into two kinds. **pdfium-bound** stages take an open//! [`Document`] and must run while the [`Library`] that opened it is alive//! (PDFium is single-threaded behind a process-global lock, so keep these@@ -40,8 +28,6 @@//!//! [`Document`] and [`Library`] are the `pdfium` crate's types re-exported//! verbatim, so that crate's handle API is part of this module's contract.-//!-//! Everything else in the crate that is not re-exported here is unstable.use std::collections::{HashMap, HashSet};use std::sync::Arc;