A fast, helpful, and open-source document parser
wip
Logan Markewich6502f5f842bc
diff --git a/crates/liteparse-ooxml/src/pptx/mod.rs b/crates/liteparse-ooxml/src/pptx/mod.rsindex 80340ea..df27e38 100644--- a/crates/liteparse-ooxml/src/pptx/mod.rs+++ b/crates/liteparse-ooxml/src/pptx/mod.rs@@ -28,7 +28,7 @@ pub use shapes::{};pub use text::{AutoNumberScheme, Bullet, ListStyle, Spacing, TextBody, TextParagraph, TextParagraphProperties,- TextStyles, parse_default_text_style, parse_text_body, parse_text_styles,+ TextStyles, parse_default_text_style, parse_diagram_text, parse_text_body, parse_text_styles,};pub use textcascade::{DeckTextDefaults, PlaceholderTextStyles, ResolvedTextStyle, SizeSource, TextCascade,diff --git a/crates/liteparse-ooxml/src/pptx/shapes.rs b/crates/liteparse-ooxml/src/pptx/shapes.rsindex 4de6be9..23cd50f 100644--- a/crates/liteparse-ooxml/src/pptx/shapes.rs+++ b/crates/liteparse-ooxml/src/pptx/shapes.rs@@ -208,7 +208,17 @@ pub struct GraphicFrame {pub enum GraphicFramePayload {/// A DrawingML table (`a:tbl`) — 36 of 102 corpus frames.Table(Box<Table>),- /// SmartArt (53), embedded OLE (11) or a chart (2).+ /// SmartArt (53 corpus frames), which does not hold its own content: the+ /// frame carries only `dgm:relIds`, and the text lives in the data part+ /// that `@r:dm` points at (`ppt/diagrams/data*.xml`). Resolving the+ /// relationship needs the owning [`Part`](crate::pptx::Part), which this+ /// layer does not have, so the id is surfaced for the caller to resolve.+ ///+ /// Worth its own variant rather than `Unsupported`: on the corpus all 53+ /// parts carry text, 12,102 characters of it, and a shape walk alone can+ /// never see any of it.+ Diagram { data_rel: String },+ /// Embedded OLE (11) or a chart (2) — payloads we genuinely do not read.////// Kept as a typed variant rather than dropped so that a caller can emit a/// placeholder and a probe can *count* what is being lost. Silently@@ -711,6 +721,17 @@ struct GraphicDataXml {uri: String,#[serde(rename = "tbl", default)]tbl: Option<TblXml>,+ #[serde(rename = "relIds", default)]+ rel_ids: Option<DgmRelIdsXml>,+}++/// §21.4.2.19 `dgm:relIds`. Four relationship ids; `@r:dm` is the data model,+/// the only one carrying text (`@r:lo` layout, `@r:qs` quick style,+/// `@r:cs` colours are all presentation).+#[derive(Debug, Deserialize, Default)]+struct DgmRelIdsXml {+ #[serde(rename = "@dm", default)]+ dm: Option<String>,}#[derive(Debug, Deserialize, Default)]@@ -917,9 +938,10 @@ fn lower_graphic_frame(gf: GraphicFrameXml) -> Shape {.unwrap_or_default().graphic_data.unwrap_or_default();- let payload = match data.tbl {- Some(tbl) => GraphicFramePayload::Table(Box::new(lower_table(tbl))),- None => GraphicFramePayload::Unsupported { uri: data.uri },+ let payload = match (data.tbl, data.rel_ids.and_then(|r| r.dm)) {+ (Some(tbl), _) => GraphicFramePayload::Table(Box::new(lower_table(tbl))),+ (None, Some(dm)) if !dm.is_empty() => GraphicFramePayload::Diagram { data_rel: dm },+ _ => GraphicFramePayload::Unsupported { uri: data.uri },};Shape {non_visual: doc_properties(gf.nv_graphic_frame_pr.map(|nv| nv.cnv_pr)),diff --git a/crates/liteparse-ooxml/src/pptx/text.rs b/crates/liteparse-ooxml/src/pptx/text.rsindex 286b83f..2338357 100644--- a/crates/liteparse-ooxml/src/pptx/text.rs+++ b/crates/liteparse-ooxml/src/pptx/text.rs@@ -330,6 +330,57 @@ pub fn parse_text_styles(data: &[u8]) -> Result<TextStyles> {.unwrap_or_default())}+/// Parse the text of a **whole SmartArt data part** (`ppt/diagrams/data*.xml`).+///+/// SmartArt is the one payload whose content is not in the slide at all: the+/// `p:graphicFrame` carries only `dgm:relIds`, and `@r:dm` points here. A+/// shape-tree walk therefore cannot see any of it — on the corpus that is+/// **53 parts, all 53 carrying text, 12,102 characters**, which made it the+/// largest single content gap for a native reader.+///+/// Returns one [`TextBody`] per `dgm:pt` that has one, in document order.+/// Nothing is filtered: the corpus says every text-bearing point is untyped+/// (334 of 334), so the `doc` and `pres` presentation nodes contribute nothing+/// and there is no duplication to guard against — 2 repeated strings across+/// the whole corpus, both genuine.+///+/// The caller resolves the relationship, because relationship resolution needs+/// the owning [`Part`](crate::pptx::Part) and this layer only sees bytes.+pub fn parse_diagram_text(data: &[u8]) -> Result<Vec<TextBody>> {+ let xml: DiagramDataXml = serde_xml::from_xml(data)?;+ Ok(xml+ .pt_lst+ .map(|l| l.points)+ .unwrap_or_default()+ .into_iter()+ .filter_map(|pt| pt.text)+ .map(TextBodyXml::into_model)+ .filter(|b| !b.is_empty())+ .collect())+}++/// §21.4.2.11 `dgm:dataModel`.+#[derive(Debug, Deserialize, Default)]+struct DiagramDataXml {+ #[serde(rename = "ptLst", default)]+ pt_lst: Option<DiagramPtLstXml>,+}++#[derive(Debug, Deserialize, Default)]+struct DiagramPtLstXml {+ #[serde(rename = "pt", default)]+ points: Vec<DiagramPtXml>,+}++/// §21.4.2.25 `dgm:pt`. `dgm:t` is an ordinary DrawingML text body, so the+/// same lowering serves it — the grammar is shared even though the namespace+/// is not.+#[derive(Debug, Deserialize, Default)]+struct DiagramPtXml {+ #[serde(rename = "t", default)]+ text: Option<TextBodyXml>,+}+/// Parse `p:defaultTextStyle` out of a **whole `ppt/presentation.xml` part**.////// The outermost rung, and the only one a non-placeholder shape can reach:diff --git a/crates/liteparse/Cargo.toml b/crates/liteparse/Cargo.tomlindex 5b95cbb..31ce271 100644--- a/crates/liteparse/Cargo.toml+++ b/crates/liteparse/Cargo.toml@@ -18,8 +18,12 @@ path = "src/main.rs"name = "docx_blocks"required-features = ["docx-native"]+[[example]]+name = "pptx_blocks"+required-features = ["pptx-native"]+[features]-default = ["tesseract", "docx-native"]+default = ["tesseract", "docx-native", "pptx-native"]oar-ocr = ["dep:oar-ocr"]oar-ocr-auto-download = ["oar-ocr", "oar-ocr/auto-download"]oar-ocr-cuda = ["oar-ocr", "oar-ocr/cuda"]@@ -29,8 +33,13 @@ oar-ocr-coreml = ["oar-ocr", "oar-ocr/coreml"]oar-ocr-webgpu = ["oar-ocr", "oar-ocr/webgpu"]oar-ocr-openvino = ["oar-ocr", "oar-ocr/openvino"]tesseract = ["dep:tesseract-rs"]-# Native DOCX structure reading (no LibreOffice, no projection).-docx-native = ["dep:liteparse-ooxml", "liteparse-ooxml/raster"]+# Native office structure reading (no LibreOffice, no projection).+# `office-native` is an internal marker enabled by every native-office reader, so code shared+# between them (office::inline, the `apply_link` re-export) has one cfg to+# name instead of an `any(...)` list that grows with each format.+office-native = []+docx-native = ["dep:liteparse-ooxml", "liteparse-ooxml/raster", "office-native"]+pptx-native = ["dep:liteparse-ooxml", "office-native"][dependencies]blake3 = "1"diff --git a/crates/liteparse/src/markdown_layout/mod.rs b/crates/liteparse/src/markdown_layout/mod.rsindex c06cd2e..6f38962 100644--- a/crates/liteparse/src/markdown_layout/mod.rs+++ b/crates/liteparse/src/markdown_layout/mod.rs@@ -21,7 +21,7 @@ mod tables;pub use blocks::{Block, Cell, render_blocks};pub use classify::classify_page_with_filters;pub use headings::{build_heading_map, compute_body_size};-#[cfg(feature = "docx-native")]+#[cfg(feature = "office-native")]pub(crate) use inline::apply_link;pub use inline::escape_inline;pub use repetition::{compute_header_footer_set, detect_single_page_chrome};diff --git a/crates/liteparse/src/office/docx.rs b/crates/liteparse/src/office/docx.rsindex a7e25bf..9cbf01f 100644--- a/crates/liteparse/src/office/docx.rs+++ b/crates/liteparse/src/office/docx.rs@@ -21,7 +21,8 @@ use liteparse_ooxml::model::{use liteparse_ooxml::render::resolve::ResolvedDocument;use crate::error::LiteParseError;-use crate::markdown_layout::{Block, Cell, apply_link, escape_inline};+use crate::markdown_layout::{Block, Cell, escape_inline};+use crate::office::inline::{Chunk, Fmt, render_chunks};/// What the emitter should produce beyond the always-on structure.#[derive(Default)]@@ -125,29 +126,6 @@ pub fn emit_with_sources(doc: &ResolvedDocument, opts: EmitOptions) -> Vec<(Blocout}-/// Effective character formatting after the style cascade is applied.-#[derive(Default, Clone, Copy, PartialEq)]-struct Fmt {- bold: bool,- italic: bool,- strike: bool,-}--impl Fmt {- fn is_plain(self) -> bool {- !self.bold && !self.italic && !self.strike- }-}--/// A formatting-tagged run of text, plus the external URL of the hyperlink it-/// sits inside, if any.-#[derive(Clone, PartialEq)]-struct Chunk {- fmt: Fmt,- link: Option<String>,- text: String,-}-struct Emitter<'a> {doc: &'a ResolvedDocument,/// `(numId, level)` → next counter, so ordered lists number correctly and@@ -554,104 +532,6 @@ impl<'a> Emitter<'a> {}}-/// Render formatting-tagged chunks to the `(text, bold, italic)` shape-/// `Block::Paragraph` and `Block::ListItem` expect.-///-/// When every chunk shares one plain-or-emphasised style the block-level flags-/// carry it and the text stays clean; otherwise emphasis is baked inline and-/// the flags are cleared. This mirrors `paragraph_from_accum` on the PDF path,-/// so the same prose looks the same whichever pipeline produced it.-fn render_chunks(chunks: &[Chunk], escape: bool) -> (String, bool, bool) {- // Coalesce adjacent runs that share formatting and link first. Word splits- // a run at every property change, including ones that don't survive the- // cascade (language, spell-check state, rsid), so a single bold phrase- // routinely arrives as several identically-formatted chunks. Emitting each- // one separately would produce `**a****b**`, which no markdown parser- // reads as one bold span.- let mut merged: Vec<Chunk> = Vec::new();- for c in chunks {- match merged.last_mut() {- Some(prev) if prev.fmt == c.fmt && prev.link == c.link => prev.text.push_str(&c.text),- _ => merged.push(c.clone()),- }- }- let chunks = &merged[..];-- let uniform = chunks.first().map(|c| c.fmt).filter(|f| {- // Strike has no block-level flag, so a uniformly-struck paragraph still- // takes the inline path. Likewise a link: it must wrap exactly its- // anchor text, so any linked chunk forces the inline path (mirroring- // the PDF path's rule in `render_line_inline`).- !f.strike && chunks.iter().all(|c| c.fmt == *f) && chunks.iter().all(|c| c.link.is_none())- });-- if let Some(fmt) = uniform {- let raw: String = chunks.iter().map(|c| c.text.as_str()).collect();- let raw = raw.trim();- let text = if escape {- escape_inline(raw)- } else {- raw.to_string()- };- return (text, fmt.bold, fmt.italic);- }-- let mut out = String::new();- for Chunk { fmt, link, text } in chunks {- // Emphasis markers must hug non-space content to be valid markdown, so- // surrounding whitespace is hoisted outside the markers.- let lead: String = text.chars().take_while(|c| c.is_whitespace()).collect();- let core = text.trim();- let trail = if core.is_empty() {- String::new()- } else {- text.chars()- .rev()- .take_while(|c| c.is_whitespace())- .collect::<Vec<_>>()- .into_iter()- .rev()- .collect()- };-- out.push_str(&lead);- if core.is_empty() {- continue;- }- let escaped = if escape {- escape_inline(core)- } else {- core.to_string()- };- let mut rendered = if fmt.is_plain() {- escaped- } else {- let (mut open, mut close) = (String::new(), String::new());- if fmt.bold {- open.push_str("**");- close.insert_str(0, "**");- }- if fmt.italic {- open.push('*');- close.insert(0, '*');- }- if fmt.strike {- open.push_str("~~");- close.insert_str(0, "~~");- }- format!("{open}{escaped}{close}")- };- // Link wraps outside emphasis — `[*anchor*](url)` — matching- // `render_line_inline` on the PDF path.- if let Some(url) = link {- rendered = apply_link(&rendered, url);- }- out.push_str(&rendered);- out.push_str(&trail);- }- (out.trim().to_string(), false, false)-}-#[cfg(test)]mod tests {use super::*;diff --git a/crates/liteparse/src/office/inline.rs b/crates/liteparse/src/office/inline.rsnew file mode 100644index 0000000..6782a90--- /dev/null+++ b/crates/liteparse/src/office/inline.rs@@ -0,0 +1,135 @@+//! Inline rendering shared by the native office readers.+//!+//! Both DOCX and PPTX arrive at the same place: a paragraph is a sequence of+//! formatting-tagged text runs, and `Block::Paragraph`/`Block::ListItem` want+//! `(text, bold, italic)`. Only the *cascade* that produces the formatting+//! differs between the two — DOCX resolves it through style chains, PPTX+//! through the placeholder text cascade — so the rendering half is shared and+//! the resolution half is not.+//!+//! Keeping this in one place is what makes the same prose look the same+//! whichever reader produced it, which is the whole point of both pipelines+//! converging on `markdown_layout::Block`.++use crate::markdown_layout::{apply_link, escape_inline};++/// Effective character formatting after the style cascade is applied.+#[derive(Default, Clone, Copy, PartialEq)]+pub(crate) struct Fmt {+ pub(crate) bold: bool,+ pub(crate) italic: bool,+ pub(crate) strike: bool,+}++impl Fmt {+ pub(crate) fn is_plain(self) -> bool {+ !self.bold && !self.italic && !self.strike+ }+}++/// A formatting-tagged run of text, plus the external URL of the hyperlink it+/// sits inside, if any.+#[derive(Clone, PartialEq)]+pub(crate) struct Chunk {+ pub(crate) fmt: Fmt,+ pub(crate) link: Option<String>,+ pub(crate) text: String,+}++/// Render formatting-tagged chunks to the `(text, bold, italic)` shape+/// `Block::Paragraph` and `Block::ListItem` expect.+///+/// When every chunk shares one plain-or-emphasised style the block-level flags+/// carry it and the text stays clean; otherwise emphasis is baked inline and+/// the flags are cleared. This mirrors `paragraph_from_accum` on the PDF path,+/// so the same prose looks the same whichever pipeline produced it.+pub(crate) fn render_chunks(chunks: &[Chunk], escape: bool) -> (String, bool, bool) {+ // Coalesce adjacent runs that share formatting and link first. Word splits+ // a run at every property change, including ones that don't survive the+ // cascade (language, spell-check state, rsid), so a single bold phrase+ // routinely arrives as several identically-formatted chunks. Emitting each+ // one separately would produce `**a****b**`, which no markdown parser+ // reads as one bold span.+ let mut merged: Vec<Chunk> = Vec::new();+ for c in chunks {+ match merged.last_mut() {+ Some(prev) if prev.fmt == c.fmt && prev.link == c.link => prev.text.push_str(&c.text),+ _ => merged.push(c.clone()),+ }+ }+ let chunks = &merged[..];++ let uniform = chunks.first().map(|c| c.fmt).filter(|f| {+ // Strike has no block-level flag, so a uniformly-struck paragraph still+ // takes the inline path. Likewise a link: it must wrap exactly its+ // anchor text, so any linked chunk forces the inline path (mirroring+ // the PDF path's rule in `render_line_inline`).+ !f.strike && chunks.iter().all(|c| c.fmt == *f) && chunks.iter().all(|c| c.link.is_none())+ });++ if let Some(fmt) = uniform {+ let raw: String = chunks.iter().map(|c| c.text.as_str()).collect();+ let raw = raw.trim();+ let text = if escape {+ escape_inline(raw)+ } else {+ raw.to_string()+ };+ return (text, fmt.bold, fmt.italic);+ }++ let mut out = String::new();+ for Chunk { fmt, link, text } in chunks {+ // Emphasis markers must hug non-space content to be valid markdown, so+ // surrounding whitespace is hoisted outside the markers.+ let lead: String = text.chars().take_while(|c| c.is_whitespace()).collect();+ let core = text.trim();+ let trail = if core.is_empty() {+ String::new()+ } else {+ text.chars()+ .rev()+ .take_while(|c| c.is_whitespace())+ .collect::<Vec<_>>()+ .into_iter()+ .rev()+ .collect()+ };++ out.push_str(&lead);+ if core.is_empty() {+ continue;+ }+ let escaped = if escape {+ escape_inline(core)+ } else {+ core.to_string()+ };+ let mut rendered = if fmt.is_plain() {+ escaped+ } else {+ let (mut open, mut close) = (String::new(), String::new());+ if fmt.bold {+ open.push_str("**");+ close.insert_str(0, "**");+ }+ if fmt.italic {+ open.push('*');+ close.insert(0, '*');+ }+ if fmt.strike {+ open.push_str("~~");+ close.insert_str(0, "~~");+ }+ format!("{open}{escaped}{close}")+ };+ // Link wraps outside emphasis — `[*anchor*](url)` — matching+ // `render_line_inline` on the PDF path.+ if let Some(url) = link {+ rendered = apply_link(&rendered, url);+ }+ out.push_str(&rendered);+ out.push_str(&trail);+ }+ (out.trim().to_string(), false, false)+}diff --git a/crates/liteparse/src/office/mod.rs b/crates/liteparse/src/office/mod.rsindex bf99d1d..0569514 100644--- a/crates/liteparse/src/office/mod.rs+++ b/crates/liteparse/src/office/mod.rs@@ -14,6 +14,10 @@pub mod docx;#[cfg(feature = "docx-native")]pub mod docx_layout;+#[cfg(feature = "office-native")]+pub(crate) mod inline;+#[cfg(feature = "pptx-native")]+pub mod pptx;/// The input's bytes when it is a `.docx` the native path should try, `None`/// otherwise. Path inputs are matched on extension only (`.doc`/`.docm`/diff --git a/crates/liteparse/src/office/pptx.rs b/crates/liteparse/src/office/pptx.rsnew file mode 100644index 0000000..a55ecfa--- /dev/null+++ b/crates/liteparse/src/office/pptx.rs@@ -0,0 +1,807 @@+//! PPTX → [`Block`], reading the package directly instead of rendering it.+//!+//! Unlike [`super::docx`] this reader has real geometry available — every shape+//! declares `<a:off>`/`<a:ext>` in EMU, and `pptx::apply_slide_geometry` has+//! already composed group coordinate spaces into `Shape::slide_rect`. That+//! changes the shape of the problem: DOCX has no coordinates so its emitter+//! walks the document in source order, while here **source order is z-order and+//! disagrees with reading order on 48.9% of slides** (measured by+//! `pptx_emit_census`). Ordering is therefore a decision this module makes, not+//! one the file makes for it.+//!+//! The design below follows that census throughout; where a rule looks+//! arbitrary it is usually a corpus measurement, and the comment says which.++use liteparse_ooxml::model::{Inline, RunElement, RunProperties, StrikeStyle};+use liteparse_ooxml::pptx::{+ self, AutoNumberScheme, Bullet, DeckTextDefaults, GraphicFramePayload, ListStyle, MatchRule,+ Placeholder, PlaceholderGeometry, PlaceholderKind, PlaceholderTextStyles, PresentationPackage,+ ResolvedTextStyle, Shape, ShapeKind, Table, TextBody, TextCascade, TextParagraph, TextStyles,+};+use std::collections::HashMap;++use crate::error::LiteParseError;+use crate::markdown_layout::{Block, Cell};+use crate::office::inline::{Chunk, Fmt, render_chunks};++/// Two shapes whose tops differ by less than this sit on the same visual row+/// and are ordered left-to-right. 0.25 in, generous for a line height, so the+/// sort never reorders two shapes a reader would call level with each other.+const ROW_BAND_EMU: i64 = 228_600;++/// Slide titles become `#`. Everything else that is prose becomes a paragraph+/// or a list item — PPTX has no other heading signal, since `outlineLvl` is a+/// DOCX concept and body placeholders carry indent levels, not ranks.+const TITLE_HEADING_LEVEL: u8 = 1;++/// What the emitter should produce beyond the always-on structure.+#[derive(Default, Clone, Copy)]+pub struct EmitOptions {+ /// Render external hyperlinks as `[text](url)`. Mirrors+ /// `LiteParseConfig::extract_links`, which the PDF path honors too.+ pub links: bool,+ /// Emit speaker notes after each slide's body. The census measured 222k+ /// characters of notes against 149k for all slide body text, and+ /// LibreOffice renders none of it, so this is the single largest content+ /// difference between the native and converted paths.+ pub notes: bool,+}++/// Where an emitted [`Block`] came from.+///+/// Simpler than the DOCX equivalent, which has to index into a flattened body+/// sequence to rejoin blocks to layout-assigned pages. A slide *is* a page, so+/// the index is intrinsic rather than recovered.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum BlockSource {+ /// Body content of slide `n`, zero-based.+ Slide(usize),+ /// Speaker notes attached to slide `n`, zero-based.+ Notes(usize),+}++/// Parse a PPTX and emit the shared block model.+///+/// Errors only when the package itself is unreadable. Individual parts are+/// fail-open — a slide whose shape tree will not parse is skipped and the rest+/// of the deck still emits, matching the tolerance the DOCX vendor was+/// retrofitted with across four separate fail-closed classes.+pub fn pptx_to_blocks(data: &[u8], opts: EmitOptions) -> Result<Vec<Block>, LiteParseError> {+ Ok(emit_with_sources(data, opts)?+ .into_iter()+ .map(|(b, _)| b)+ .collect())+}++/// Emit blocks tagged with the slide they came from.+pub fn emit_with_sources(+ data: &[u8],+ opts: EmitOptions,+) -> Result<Vec<(Block, BlockSource)>, LiteParseError> {+ let pkg = pptx::walk(data)+ .map_err(|e| LiteParseError::Conversion(format!("pptx parse failed: {e}")))?;+ Ok(Deck::new(&pkg).emit(&pkg, opts))+}++/// Per-deck caches for the two cascades.+///+/// Both rungs are keyed by part *path* and rebuilt once per path, never once+/// per slide. Every previous PPTX probe records this discipline because a deck+/// with 60 slides sharing 3 layouts otherwise reparses those layouts 60 times.+struct Deck {+ master_geo: HashMap<String, PlaceholderGeometry>,+ layout_geo: HashMap<String, PlaceholderGeometry>,+ master_text: HashMap<String, (PlaceholderTextStyles, DeckTextDefaults)>,+ layout_text: HashMap<String, PlaceholderTextStyles>,+ default_text_style: ListStyle,+}++impl Deck {+ fn new(pkg: &PresentationPackage) -> Self {+ Self {+ master_geo: HashMap::new(),+ layout_geo: HashMap::new(),+ master_text: HashMap::new(),+ layout_text: HashMap::new(),+ // Rung 7. A deck whose presentation.xml will not parse still+ // resolves through the earlier rungs, so this degrades to empty+ // rather than failing the deck.+ default_text_style: pptx::parse_default_text_style(&pkg.presentation.xml)+ .unwrap_or_default(),+ }+ }++ fn emit(&mut self, pkg: &PresentationPackage, opts: EmitOptions) -> Vec<(Block, BlockSource)> {+ let mut out = Vec::new();++ for (idx, slide) in pkg.slides.iter().enumerate() {+ self.prime(slide);++ let Ok(mut shapes) = pptx::parse_shape_tree(&slide.slide.xml) else {+ continue;+ };+ let layout_geo = slide+ .layout+ .as_ref()+ .and_then(|l| self.layout_geo.get(&l.path));+ // Cascade before composition: a placeholder has no rectangle to+ // compose until it has inherited one.+ pptx::apply_inherited_geometry(&mut shapes, layout_geo, MatchRule::Idx);+ pptx::apply_slide_geometry(&mut shapes);++ let fallback = DeckTextDefaults {+ master_styles: TextStyles::default(),+ default_text_style: self.default_text_style.clone(),+ };+ let master = slide+ .master+ .as_ref()+ .and_then(|m| self.master_text.get(&m.path));+ let cascade = TextCascade {+ shape: None,+ layout: slide+ .layout+ .as_ref()+ .and_then(|l| self.layout_text.get(&l.path)),+ master: master.map(|(p, _)| p),+ deck: Some(master.map_or(&fallback, |(_, d)| d)),+ };++ let mut ctx = SlideCtx {+ cascade,+ part: &slide.slide,+ package: &pkg.package,+ opts,+ };+ for shape in reading_order(&shapes) {+ emit_shape(shape, &mut ctx, &mut out, BlockSource::Slide(idx));+ }++ if opts.notes+ && let Some(notes) = &slide.notes+ && let Ok(nshapes) = pptx::parse_shape_tree(¬es.xml)+ {+ emit_notes(&nshapes, &mut ctx, &mut out, idx);+ }+ }+ out+ }++ /// Build whichever cascade rungs this slide's layout and master have not+ /// contributed yet.+ fn prime(&mut self, slide: &pptx::SlideParts) {+ if let Some(master) = &slide.master+ && !self.master_geo.contains_key(&master.path)+ {+ let shapes = pptx::parse_shape_tree(&master.xml).unwrap_or_default();+ self.master_geo.insert(+ master.path.clone(),+ PlaceholderGeometry::from_master(&shapes),+ );+ self.master_text.insert(+ master.path.clone(),+ (+ PlaceholderTextStyles::from_part(&shapes),+ DeckTextDefaults {+ master_styles: pptx::parse_text_styles(&master.xml).unwrap_or_default(),+ default_text_style: self.default_text_style.clone(),+ },+ ),+ );+ }+ let master_geo = slide+ .master+ .as_ref()+ .and_then(|m| self.master_geo.get(&m.path));++ if let Some(layout) = &slide.layout+ && !self.layout_geo.contains_key(&layout.path)+ {+ let mut shapes = pptx::parse_shape_tree(&layout.xml).unwrap_or_default();+ pptx::apply_inherited_geometry(&mut shapes, master_geo, MatchRule::CollapsedKind);+ self.layout_geo.insert(+ layout.path.clone(),+ PlaceholderGeometry::from_layout(&shapes, master_geo),+ );+ self.layout_text.insert(+ layout.path.clone(),+ PlaceholderTextStyles::from_part(&shapes),+ );+ }+ }+}++struct SlideCtx<'a> {+ cascade: TextCascade<'a>,+ /// The slide part, for resolving relationships. Hyperlinks need the raw+ /// external target and SmartArt needs a resolved part path, so both the+ /// relationship table and the part's own directory are required.+ part: &'a pptx::Part,+ package: &'a liteparse_ooxml::docx::zip::PackageContents,+ opts: EmitOptions,+}++// ── reading order ───────────────────────────────────────────────────────────++/// Order a slide's top-level shapes for reading: **title first, then+/// geometric** (top-to-bottom in 0.25 in bands, left-to-right within a band).+///+/// Both halves are load-bearing and the census measured each. Source order+/// alone puts the title somewhere other than first on 95 slides; geometric+/// order alone still does on 41, because authors routinely place a kicker, a+/// caption or a logo strip above the title. Together: **0**.+///+/// Chrome is dropped rather than sorted — a slide number in a corner otherwise+/// perturbs the order without a reader ever caring.+///+/// A group is one unit. Its children are emitted in their own reading order+/// within it, so a two-column group does not interleave with unrelated shapes+/// elsewhere on the slide.+fn reading_order(shapes: &[Shape]) -> Vec<&Shape> {+ let mut v: Vec<&Shape> = shapes+ .iter()+ .filter(|s| !is_chrome(s.placeholder.as_ref()))+ .collect();+ v.sort_by_key(|s| {+ let (band, x) = match s.slide_rect {+ Some(r) => {+ let b = r.bounding_box();+ (b.origin.y.raw().div_euclid(ROW_BAND_EMU), b.origin.x.raw())+ }+ // A shape with no rectangle cannot be placed. Sorting it last+ // keeps it in the output — dropping content to keep a sort total+ // is never the right trade — while leaving positioned shapes in+ // their proper order. The census measured 0 of these.+ None => (i64::MAX, i64::MAX),+ };+ (!is_title(s.placeholder.as_ref()), band, x)+ });+ v+}++fn is_title(ph: Option<&Placeholder>) -> bool {+ matches!(+ ph.map(|p| p.kind),+ Some(PlaceholderKind::Title | PlaceholderKind::CtrTitle)+ )+}++/// The date / footer / slide-number family.+///+/// Note this drops ~7k characters the census found in `dt` and `ftr`+/// placeholders, which average 50 and 56 characters — far too long to be a+/// date or a page number, so some authors are reusing them for real text.+/// Dropping them wholesale is the conservative choice for now because a+/// length test would also admit genuine repeated footer chrome on every+/// slide of a 60-slide deck; revisit with a measurement, not a guess.+fn is_chrome(ph: Option<&Placeholder>) -> bool {+ matches!(+ ph.map(|p| p.kind),+ Some(+ PlaceholderKind::Dt+ | PlaceholderKind::Ftr+ | PlaceholderKind::SldNum+ | PlaceholderKind::Hdr+ | PlaceholderKind::SldImg+ )+ )+}++// ── shapes ──────────────────────────────────────────────────────────────────++fn emit_shape(+ shape: &Shape,+ ctx: &mut SlideCtx<'_>,+ out: &mut Vec<(Block, BlockSource)>,+ src: BlockSource,+) {+ match &shape.kind {+ ShapeKind::AutoShape(sp) => {+ if let Some(body) = &sp.text {+ emit_text_body(body, shape.placeholder.as_ref(), ctx, out, src);+ }+ }+ ShapeKind::Group(group) => {+ for child in reading_order(&group.children) {+ emit_shape(child, ctx, out, src);+ }+ }+ ShapeKind::GraphicFrame(frame) => {+ match &frame.payload {+ GraphicFramePayload::Table(table) => emit_table(table, ctx, out, src),+ GraphicFramePayload::Diagram { data_rel } => emit_diagram(data_rel, ctx, out, src),+ // OLE and charts, which we genuinely do not read.+ GraphicFramePayload::Unsupported { .. } => {}+ }+ }+ // Pictures and connectors carry no text. Figures need image extraction+ // wired first, which on the DOCX path takes its ids from the layout+ // stage; PPTX has no equivalent yet.+ ShapeKind::Picture(_) | ShapeKind::Connector(_) => {}+ }+}++fn emit_text_body(+ body: &TextBody,+ placeholder: Option<&Placeholder>,+ ctx: &mut SlideCtx<'_>,+ out: &mut Vec<(Block, BlockSource)>,+ src: BlockSource,+) {+ let title = is_title(placeholder);+ // Rung 2 is the shape's own list style, so the cascade is per-shape.+ let cascade = TextCascade {+ shape: Some(&body.list_style),+ ..ctx.cascade+ };+ // Ordered-list counters, per indent level, scoped to this text body.+ // PowerPoint restarts numbering per shape, and `render_blocks` prints the+ // marker verbatim rather than renumbering, so emitting a constant "1."+ // would put a literal `1.` on every item.+ let mut counters: HashMap<u8, u32> = HashMap::new();+ for para in &body.paragraphs {+ let resolved = cascade.resolve(¶.properties, placeholder);+ let chunks = chunks_of(para, &resolved, ctx);+ let (text, bold, italic) = render_chunks(&chunks, true);+ if text.is_empty() {+ // 1,114 empty paragraphs on the corpus. They are blank-line+ // separators inside a text box, and emitting them as empty blocks+ // would put stray blank paragraphs through `render_blocks`.+ continue;+ }++ let block = if title {+ Block::Heading {+ level: TITLE_HEADING_LEVEL,+ text,+ }+ } else if let Some((ordered, marker)) =+ list_marker(resolved.bullet.as_ref(), resolved.level, &mut counters)+ {+ Block::ListItem {+ ordered,+ marker,+ level: resolved.level,+ text,+ bold,+ italic,+ }+ } else {+ Block::Paragraph { text, bold, italic }+ };+ out.push((block, src));+ }+}++/// The bullet a paragraph resolves to, as `(ordered, marker)`, or `None` when+/// it is not a list item at all.+///+/// **68.6% of non-empty paragraphs are not list items** on the corpus — 44.7%+/// resolve to an explicit `buNone` and 23.9% to no bullet. The `buNone` share+/// is the reason this consults the resolved style rather than the paragraph:+/// `buNone` exists precisely to override an inherited bullet, so an emitter+/// reading only the master's list style would turn 3,890 plain paragraphs into+/// list items.+fn list_marker(+ bullet: Option<&Bullet>,+ level: u8,+ counters: &mut HashMap<u8, u32>,+) -> Option<(bool, String)> {+ match bullet? {+ Bullet::None => None,+ // The glyph is nearly always from a symbol font — `\u{f0b7}` in+ // Wingdings is the commonest bullet in the corpus — and is meaningless+ // as text. `render_blocks` prints the marker verbatim, so passing the+ // codepoint through would put a private-use character in the markdown.+ // Every bullet character therefore normalises to `-`.+ Bullet::Character { .. } => Some((false, "-".into())),+ Bullet::AutoNumber { scheme, start_at } => {+ let n = counters+ .entry(level)+ .and_modify(|n| *n += 1)+ .or_insert_with(|| start_at.unwrap_or(1));+ // A deeper level restarting means the shallower one keeps counting,+ // which is what PowerPoint does and what a reader expects.+ Some((true, format_autonumber(scheme, *n)))+ }+ }+}++/// Render an auto-number in its declared scheme.+///+/// Four schemes appear on the corpus (`ArabicPeriod` 102, `AlphaLcParenR` 8,+/// `AlphaLcPeriod` 5, `ArabicParenR` 2) but all nine are cheap to support, and+/// `Other` degrades to a plain number rather than guessing.+fn format_autonumber(scheme: &AutoNumberScheme, n: u32) -> String {+ use AutoNumberScheme as S;+ match scheme {+ S::ArabicPeriod => format!("{n}."),+ S::ArabicParenR => format!("{n})"),+ S::ArabicParenBoth => format!("({n})"),+ S::AlphaLcPeriod => format!("{}.", alpha(n, b'a')),+ S::AlphaUcPeriod => format!("{}.", alpha(n, b'A')),+ S::AlphaLcParenR => format!("{})", alpha(n, b'a')),+ S::AlphaUcParenR => format!("{})", alpha(n, b'A')),+ S::RomanLcPeriod => format!("{}.", roman(n).to_lowercase()),+ S::RomanUcPeriod => format!("{}.", roman(n)),+ S::Other(_) => format!("{n}."),+ }+}++/// Spreadsheet-style lettering: a..z, then aa, ab. Matches how PowerPoint+/// continues past 26 rather than wrapping back to `a`.+fn alpha(n: u32, base: u8) -> String {+ let mut n = n.max(1);+ let mut out = Vec::new();+ while n > 0 {+ let rem = (n - 1) % 26;+ out.push(base + rem as u8);+ n = (n - 1) / 26;+ }+ out.reverse();+ String::from_utf8(out).unwrap_or_default()+}++fn roman(n: u32) -> String {+ const TABLE: [(u32, &str); 13] = [+ (1000, "M"),+ (900, "CM"),+ (500, "D"),+ (400, "CD"),+ (100, "C"),+ (90, "XC"),+ (50, "L"),+ (40, "XL"),+ (10, "X"),+ (9, "IX"),+ (5, "V"),+ (4, "IV"),+ (1, "I"),+ ];+ // Beyond the table's range there is no roman numeral to render, so fall+ // back to the arabic number rather than emitting an empty marker.+ if n == 0 || n > 3999 {+ return n.to_string();+ }+ let mut n = n;+ let mut out = String::new();+ for (v, s) in TABLE {+ while n >= v {+ out.push_str(s);+ n -= v;+ }+ }+ out+}++// ── tables ──────────────────────────────────────────────────────────────────++/// Emit `<a:tbl>` as [`Block::MergedTable`].+///+/// The occupancy rule is the trap the schema doc calls out: a cell absorbed by+/// a neighbour's merge is **still present** in the XML with `hMerge`/`vMerge`+/// set, while `MergedTable` follows HTML and expects it to be *absent*. So+/// absorbed cells are dropped and the origin cell carries the span.+///+/// Merges are rare here — 8 colspan and 6 rowspan cells across the whole+/// corpus, against DOCX where they were the single biggest win. The variant is+/// reused because it costs nothing, not because PPTX needs it.+fn emit_table(+ table: &Table,+ ctx: &mut SlideCtx<'_>,+ out: &mut Vec<(Block, BlockSource)>,+ src: BlockSource,+) {+ let mut rows: Vec<Vec<Cell>> = Vec::new();+ for row in &table.rows {+ let mut cells = Vec::new();+ for cell in &row.cells {+ if cell.is_absorbed() {+ continue;+ }+ cells.push(Cell::spanning(+ cell_text(cell.text.as_ref(), ctx),+ cell.grid_span.min(u16::MAX as u32) as u16,+ cell.row_span.min(u16::MAX as u32) as u16,+ ));+ }+ rows.push(cells);+ }+ if rows.iter().all(|r| r.is_empty()) {+ return;+ }+ // `firstRow` is PPTX's only header signal and is set on 15 of 36 corpus+ // tables. When absent we claim no header rather than promoting row 0:+ // `MergedTable` renders a headerless table as a pipe table with an empty+ // header, which is what the DOCX path does for the same case.+ let header_rows = usize::from(table.first_row);+ out.push((Block::MergedTable { rows, header_rows }, src));+}++/// Collapse a cell's paragraphs to one line.+///+/// Text is left **unescaped**: `render_blocks` escapes per dialect on the way+/// out (`|` for pipe tables, `&<>` for HTML), and escaping here would either+/// double the backslashes or surface them literally inside an HTML cell. Same+/// rule as the DOCX emitter's `in_cell`.+fn cell_text(body: Option<&TextBody>, ctx: &mut SlideCtx<'_>) -> String {+ let Some(body) = body else {+ return String::new();+ };+ let cascade = TextCascade {+ shape: Some(&body.list_style),+ ..ctx.cascade+ };+ let mut parts = Vec::new();+ for para in &body.paragraphs {+ let resolved = cascade.resolve(¶.properties, None);+ let chunks = chunks_of(para, &resolved, ctx);+ let (text, ..) = render_chunks(&chunks, false);+ if !text.is_empty() {+ parts.push(text);+ }+ }+ parts.join(" ")+}++// ── SmartArt ────────────────────────────────────────────────────────────────++/// Emit a SmartArt diagram's text, in the graphic frame's place.+///+/// The largest single content gap a shape-tree walk has: 53 corpus parts, all+/// 53 carrying text, 12,102 characters that no walk of the slide can reach.+///+/// Emitted as paragraphs at the frame's position in reading order, which is+/// the part geometry buys us — a document-order reader can only append the+/// diagram wherever its frame happened to be serialised.+///+/// A diagram is a graph, not a document: `dgm:pt` order is the data model's,+/// and the visual arrangement lives in the layout part we do not read. So the+/// text is emitted flat, in data-model order, and nothing here pretends to+/// recover the diagram's shape.+fn emit_diagram(+ data_rel: &str,+ ctx: &mut SlideCtx<'_>,+ out: &mut Vec<(Block, BlockSource)>,+ src: BlockSource,+) {+ let Some(path) = ctx.part.resolve_rel(data_rel) else {+ return;+ };+ let Some(xml) = ctx.package.get_part(&path) else {+ return;+ };+ let Ok(bodies) = pptx::parse_diagram_text(xml) else {+ return;+ };+ for body in &bodies {+ emit_text_body(body, None, ctx, out, src);+ }+}++// ── notes ───────────────────────────────────────────────────────────────────++/// Emit a notes slide's body.+///+/// Only the `body` placeholder. The census measured 222,109 characters there+/// against 1,420 characters everywhere else on notes slides, almost all of it a+/// slide number — so this needs no heuristic, and the `sldImg` thumbnail+/// carries no text at all.+fn emit_notes(+ shapes: &[Shape],+ ctx: &mut SlideCtx<'_>,+ out: &mut Vec<(Block, BlockSource)>,+ idx: usize,+) {+ let before = out.len();+ pptx::visit_all(shapes, &mut |shape: &Shape| {+ if !matches!(+ shape.placeholder.as_ref().map(|p| p.kind),+ Some(PlaceholderKind::Body)+ ) {+ return;+ }+ let Some(body) = shape.text() else { return };+ emit_text_body(body, None, ctx, out, BlockSource::Notes(idx));+ });+ if out.len() > before {+ // A rule ahead of the notes marks the boundary, mirroring how the DOCX+ // emitter sets footnote bodies off from the body text.+ out.insert(before, (Block::HorizontalRule, BlockSource::Notes(idx)));+ }+}++// ── inline ──────────────────────────────────────────────────────────────────++/// Flatten a paragraph's inlines to formatting-tagged chunks.+fn chunks_of(para: &TextParagraph, resolved: &ResolvedTextStyle, ctx: &SlideCtx<'_>) -> Vec<Chunk> {+ let mut out = Vec::new();+ walk_inlines(¶.content, None, resolved, ctx, &mut out);+ out+}++fn walk_inlines(+ inlines: &[Inline],+ link: Option<&str>,+ resolved: &ResolvedTextStyle,+ ctx: &SlideCtx<'_>,+ out: &mut Vec<Chunk>,+) {+ for inline in inlines {+ match inline {+ Inline::TextRun(run) => {+ // The cascade fills only what the run leaves unspecified, so a+ // run's own `b="1"` still wins. `apply_to_run` mutates a copy+ // for exactly this reason.+ let mut props = run.properties.clone();+ resolved.apply_to_run(&mut props);+ let fmt = fmt_of(&props);+ let mut text = String::new();+ for el in &run.content {+ match el {+ RunElement::Text(t) => text.push_str(t),+ RunElement::Tab => text.push('\t'),+ // `a:br` is a hard break inside one text box. 250+ // paragraphs on the corpus have one, and `Block`'s+ // single-line `text` cannot hold it, so it becomes a+ // space rather than being dropped or splitting the+ // paragraph into two unrelated blocks.+ RunElement::LineBreak(_) => text.push(' '),+ _ => {}+ }+ }+ if !text.is_empty() {+ out.push(Chunk {+ fmt,+ link: link.map(str::to_string),+ text,+ });+ }+ }+ Inline::Hyperlink(h) => {+ let url = ctx.opts.links.then(|| hyperlink_url(h, ctx)).flatten();+ walk_inlines(&h.content, url.as_deref(), resolved, ctx, out);+ }+ // A field (slide number, date) is already flattened to a plain run+ // by the text parser, so nothing reaches here. Images inside a+ // text body have no text to contribute.+ _ => {}+ }+ }+}++fn hyperlink_url(h: &liteparse_ooxml::model::Hyperlink, ctx: &SlideCtx<'_>) -> Option<String> {+ use liteparse_ooxml::model::HyperlinkTarget as T;+ match &h.target {+ T::ExternalUrl(u) => Some(u.clone()),+ // PPTX hyperlinks are not rewritten at parse time the way DOCX's are,+ // so the rel is resolved here. `Part::resolve_rel` deliberately returns+ // None for external targets — it resolves *part paths* — and a+ // hyperlink is exactly the external case, so the relationship is read+ // directly.+ T::ExternalRel(id) => ctx+ .part+ .rels+ .find_by_id(id.as_str())+ .map(|r| r.target.clone())+ .filter(|t| !t.is_empty()),+ // An internal jump targets a slide, which markdown has no anchor for.+ T::Internal { .. } => None,+ }+}++fn fmt_of(p: &RunProperties) -> Fmt {+ Fmt {+ bold: p.bold.unwrap_or(false),+ italic: p.italic.unwrap_or(false),+ strike: matches!(p.strike, Some(StrikeStyle::Single | StrikeStyle::Double)),+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use liteparse_ooxml::pptx::{Placeholder, PlaceholderKind};++ #[test]+ fn explicit_bu_none_is_not_a_list_item() {+ // 44.7% of corpus paragraphs land here. `buNone` exists to override an+ // inherited bullet, so treating "has a resolved bullet" as "is a list"+ // would turn 3,890 plain paragraphs into list items.+ let mut c = HashMap::new();+ assert!(list_marker(Some(&Bullet::None), 0, &mut c).is_none());+ }++ #[test]+ fn absent_bullet_is_not_a_list_item() {+ let mut c = HashMap::new();+ assert!(list_marker(None, 0, &mut c).is_none());+ }++ #[test]+ fn symbol_font_bullet_never_reaches_the_markdown() {+ // Wingdings' private-use codepoint is the commonest bullet in the+ // corpus and is meaningless as text; `render_blocks` would print it.+ let mut c = HashMap::new();+ let bullet = Bullet::Character {+ char: "\u{f0b7}".into(),+ font: Some("Wingdings".into()),+ };+ let (ordered, marker) =+ list_marker(Some(&bullet), 0, &mut c).expect("a character bullet is a list item");+ assert!(!ordered);+ assert_eq!(marker, "-");+ }++ #[test]+ fn autonumber_counts_up_per_level() {+ let mut c = HashMap::new();+ let b = Bullet::AutoNumber {+ scheme: AutoNumberScheme::ArabicPeriod,+ start_at: None,+ };+ let m = |c: &mut HashMap<u8, u32>, lvl| list_marker(Some(&b), lvl, c).unwrap().1;+ assert_eq!(m(&mut c, 0), "1.");+ assert_eq!(m(&mut c, 0), "2.");+ // A nested level counts independently, and the outer one resumes.+ assert_eq!(m(&mut c, 1), "1.");+ assert_eq!(m(&mut c, 0), "3.");+ }++ #[test]+ fn autonumber_honors_start_at() {+ let mut c = HashMap::new();+ let b = Bullet::AutoNumber {+ scheme: AutoNumberScheme::ArabicParenR,+ start_at: Some(4),+ };+ assert_eq!(list_marker(Some(&b), 0, &mut c).unwrap().1, "4)");+ }++ #[test]+ fn alpha_continues_past_z_instead_of_wrapping() {+ assert_eq!(alpha(1, b'a'), "a");+ assert_eq!(alpha(26, b'a'), "z");+ assert_eq!(alpha(27, b'a'), "aa");+ assert_eq!(alpha(28, b'A'), "AB");+ }++ #[test]+ fn roman_falls_back_rather_than_emitting_nothing() {+ assert_eq!(roman(4), "IV");+ assert_eq!(roman(1987), "MCMLXXXVII");+ // Out of range: an empty marker would be worse than an arabic one.+ assert_eq!(roman(0), "0");+ assert_eq!(roman(4000), "4000");+ }++ fn ph(kind: PlaceholderKind) -> Option<Placeholder> {+ Some(Placeholder { kind, idx: 0 })+ }++ #[test]+ fn chrome_roles_are_recognised_and_titles_are_not() {+ for k in [+ PlaceholderKind::Dt,+ PlaceholderKind::Ftr,+ PlaceholderKind::SldNum,+ PlaceholderKind::Hdr,+ PlaceholderKind::SldImg,+ ] {+ assert!(is_chrome(ph(k).as_ref()), "{k:?} should be chrome");+ }+ assert!(!is_chrome(ph(PlaceholderKind::Title).as_ref()));+ assert!(!is_chrome(ph(PlaceholderKind::Body).as_ref()));+ // 59% of corpus slide text sits on shapes with no placeholder, so the+ // absent case must never be chrome.+ assert!(!is_chrome(None));+ }++ #[test]+ fn both_title_kinds_count_as_titles() {+ assert!(is_title(ph(PlaceholderKind::Title).as_ref()));+ assert!(is_title(ph(PlaceholderKind::CtrTitle).as_ref()));+ assert!(!is_title(ph(PlaceholderKind::SubTitle).as_ref()));+ assert!(!is_title(None));+ }+}