office_oxide 0.1.1

The fastest Office document processing library — DOCX, XLSX, PPTX, DOC, XLS, PPT
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! Parse Markdown text into a `DocumentIR`.
//!
//! Handles the subset of Markdown that document extraction pipelines and
//! office_oxide's own `to_markdown()` produce: ATX headings, pipe tables,
//! bullet/numbered lists, thematic breaks, and paragraphs with bold/italic
//! inline spans.  This is not a full CommonMark implementation — it is
//! intentionally minimal so it carries no extra dependencies.

use crate::format::DocumentFormat;
use crate::ir::{
    DocumentIR, Element, Heading, InlineContent, List, ListItem, Metadata, Paragraph, Section,
    Table, TableCell, TableRow, TextSpan,
};

impl DocumentIR {
    /// Parse Markdown text into a `DocumentIR`.
    ///
    /// Sections are separated by `---` horizontal rules (common for page
    /// boundaries in extracted documents).  Each ATX heading that is not immediately inside
    /// a list or table also acts as a natural section boundary.
    ///
    /// # Example
    ///
    /// ```rust
    /// use office_oxide::ir::DocumentIR;
    /// use office_oxide::format::DocumentFormat;
    ///
    /// let md = "# Title\n\nHello **world**.\n\n- item one\n- item two\n";
    /// let ir = DocumentIR::from_markdown(md, DocumentFormat::Docx);
    /// assert!(!ir.sections.is_empty());
    /// ```
    pub fn from_markdown(markdown: &str, format: DocumentFormat) -> Self {
        let mut parser = MarkdownParser::new(markdown);
        let sections = parser.parse_sections();
        DocumentIR {
            metadata: Metadata {
                format,
                title: None,
                ..Default::default()
            },
            sections,
        }
    }
}

// ---------------------------------------------------------------------------
// Parser state
// ---------------------------------------------------------------------------

struct MarkdownParser<'a> {
    lines: Vec<&'a str>,
    pos: usize,
}

impl<'a> MarkdownParser<'a> {
    fn new(src: &'a str) -> Self {
        Self {
            lines: src.lines().collect(),
            pos: 0,
        }
    }

    fn peek(&self) -> Option<&'a str> {
        self.lines.get(self.pos).copied()
    }

    fn advance(&mut self) -> Option<&'a str> {
        let line = self.lines.get(self.pos).copied();
        self.pos += 1;
        line
    }

    /// Parse the full document into sections, splitting on thematic `---`
    /// breaks and top-level H1/H2 headings.
    fn parse_sections(&mut self) -> Vec<Section> {
        let mut sections: Vec<Section> = Vec::new();
        let mut current = Section {
            title: None,
            elements: Vec::new(),
            ..Default::default()
        };

        while self.pos < self.lines.len() {
            let line = match self.peek() {
                Some(l) => l,
                None => break,
            };

            // Blank line
            if line.trim().is_empty() {
                self.advance();
                continue;
            }

            // Thematic break `---` / `***` / `___` starts a new section
            if is_thematic_break(line) {
                self.advance();
                if !current.elements.is_empty() || current.title.is_some() {
                    sections.push(current);
                    current = Section {
                        title: None,
                        elements: Vec::new(),
                        ..Default::default()
                    };
                }
                continue;
            }

            // ATX heading
            if let Some((level, text)) = parse_atx_heading(line) {
                self.advance();
                // H1 headings start a new section
                if level == 1 {
                    if !current.elements.is_empty() || current.title.is_some() {
                        sections.push(current);
                    }
                    current = Section {
                        title: Some(text.clone()),
                        elements: Vec::new(),
                        ..Default::default()
                    };
                } else {
                    current.elements.push(Element::Heading(Heading {
                        level,
                        content: parse_inline(&text),
                    }));
                }
                continue;
            }

            // Pipe table
            if line.trim_start().starts_with('|') {
                if let Some(table) = self.parse_table() {
                    current.elements.push(Element::Table(table));
                    continue;
                }
            }

            // Unordered list
            if is_unordered_list_marker(line) {
                let list = self.parse_list(false);
                current.elements.push(Element::List(list));
                continue;
            }

            // Ordered list
            if is_ordered_list_marker(line) {
                let list = self.parse_list(true);
                current.elements.push(Element::List(list));
                continue;
            }

            // Regular paragraph (accumulate until blank line or block element)
            let para = self.parse_paragraph();
            if !para.content.is_empty() {
                current.elements.push(Element::Paragraph(para));
            }
        }

        if !current.elements.is_empty() || current.title.is_some() {
            sections.push(current);
        }

        // Ensure at least one section
        if sections.is_empty() {
            sections.push(Section {
                title: None,
                elements: Vec::new(),
                ..Default::default()
            });
        }

        sections
    }

    // -----------------------------------------------------------------------
    // Block parsers
    // -----------------------------------------------------------------------

    fn parse_paragraph(&mut self) -> Paragraph {
        let mut lines: Vec<&str> = Vec::new();
        loop {
            match self.peek() {
                None => break,
                Some(line) => {
                    if line.trim().is_empty()
                        || parse_atx_heading(line).is_some()
                        || is_thematic_break(line)
                        || line.trim_start().starts_with('|')
                        || is_unordered_list_marker(line)
                        || is_ordered_list_marker(line)
                    {
                        break;
                    }
                    lines.push(line);
                    self.advance();
                },
            }
        }
        let text = lines.join(" ");
        Paragraph {
            content: parse_inline(&text),
            ..Default::default()
        }
    }

    fn parse_table(&mut self) -> Option<Table> {
        // Collect all consecutive pipe lines
        let mut raw: Vec<&'a str> = Vec::new();
        while let Some(line) = self.peek() {
            if line.trim_start().starts_with('|') {
                raw.push(line);
                self.advance();
            } else {
                break;
            }
        }

        if raw.is_empty() {
            return None;
        }

        // Filter out alignment rows (cells that look like `---`, `:---`, `---:`)
        let data_rows: Vec<&str> = raw
            .iter()
            .copied()
            .filter(|line| !is_table_separator_row(line))
            .collect();

        if data_rows.is_empty() {
            return None;
        }

        let mut rows: Vec<TableRow> = Vec::new();
        for (i, row_line) in data_rows.iter().enumerate() {
            let cells = split_pipe_row(row_line)
                .into_iter()
                .map(|cell_text| TableCell {
                    content: vec![Element::Paragraph(Paragraph {
                        content: parse_inline(cell_text.trim()),
                        ..Default::default()
                    })],
                    col_span: 1,
                    row_span: 1,
                    ..Default::default()
                })
                .collect();
            rows.push(TableRow {
                cells,
                is_header: i == 0,
                ..Default::default()
            });
        }

        Some(Table {
            rows,
            ..Default::default()
        })
    }

    fn parse_list(&mut self, ordered: bool) -> List {
        let mut items: Vec<ListItem> = Vec::new();
        loop {
            match self.peek() {
                None => break,
                Some(line) => {
                    if ordered && !is_ordered_list_marker(line) {
                        break;
                    }
                    if !ordered && !is_unordered_list_marker(line) {
                        break;
                    }
                    self.advance();
                    let content_str = strip_list_marker(line);
                    items.push(ListItem {
                        content: vec![Element::Paragraph(Paragraph {
                            content: parse_inline(content_str),
                            ..Default::default()
                        })],
                        nested: None,
                    });
                },
            }
        }
        List {
            ordered,
            items,
            ..Default::default()
        }
    }
}

// ---------------------------------------------------------------------------
// Inline parser (bold, italic, plain)
// ---------------------------------------------------------------------------

fn parse_inline(text: &str) -> Vec<InlineContent> {
    let mut out: Vec<InlineContent> = Vec::new();
    let bytes = text.as_bytes();
    let len = text.len();
    let mut plain_start = 0usize;

    macro_rules! flush_plain {
        ($end:expr) => {
            if plain_start < $end {
                let t = &text[plain_start..$end];
                if !t.is_empty() {
                    out.push(InlineContent::Text(TextSpan::plain(t)));
                }
            }
        };
    }

    let mut i = 0usize;
    while i < len {
        // Bold: **text** or __text__
        if i + 1 < len
            && ((bytes[i] == b'*' && bytes[i + 1] == b'*')
                || (bytes[i] == b'_' && bytes[i + 1] == b'_'))
        {
            let marker = &text[i..i + 2];
            if let Some(end) = text[i + 2..].find(marker) {
                flush_plain!(i);
                let inner = &text[i + 2..i + 2 + end];
                out.push(InlineContent::Text(TextSpan {
                    text: inner.to_string(),
                    bold: true,
                    ..Default::default()
                }));
                i += 2 + end + 2;
                plain_start = i;
                continue;
            }
        }

        // Italic: *text* or _text_
        if (bytes[i] == b'*' || bytes[i] == b'_') && i + 1 < len && bytes[i + 1] != bytes[i] {
            let marker = &text[i..i + 1];
            if let Some(end) = text[i + 1..].find(marker) {
                flush_plain!(i);
                let inner = &text[i + 1..i + 1 + end];
                out.push(InlineContent::Text(TextSpan {
                    text: inner.to_string(),
                    italic: true,
                    ..Default::default()
                }));
                i += 1 + end + 1;
                plain_start = i;
                continue;
            }
        }

        // Strikethrough: ~~text~~
        if i + 1 < len && bytes[i] == b'~' && bytes[i + 1] == b'~' {
            if let Some(end) = text[i + 2..].find("~~") {
                flush_plain!(i);
                let inner = &text[i + 2..i + 2 + end];
                out.push(InlineContent::Text(TextSpan {
                    text: inner.to_string(),
                    strikethrough: true,
                    ..Default::default()
                }));
                i += 2 + end + 2;
                plain_start = i;
                continue;
            }
        }

        // Inline code: `code` — strip backticks, treat as plain
        if bytes[i] == b'`' {
            if let Some(end) = text[i + 1..].find('`') {
                flush_plain!(i);
                let inner = &text[i + 1..i + 1 + end];
                out.push(InlineContent::Text(TextSpan::plain(inner)));
                i += 1 + end + 1;
                plain_start = i;
                continue;
            }
        }

        // Markdown link: [text](url)
        if bytes[i] == b'[' {
            if let Some(bracket_end) = text[i + 1..].find(']') {
                let after_bracket = i + 1 + bracket_end + 1;
                if after_bracket < len && bytes[after_bracket] == b'(' {
                    if let Some(paren_end) = text[after_bracket + 1..].find(')') {
                        flush_plain!(i);
                        let link_text = &text[i + 1..i + 1 + bracket_end];
                        let url = &text[after_bracket + 1..after_bracket + 1 + paren_end];
                        out.push(InlineContent::Text(TextSpan {
                            text: link_text.to_string(),
                            hyperlink: Some(url.to_string()),
                            ..Default::default()
                        }));
                        i = after_bracket + 1 + paren_end + 1;
                        plain_start = i;
                        continue;
                    }
                }
            }
        }

        i += text[i..].chars().next().map(|c| c.len_utf8()).unwrap_or(1);
    }

    flush_plain!(len);

    out
}

// ---------------------------------------------------------------------------
// Line classifiers
// ---------------------------------------------------------------------------

fn parse_atx_heading(line: &str) -> Option<(u8, String)> {
    let trimmed = line.trim_start();
    let hashes = trimmed.bytes().take_while(|&b| b == b'#').count();
    if hashes == 0 || hashes > 6 {
        return None;
    }
    let rest = &trimmed[hashes..];
    if rest.is_empty() || rest.starts_with(' ') || rest.starts_with('\t') {
        let text = rest.trim().trim_end_matches('#').trim().to_string();
        Some((hashes as u8, text))
    } else {
        None
    }
}

fn is_thematic_break(line: &str) -> bool {
    let t = line.trim();
    if t.len() < 3 {
        return false;
    }
    let Some(ch) = t.chars().next() else {
        return false;
    };
    if !matches!(ch, '-' | '*' | '_') {
        return false;
    }
    t.chars().all(|c| c == ch || c == ' ') && t.chars().filter(|&c| c == ch).count() >= 3
}

fn is_table_separator_row(line: &str) -> bool {
    let trimmed = line.trim().trim_matches('|');
    trimmed.split('|').all(|cell| {
        let c = cell.trim().trim_start_matches(':').trim_end_matches(':');
        !c.is_empty() && c.bytes().all(|b| b == b'-')
    })
}

fn split_pipe_row(line: &str) -> Vec<&str> {
    let inner = line.trim().trim_start_matches('|').trim_end_matches('|');
    inner.split('|').collect()
}

fn is_unordered_list_marker(line: &str) -> bool {
    let t = line.trim_start();
    (t.starts_with("- ") || t.starts_with("* ") || t.starts_with("+ ")) && !is_thematic_break(line)
}

fn is_ordered_list_marker(line: &str) -> bool {
    let t = line.trim_start();
    // e.g. "1. " "12. " "1) "
    let num_end = t.bytes().take_while(|b| b.is_ascii_digit()).count();
    if num_end == 0 {
        return false;
    }
    let after = &t[num_end..];
    after.starts_with(". ") || after.starts_with(") ")
}

fn strip_list_marker(line: &str) -> &str {
    let t = line.trim_start();
    if t.starts_with("- ") || t.starts_with("* ") || t.starts_with("+ ") {
        t[2..].trim_start()
    } else {
        // ordered: skip digits + ". " or ") "
        let num_end = t.bytes().take_while(|b| b.is_ascii_digit()).count();
        t[num_end + 2..].trim_start()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::format::DocumentFormat;

    #[test]
    fn parse_heading_paragraph() {
        let md = "# Hello\n\nSome text here.\n";
        let ir = DocumentIR::from_markdown(md, DocumentFormat::Docx);
        assert_eq!(ir.sections.len(), 1);
        assert_eq!(ir.sections[0].title.as_deref(), Some("Hello"));
        assert!(matches!(ir.sections[0].elements[0], Element::Paragraph(_)));
    }

    #[test]
    fn parse_page_break_into_sections() {
        let md = "# Page 1\n\nText one.\n\n---\n\n# Page 2\n\nText two.\n";
        let ir = DocumentIR::from_markdown(md, DocumentFormat::Docx);
        assert_eq!(ir.sections.len(), 2);
        assert_eq!(ir.sections[0].title.as_deref(), Some("Page 1"));
        assert_eq!(ir.sections[1].title.as_deref(), Some("Page 2"));
    }

    #[test]
    fn parse_unordered_list() {
        let md = "- apple\n- banana\n- cherry\n";
        let ir = DocumentIR::from_markdown(md, DocumentFormat::Docx);
        let list = match &ir.sections[0].elements[0] {
            Element::List(l) => l,
            other => panic!("expected List, got {other:?}"),
        };
        assert!(!list.ordered);
        assert_eq!(list.items.len(), 3);
    }

    #[test]
    fn parse_ordered_list() {
        let md = "1. first\n2. second\n";
        let ir = DocumentIR::from_markdown(md, DocumentFormat::Docx);
        let list = match &ir.sections[0].elements[0] {
            Element::List(l) => l,
            other => panic!("expected List, got {other:?}"),
        };
        assert!(list.ordered);
    }

    #[test]
    fn parse_pipe_table() {
        let md = "| Name | Age |\n|------|-----|\n| Alice | 30 |\n| Bob | 25 |\n";
        let ir = DocumentIR::from_markdown(md, DocumentFormat::Docx);
        let table = match &ir.sections[0].elements[0] {
            Element::Table(t) => t,
            other => panic!("expected Table, got {other:?}"),
        };
        assert_eq!(table.rows.len(), 3); // header + 2 data rows (separator stripped)
        assert!(table.rows[0].is_header);
    }

    #[test]
    fn parse_bold_italic_inline() {
        let md = "Hello **world** and *rust*.\n";
        let ir = DocumentIR::from_markdown(md, DocumentFormat::Docx);
        let para = match &ir.sections[0].elements[0] {
            Element::Paragraph(p) => p,
            other => panic!("expected Paragraph, got {other:?}"),
        };
        let spans: Vec<_> = para
            .content
            .iter()
            .filter_map(|c| match c {
                InlineContent::Text(s) => Some(s),
                _ => None,
            })
            .collect();
        assert!(spans.iter().any(|s| s.bold && s.text == "world"));
        assert!(spans.iter().any(|s| s.italic && s.text == "rust"));
    }

    #[test]
    fn parse_empty_markdown() {
        let ir = DocumentIR::from_markdown("", DocumentFormat::Docx);
        assert_eq!(ir.sections.len(), 1);
        assert!(ir.sections[0].elements.is_empty());
    }
}