xberg 1.2.9

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
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
//! Internal types for PPTX extraction.
//!
//! This module defines the internal data structures used to represent
//! slide elements, formatting, and text runs during XML parsing.

use ahash::AHashMap;

use crate::error::Result;

use super::content_builder::ContentBuilder;
use super::{join_runs_with_spacing, parser};

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
pub(super) struct ElementPosition {
    pub(super) x: i64,
    pub(super) y: i64,
    /// Width in EMUs (from `a:ext cx`).
    pub(super) cx: i64,
    /// Height in EMUs (from `a:ext cy`).
    pub(super) cy: i64,
}

#[derive(Debug, Clone, Default)]
pub(super) struct Formatting {
    pub(super) bold: bool,
    pub(super) italic: bool,
    pub(super) underlined: bool,
    pub(super) strikethrough: bool,
    /// Font size in hundredths of a point (from `a:rPr sz`).
    pub(super) font_size: Option<u32>,
    pub(super) lang: String,
}

#[derive(Debug, Clone)]
pub(super) struct Run {
    pub(super) text: String,
    pub(super) formatting: Formatting,
    /// Relationship ID for a hyperlink attached to this run (`a:hlinkClick r:id`).
    pub(super) hyperlink_id: Option<String>,
    /// LaTeX rendering of an OMML `m:oMath`/`m:oMathPara` element and whether it
    /// was display math (`m:oMathPara`, `true`) or inline math (`m:oMath`, `false`).
    /// When `Some`, `text` is empty and rendering must use `math_latex` instead.
    pub(super) math_latex: Option<(String, bool)>,
}

impl Run {
    pub(super) fn extract(&self) -> String {
        if let Some((ref latex, _)) = self.math_latex {
            latex.clone()
        } else {
            self.text.clone()
        }
    }

    pub(super) fn render_as_md(&self) -> String {
        if let Some((ref latex, is_display)) = self.math_latex {
            if latex.is_empty() {
                return String::new();
            }
            return if is_display {
                format!("$${}$$", latex)
            } else {
                format!("${}$", latex)
            };
        }

        let mut result = self.text.clone();

        if self.formatting.bold {
            result = format!("**{}**", result);
        }
        if self.formatting.italic {
            result = format!("*{}*", result);
        }
        if self.formatting.underlined {
            result = format!("<u>{}</u>", result);
        }
        if self.formatting.strikethrough {
            result = format!("~~{}~~", result);
        }

        result
    }
}

#[derive(Debug, Clone)]
pub(super) struct TextElement {
    pub(super) runs: Vec<Run>,
    /// Whether this text element comes from a title placeholder shape.
    pub(super) is_title: bool,
}

#[derive(Debug, Clone)]
pub(super) struct ListItem {
    pub(super) level: u32,
    pub(super) is_ordered: bool,
    pub(super) runs: Vec<Run>,
    /// Whether this paragraph has an explicit bullet marker (`buAutoNum` or `buChar`).
    /// When false, the paragraph is a plain text preamble within a list shape.
    pub(super) has_bullet: bool,
}

#[derive(Debug, Clone)]
pub(super) struct ListElement {
    pub(super) items: Vec<ListItem>,
}

#[derive(Debug, Clone)]
pub(super) struct TableCell {
    pub(super) runs: Vec<Run>,
}

#[derive(Debug, Clone)]
pub(super) struct TableRow {
    pub(super) cells: Vec<TableCell>,
}

#[derive(Debug, Clone)]
pub(super) struct TableElement {
    pub(super) rows: Vec<TableRow>,
}

#[derive(Debug, Clone)]
pub(super) struct ImageReference {
    pub(super) id: String,
    pub(super) target: String,
    /// Alt text / description from shape `descr` attribute.
    pub(super) description: Option<String>,
}

/// A hyperlink relationship resolved from a slide rels file.
#[derive(Debug, Clone)]
pub(super) struct HyperlinkReference {
    pub(super) id: String,
    pub(super) url: String,
}

/// A `<c:chart>` graphic frame reference (`p:graphicFrame` with a chart
/// `graphicData` payload). The chart part itself lives in a separate ZIP
/// entry (e.g. `ppt/charts/chart1.xml`) resolved via `rel_id` against the
/// slide's relationships.
#[derive(Debug, Clone)]
pub(super) struct ChartReference {
    pub(super) rel_id: String,
    /// Text recovered from the chart part (title, category and series
    /// labels, data point values). `None` until resolved, or if resolution
    /// failed or produced no text.
    pub(super) resolved_text: Option<String>,
}

/// A `<dgm:relIds>` SmartArt/diagram graphic frame reference. The diagram
/// data model lives in a separate ZIP entry (e.g. `ppt/diagrams/data1.xml`)
/// resolved via `rel_id` (the `r:dm` relationship) against the slide's
/// relationships.
#[derive(Debug, Clone)]
pub(super) struct DiagramReference {
    pub(super) rel_id: String,
    /// Text recovered from the diagram data part (one line per node).
    /// `None` until resolved, or if resolution failed or produced no text.
    pub(super) resolved_text: Option<String>,
}

#[derive(Debug, Clone)]
pub(super) enum SlideElement {
    Text(TextElement, ElementPosition),
    Table(TableElement, ElementPosition),
    Image(ImageReference, ElementPosition),
    List(ListElement, ElementPosition),
    Chart(ChartReference, ElementPosition),
    SmartArt(DiagramReference, ElementPosition),
    Unknown,
}

impl SlideElement {
    pub(super) fn position(&self) -> ElementPosition {
        match self {
            SlideElement::Text(_, pos)
            | SlideElement::Table(_, pos)
            | SlideElement::Image(_, pos)
            | SlideElement::List(_, pos)
            | SlideElement::Chart(_, pos)
            | SlideElement::SmartArt(_, pos) => *pos,
            SlideElement::Unknown => ElementPosition::default(),
        }
    }
}

#[derive(Debug)]
pub(super) struct Slide {
    pub(super) slide_number: u32,
    pub(super) elements: Vec<SlideElement>,
    pub(super) images: Vec<ImageReference>,
    /// Hyperlink relationships resolved from the slide rels file.
    pub(super) hyperlinks: Vec<HyperlinkReference>,
    /// All relationship IDs from the slide rels file mapped to their target,
    /// regardless of relationship type. Used to resolve chart/SmartArt
    /// `graphicData` references, which are not images or hyperlinks.
    pub(super) rel_targets: AHashMap<String, String>,
}

#[derive(Debug, Clone)]
pub(super) struct ParserConfig {
    pub(super) extract_images: bool,
    pub(super) include_slide_comment: bool,
    pub(super) plain: bool,
    /// When `false`, `![alt](target)` image references are omitted from the
    /// markdown output even though the slide element is present. Mirrors
    /// `ImageExtractionConfig::inject_placeholders`. Default: `true`.
    pub(super) inject_placeholders: bool,
}

impl Default for ParserConfig {
    fn default() -> Self {
        Self {
            extract_images: true,
            include_slide_comment: false,
            plain: false,
            inject_placeholders: true,
        }
    }
}

pub(super) enum ParsedContent {
    Text(TextElement),
    List(ListElement),
}

impl Slide {
    pub(super) fn from_xml(slide_number: u32, xml_data: &[u8], rels_data: Option<&[u8]>) -> Result<Self> {
        let elements = parser::parse_slide_xml(xml_data)?;

        let (images, hyperlinks, rel_targets) = if let Some(rels) = rels_data {
            let slide_rels = parser::parse_slide_rels(rels)?;
            (slide_rels.images, slide_rels.hyperlinks, slide_rels.targets)
        } else {
            (Vec::new(), Vec::new(), AHashMap::new())
        };

        Ok(Self {
            slide_number,
            elements,
            images,
            hyperlinks,
            rel_targets,
        })
    }

    fn render_text_markdown(builder: &mut ContentBuilder, text: &TextElement, config: &ParserConfig) {
        let text_content: String = if config.plain {
            join_runs_with_spacing(&text.runs, Run::extract)
        } else {
            join_runs_with_spacing(&text.runs, Run::render_as_md)
        };
        builder.add_text(&text_content);
    }

    fn render_table_markdown(builder: &mut ContentBuilder, table: &TableElement, config: &ParserConfig) {
        let extract_fn: fn(&Run) -> String = if config.plain { Run::extract } else { Run::render_as_md };
        let table_rows: Vec<Vec<String>> = table
            .rows
            .iter()
            .map(|row| {
                row.cells
                    .iter()
                    .map(|cell| join_runs_with_spacing(&cell.runs, extract_fn))
                    .collect()
            })
            .collect();
        builder.add_table(&table_rows);
    }

    fn render_list_markdown(builder: &mut ContentBuilder, list: &ListElement, config: &ParserConfig) {
        let extract_fn: fn(&Run) -> String = if config.plain { Run::extract } else { Run::render_as_md };
        for item in &list.items {
            let item_text = join_runs_with_spacing(&item.runs, extract_fn);
            if item.has_bullet {
                builder.add_list_item(item.level, item.is_ordered, &item_text);
            } else {
                builder.add_text(&item_text);
            }
        }
    }

    fn render_image_markdown(
        builder: &mut ContentBuilder,
        img_ref: &ImageReference,
        images: &[ImageReference],
        config: &ParserConfig,
    ) {
        if !config.inject_placeholders {
            return;
        }
        let target = images
            .iter()
            .find(|rel| rel.id == img_ref.id)
            .map(|rel| rel.target.as_str())
            .unwrap_or("");
        builder.add_image_with_desc(&img_ref.id, img_ref.description.as_deref(), target);
    }

    /// Find the element index `to_markdown` should render as the slide's
    /// title: the first explicitly-marked title with non-empty text, or
    /// (failing that) the first short (<100 char) text element, matching the
    /// same two-pass search `build_slide_structure` does for its heading.
    fn find_markdown_title_index(elements: &[SlideElement], element_indices: &[usize]) -> Option<usize> {
        element_indices
            .iter()
            .find_map(|&idx| {
                if let SlideElement::Text(text, _) = &elements[idx]
                    && text.is_title
                {
                    let plain = join_runs_with_spacing(&text.runs, Run::extract);
                    if !plain.trim().is_empty() {
                        return Some(idx);
                    }
                }
                None
            })
            .or_else(|| {
                element_indices.iter().find_map(|&idx| {
                    if let SlideElement::Text(text, _) = &elements[idx] {
                        let plain = join_runs_with_spacing(&text.runs, Run::extract);
                        let normalized = plain.replace('\n', " ");
                        if normalized.len() < 100 && !normalized.trim().is_empty() {
                            return Some(idx);
                        }
                    }
                    None
                })
            })
    }

    pub(super) fn to_markdown(&self, config: &ParserConfig) -> String {
        let mut builder = ContentBuilder::new(config.plain);

        if config.include_slide_comment {
            builder.add_slide_header(self.slide_number);
        }

        let mut element_indices: Vec<usize> = (0..self.elements.len()).collect();
        element_indices.sort_by_key(|&i| {
            let pos = self.elements[i].position();
            (pos.y, pos.x)
        });

        let title_idx = Self::find_markdown_title_index(&self.elements, &element_indices);

        if let Some(tidx) = title_idx
            && let SlideElement::Text(text, _) = &self.elements[tidx]
        {
            let text_content: String = if config.plain {
                join_runs_with_spacing(&text.runs, Run::extract)
            } else {
                join_runs_with_spacing(&text.runs, Run::render_as_md)
            };
            let normalized = text_content.replace('\n', " ");
            builder.add_title(normalized.trim());
        }

        for &idx in &element_indices {
            if Some(idx) == title_idx {
                continue;
            }

            match &self.elements[idx] {
                SlideElement::Text(text, _) => {
                    Self::render_text_markdown(&mut builder, text, config);
                }
                SlideElement::Table(table, _) => {
                    Self::render_table_markdown(&mut builder, table, config);
                }
                SlideElement::List(list, _) => {
                    Self::render_list_markdown(&mut builder, list, config);
                }
                SlideElement::Image(img_ref, _) => {
                    Self::render_image_markdown(&mut builder, img_ref, &self.images, config);
                }
                SlideElement::Chart(chart_ref, _) => {
                    if let Some(text) = chart_ref.resolved_text.as_deref() {
                        builder.add_text(text);
                    }
                }
                SlideElement::SmartArt(diagram_ref, _) => {
                    if let Some(text) = diagram_ref.resolved_text.as_deref() {
                        builder.add_text(text);
                    }
                }
                SlideElement::Unknown => {}
            }
        }

        builder.build().0
    }

    pub(super) fn image_count(&self) -> usize {
        self.elements
            .iter()
            .filter(|e| matches!(e, SlideElement::Image(_, _)))
            .count()
    }

    pub(super) fn table_count(&self) -> usize {
        self.elements
            .iter()
            .filter(|e| matches!(e, SlideElement::Table(_, _)))
            .count()
    }
}