litchi 0.0.1

High-performance parser for Microsoft Office, OpenDocument, and Apple iWork file formats with unified API
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
//! Text-related ODF elements.
//!
//! This module provides classes for text elements like paragraphs, spans,
//! headings, and other text content elements.

use super::element::{Element, ElementBase};
use crate::common::{Error, Result};

/// A text paragraph element
#[derive(Debug, Clone)]
pub struct Paragraph {
    element: Element,
}

impl Default for Paragraph {
    fn default() -> Self {
        Self::new()
    }
}

impl Paragraph {
    /// Create a new paragraph
    pub fn new() -> Self {
        Self {
            element: Element::new("text:p"),
        }
    }

    /// Create paragraph from element
    pub fn from_element(element: Element) -> Result<Self> {
        if element.tag_name() != "text:p" {
            return Err(Error::InvalidFormat("Element is not a paragraph".to_string()));
        }
        Ok(Self { element })
    }

    /// Get the text content of the paragraph
    pub fn text(&self) -> Result<String> {
        Ok(self.element.get_text_recursive())
    }

    /// Set the text content of the paragraph
    pub fn set_text(&mut self, text: &str) {
        self.element.set_text(text);
    }

    /// Get all text spans within this paragraph
    pub fn spans(&self) -> Result<Vec<Span>> {
        let mut spans = Vec::new();
        for child in self.element.children() {
            if child.tag_name() == "text:span" {
                // This is a simplified conversion - in practice you'd need proper downcasting
                if let Ok(span) = Span::from_element(unsafe { &*(child as *const _ as *const Element) }.clone()) {
                    spans.push(span);
                }
            }
        }
        Ok(spans)
    }

    /// Add a text span to this paragraph
    pub fn add_span(&mut self, span: Span) {
        self.element.add_child(Box::new(span.element));
    }

    /// Check if this paragraph is a heading
    pub fn is_heading(&self) -> bool {
        false // Paragraphs are not headings
    }

    /// Get the style name
    pub fn style_name(&self) -> Option<&str> {
        self.element.get_attribute("text:style-name")
    }

    /// Set the style name
    pub fn set_style_name(&mut self, name: &str) {
        self.element.set_attribute("text:style-name", name);
    }
}

impl From<Paragraph> for Element {
    fn from(para: Paragraph) -> Element {
        para.element
    }
}

/// A text span element (formatted text within a paragraph)
#[derive(Debug, Clone)]
pub struct Span {
    element: Element,
}

impl Default for Span {
    fn default() -> Self {
        Self::new()
    }
}

impl Span {
    /// Create a new span
    pub fn new() -> Self {
        Self {
            element: Element::new("text:span"),
        }
    }

    /// Create span from element
    pub fn from_element(element: Element) -> Result<Self> {
        if element.tag_name() != "text:span" {
            return Err(Error::InvalidFormat("Element is not a span".to_string()));
        }
        Ok(Self { element })
    }

    /// Get the text content of the span
    pub fn text(&self) -> Result<String> {
        Ok(self.element.get_text_recursive())
    }

    /// Set the text content of the span
    pub fn set_text(&mut self, text: &str) {
        self.element.set_text(text);
    }

    /// Get the style name
    pub fn style_name(&self) -> Option<&str> {
        self.element.get_attribute("text:style-name")
    }

    /// Set the style name
    pub fn set_style_name(&mut self, name: &str) {
        self.element.set_attribute("text:style-name", name);
    }
}

impl From<Span> for Element {
    fn from(span: Span) -> Element {
        span.element
    }
}

/// A heading element
#[derive(Debug, Clone)]
pub struct Heading {
    element: Element,
}

impl Heading {
    /// Create a new heading
    pub fn new(level: u8) -> Self {
        let mut element = Element::new("text:h");
        element.set_attribute("text:outline-level", &level.to_string());
        Self { element }
    }

    /// Create heading from element
    pub fn from_element(element: Element) -> Result<Self> {
        if element.tag_name() != "text:h" {
            return Err(Error::InvalidFormat("Element is not a heading".to_string()));
        }
        Ok(Self { element })
    }

    /// Get the text content of the heading
    pub fn text(&self) -> Result<String> {
        Ok(self.element.get_text_recursive())
    }

    /// Set the text content of the heading
    pub fn set_text(&mut self, text: &str) {
        self.element.set_text(text);
    }

    /// Get the outline level
    pub fn level(&self) -> Option<u8> {
        self.element.get_int_attribute("text:outline-level").map(|n| n as u8)
    }

    /// Set the outline level
    pub fn set_level(&mut self, level: u8) {
        self.element.set_attribute("text:outline-level", &level.to_string());
    }

    /// Get the style name
    pub fn style_name(&self) -> Option<&str> {
        self.element.get_attribute("text:style-name")
    }

    /// Set the style name
    pub fn set_style_name(&mut self, name: &str) {
        self.element.set_attribute("text:style-name", name);
    }

    /// Check if this is a heading
    pub fn is_heading(&self) -> bool {
        true
    }
}

impl From<Heading> for Element {
    fn from(heading: Heading) -> Element {
        heading.element
    }
}

/// A text list element
#[derive(Debug, Clone)]
pub struct List {
    element: Element,
}

impl Default for List {
    fn default() -> Self {
        Self::new()
    }
}

impl List {
    /// Create a new list
    pub fn new() -> Self {
        Self {
            element: Element::new("text:list"),
        }
    }

    /// Create list from element
    pub fn from_element(element: Element) -> Result<Self> {
        if element.tag_name() != "text:list" {
            return Err(Error::InvalidFormat("Element is not a list".to_string()));
        }
        Ok(Self { element })
    }

    /// Get list items
    pub fn items(&self) -> Result<Vec<ListItem>> {
        let mut items = Vec::new();
        for child in self.element.children() {
            if child.tag_name() == "text:list-item"
                && let Ok(item) = ListItem::from_element(unsafe { &*(child as *const _ as *const Element) }.clone()) {
                    items.push(item);
                }
        }
        Ok(items)
    }

    /// Add a list item
    pub fn add_item(&mut self, item: ListItem) {
        self.element.add_child(Box::new(item.element));
    }

    /// Get the style name
    pub fn style_name(&self) -> Option<&str> {
        self.element.get_attribute("text:style-name")
    }

    /// Set the style name
    pub fn set_style_name(&mut self, name: &str) {
        self.element.set_attribute("text:style-name", name);
    }
}

impl From<List> for Element {
    fn from(list: List) -> Element {
        list.element
    }
}

/// A list item element
#[derive(Debug, Clone)]
pub struct ListItem {
    element: Element,
}

impl Default for ListItem {
    fn default() -> Self {
        Self::new()
    }
}

impl ListItem {
    /// Create a new list item
    pub fn new() -> Self {
        Self {
            element: Element::new("text:list-item"),
        }
    }

    /// Create list item from element
    pub fn from_element(element: Element) -> Result<Self> {
        if element.tag_name() != "text:list-item" {
            return Err(Error::InvalidFormat("Element is not a list item".to_string()));
        }
        Ok(Self { element })
    }

    /// Get the text content of the list item
    pub fn text(&self) -> Result<String> {
        Ok(self.element.get_text_recursive())
    }

    /// Set the text content of the list item
    pub fn set_text(&mut self, text: &str) {
        self.element.set_text(text);
    }

    /// Get nested paragraphs
    pub fn paragraphs(&self) -> Result<Vec<Paragraph>> {
        let mut paragraphs = Vec::new();
        for child in self.element.children() {
            if child.tag_name() == "text:p"
                && let Ok(para) = Paragraph::from_element(unsafe { &*(child as *const _ as *const Element) }.clone()) {
                    paragraphs.push(para);
                }
        }
        Ok(paragraphs)
    }

    /// Add a paragraph to this list item
    pub fn add_paragraph(&mut self, paragraph: Paragraph) {
        self.element.add_child(Box::new(paragraph.element));
    }
}

impl From<ListItem> for Element {
    fn from(item: ListItem) -> Element {
        item.element
    }
}

/// A page break element
#[derive(Debug, Clone)]
pub struct PageBreak {
    element: Element,
}

impl Default for PageBreak {
    fn default() -> Self {
        Self::new()
    }
}

impl PageBreak {
    /// Create a new page break
    pub fn new() -> Self {
        let mut element = Element::new("text:p");
        element.set_attribute("text:style-name", "PageBreak");
        Self { element }
    }

    /// Create page break from element
    pub fn from_element(element: Element) -> Result<Self> {
        if element.tag_name() != "text:p" {
            return Err(Error::InvalidFormat("Element is not a page break".to_string()));
        }
        if element.get_attribute("text:style-name") != Some("PageBreak") {
            return Err(Error::InvalidFormat("Element is not a page break".to_string()));
        }
        Ok(Self { element })
    }
}

impl From<PageBreak> for Element {
    fn from(pb: PageBreak) -> Element {
        pb.element
    }
}

/// Collection of text elements for easy parsing
pub struct TextElements;

impl TextElements {
    /// Parse all paragraphs from an XML reader
    pub fn parse_paragraphs(xml_content: &str) -> Result<Vec<Paragraph>> {
        let mut reader = quick_xml::Reader::from_str(xml_content);
        let mut buf = Vec::new();
        let mut paragraphs = Vec::new();
        let mut current_para: Option<Element> = None;

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(quick_xml::events::Event::Start(ref e)) => {
                    let tag_name = String::from_utf8(e.name().as_ref().to_vec())
                        .unwrap_or_default();

                    if tag_name == "text:p" || tag_name == "text:h" {
                        if let Some(para) = current_para.take()
                            && let Ok(p) = Paragraph::from_element(para) {
                                paragraphs.push(p);
                            }

                        let mut element = Element::new(&tag_name);

                        // Parse attributes
                        for attr_result in e.attributes() {
                            if let Ok(attr) = attr_result
                                && let (Ok(key), Ok(value)) = (
                                    String::from_utf8(attr.key.as_ref().to_vec()),
                                    String::from_utf8(attr.value.to_vec())
                                ) {
                                    element.set_attribute(&key, &value);
                                }
                        }

                        current_para = Some(element);
                    }
                }
                Ok(quick_xml::events::Event::Text(ref t)) => {
                    if let Some(ref mut para) = current_para
                        && let Ok(text) = String::from_utf8(t.to_vec()) {
                            let current_text = para.text().to_string();
                            para.set_text(&format!("{}{}", current_text, text));
                        }
                }
                Ok(quick_xml::events::Event::End(ref e)) => {
                    let tag_name = String::from_utf8(e.name().as_ref().to_vec())
                        .unwrap_or_default();

                    if (tag_name == "text:p" || tag_name == "text:h") && current_para.is_some()
                        && let Some(para) = current_para.take()
                            && let Ok(p) = Paragraph::from_element(para) {
                                paragraphs.push(p);
                            }
                }
                Ok(quick_xml::events::Event::Eof) => break,
                Err(_) => break,
                _ => {}
            }
            buf.clear();
        }

        // Handle any remaining paragraph
        if let Some(para) = current_para
            && let Ok(p) = Paragraph::from_element(para) {
                paragraphs.push(p);
            }

        Ok(paragraphs)
    }

    /// Parse all headings from XML content
    #[allow(dead_code)]
    pub fn parse_headings(xml_content: &str) -> Result<Vec<Heading>> {
        let paragraphs = Self::parse_paragraphs(xml_content)?;
        let mut headings = Vec::new();

        for para in paragraphs {
            let element = para.element;
            if element.tag_name() == "text:h"
                && let Ok(heading) = Heading::from_element(element) {
                    headings.push(heading);
                }
        }

        Ok(headings)
    }

    /// Extract all text content from XML with improved handling of nested elements
    pub fn extract_text(xml_content: &str) -> Result<String> {
        let mut reader = quick_xml::Reader::from_str(xml_content);
        let mut buf = Vec::new();
        let mut text = String::new();
        let mut in_paragraph = false;
        let mut paragraph_text = String::new();

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(quick_xml::events::Event::Start(ref e)) => {
                    let tag_name = String::from_utf8(e.name().as_ref().to_vec())
                        .unwrap_or_default();

                    match tag_name.as_str() {
                        "text:p" | "text:h" => {
                            if in_paragraph && !paragraph_text.is_empty() {
                                if !text.is_empty() {
                                    text.push('\n');
                                }
                                text.push_str(&paragraph_text);
                                paragraph_text.clear();
                            }
                            in_paragraph = true;
                        }
                        "text:list" => {
                            // Handle lists - add a newline before list if needed
                            if in_paragraph && !paragraph_text.is_empty() {
                                if !text.is_empty() {
                                    text.push('\n');
                                }
                                text.push_str(&paragraph_text);
                                paragraph_text.clear();
                            }
                            in_paragraph = false;
                        }
                        "text:list-item" => {
                            // Add bullet point
                            if !paragraph_text.is_empty() {
                                paragraph_text.push('\n');
                            }
                            paragraph_text.push_str("");
                        }
                        "text:line-break" | "text:tab" => {
                            // Handle line breaks and tabs
                            if in_paragraph {
                                if tag_name == "text:line-break" {
                                    paragraph_text.push('\n');
                                } else {
                                    paragraph_text.push('\t');
                                }
                            }
                        }
                        _ => {} // Ignore other elements
                    }
                }
                Ok(quick_xml::events::Event::Text(ref t)) => {
                    if in_paragraph
                        && let Ok(text_content) = String::from_utf8(t.to_vec()) {
                            paragraph_text.push_str(&text_content);
                        }
                }
                Ok(quick_xml::events::Event::End(ref e)) => {
                    let tag_name = String::from_utf8(e.name().as_ref().to_vec())
                        .unwrap_or_default();

                    match tag_name.as_str() {
                        "text:p" | "text:h" => {
                            if in_paragraph && !paragraph_text.is_empty() {
                                if !text.is_empty() {
                                    text.push('\n');
                                }
                                text.push_str(&paragraph_text);
                                paragraph_text.clear();
                            }
                            in_paragraph = false;
                        }
                        "text:list" => {
                            in_paragraph = false;
                        }
                        _ => {}
                    }
                }
                Ok(quick_xml::events::Event::Eof) => {
                    // Handle any remaining paragraph text
                    if in_paragraph && !paragraph_text.is_empty() {
                        if !text.is_empty() {
                            text.push('\n');
                        }
                        text.push_str(&paragraph_text);
                    }
                    break;
                }
                Err(_) => break,
                _ => {}
            }
            buf.clear();
        }

        Ok(text)
    }
}