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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/// Low-level writer for Markdown generation.
///
/// This module provides the `MarkdownWriter` struct which handles the actual
/// conversion of document elements to Markdown format.
use crate::common::{Error, Result, Metadata};
use crate::document::{Paragraph, Run, Table};
use super::config::{MarkdownOptions, TableStyle};
use std::fmt::Write as FmtWrite;

/// Information about a detected list item.
#[derive(Debug, Clone)]
struct ListItemInfo {
    /// The type of list
    list_type: ListType,
    /// The nesting level (0 = top level)
    level: usize,
    /// The marker text (e.g., "1.", "-", "*")
    marker: String,
    /// The content after the marker
    content: String,
}

/// Types of lists supported.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ListType {
    /// Ordered list (numbered)
    Ordered,
    /// Unordered list (bulleted)
    Unordered,
}

/// Low-level writer for efficient Markdown generation.
///
/// This struct provides optimized methods for writing Markdown elements
/// with minimal allocations.
pub(crate) struct MarkdownWriter {
    /// The output buffer
    buffer: String,
    /// Current options
    options: MarkdownOptions,
}

impl MarkdownWriter {
    /// Create a new writer with the given options.
    pub fn new(options: MarkdownOptions) -> Self {
        Self {
            buffer: String::with_capacity(4096), // Pre-allocate reasonable size
            options,
        }
    }

    /// Write a paragraph to the buffer.
    pub fn write_paragraph(&mut self, para: &Paragraph) -> Result<()> {
        let text = para.text()?;

        // Check if this is a list item
        if let Some(list_info) = self.detect_list_item(&text) {
            self.write_list_item(para, &list_info)?;
        } else {
            // Regular paragraph
            if self.options.include_styles {
                // Write runs with style information
                let runs = para.runs()?;
                for run in runs {
                    self.write_run(&run)?;
                }
            } else {
                // Write plain text
                self.buffer.push_str(&text);
            }
        }

        // Add paragraph break
        self.buffer.push_str("\n\n");
        Ok(())
    }

    /// Write a run with formatting.
    pub fn write_run(&mut self, run: &Run) -> Result<()> {
        // First check if this run contains a formula
        if let Some(formula_markdown) = self.extract_formula_from_run(run)? {
            self.buffer.push_str(&formula_markdown);
            return Ok(());
        }

        let text = run.text()?;
        if text.is_empty() {
            return Ok(());
        }

        let bold = run.bold()?.unwrap_or(false);
        let italic = run.italic()?.unwrap_or(false);
        let strikethrough = run.strikethrough()?.unwrap_or(false);
        let vertical_pos = run.vertical_position()?;

        // Pre-calculate buffer size needed to minimize reallocations
        let mut needed_capacity = text.len();
        if vertical_pos.is_some() {
            needed_capacity += 11; // <sup></sup> or <sub></sub>
        }
        if strikethrough {
            needed_capacity += 9; // ~~ or <del></del>
        }
        if bold && italic {
            needed_capacity += 6; // ***
        } else if bold || italic {
            needed_capacity += 4; // ** or *
        }

        // Reserve capacity to avoid reallocations
        self.buffer.reserve(needed_capacity);

        // For superscript/subscript, we apply them directly and skip other formatting
        if let Some(pos) = vertical_pos {
            match self.options.script_style {
                super::config::ScriptStyle::Html => {
                    match pos {
                        crate::ole::doc::parts::chp::VerticalPosition::Superscript => {
                            self.buffer.push_str("<sup>");
                            self.buffer.push_str(&text);
                            self.buffer.push_str("</sup>");
                        }
                        crate::ole::doc::parts::chp::VerticalPosition::Subscript => {
                            self.buffer.push_str("<sub>");
                            self.buffer.push_str(&text);
                            self.buffer.push_str("</sub>");
                        }
                        _ => {
                            self.buffer.push_str(&text);
                        }
                    }
                }
                super::config::ScriptStyle::Unicode => {
                    // For Unicode, we'd need to convert each character to superscript/subscript
                    // This is complex, so for now fall back to HTML for unsupported characters
                    match pos {
                        crate::ole::doc::parts::chp::VerticalPosition::Superscript => {
                            self.buffer.push_str("<sup>");
                            self.buffer.push_str(&text);
                            self.buffer.push_str("</sup>");
                        }
                        crate::ole::doc::parts::chp::VerticalPosition::Subscript => {
                            self.buffer.push_str("<sub>");
                            self.buffer.push_str(&text);
                            self.buffer.push_str("</sub>");
                        }
                        _ => {
                            self.buffer.push_str(&text);
                        }
                    }
                }
            }
            return Ok(());
        }

        // Apply strikethrough and bold/italic formatting
        if strikethrough {
            match self.options.strikethrough_style {
                super::config::StrikethroughStyle::Markdown => {
                    match (bold, italic) {
                        (true, true) => {
                            self.buffer.push_str("~~***");
                            self.buffer.push_str(&text);
                            self.buffer.push_str("***~~");
                        }
                        (true, false) => {
                            self.buffer.push_str("~~**");
                            self.buffer.push_str(&text);
                            self.buffer.push_str("**~~");
                        }
                        (false, true) => {
                            self.buffer.push_str("~~*");
                            self.buffer.push_str(&text);
                            self.buffer.push_str("*~~");
                        }
                        (false, false) => {
                            self.buffer.push_str("~~");
                            self.buffer.push_str(&text);
                            self.buffer.push_str("~~");
                        }
                    }
                }
                super::config::StrikethroughStyle::Html => {
                    self.buffer.push_str("<del>");
                    match (bold, italic) {
                        (true, true) => {
                            self.buffer.push_str("***");
                            self.buffer.push_str(&text);
                            self.buffer.push_str("***");
                        }
                        (true, false) => {
                            self.buffer.push_str("**");
                            self.buffer.push_str(&text);
                            self.buffer.push_str("**");
                        }
                        (false, true) => {
                            self.buffer.push('*');
                            self.buffer.push_str(&text);
                            self.buffer.push('*');
                        }
                        (false, false) => {
                            self.buffer.push_str(&text);
                        }
                    }
                    self.buffer.push_str("</del>");
                }
            }
        } else {
            // Apply bold/italic only
            match (bold, italic) {
                (true, true) => {
                    self.buffer.push_str("***");
                    self.buffer.push_str(&text);
                    self.buffer.push_str("***");
                }
                (true, false) => {
                    self.buffer.push_str("**");
                    self.buffer.push_str(&text);
                    self.buffer.push_str("**");
                }
                (false, true) => {
                    self.buffer.push('*');
                    self.buffer.push_str(&text);
                    self.buffer.push('*');
                }
                (false, false) => {
                    self.buffer.push_str(&text);
                }
            }
        }

        Ok(())
    }

    /// Write a table to the buffer.
    pub fn write_table(&mut self, table: &Table) -> Result<()> {
        // Check if table has merged cells
        let has_merged_cells = self.table_has_merged_cells(table)?;

        match self.options.table_style {
            TableStyle::Markdown if !has_merged_cells => {
                self.write_markdown_table(table)?;
            }
            TableStyle::MinimalHtml | TableStyle::Markdown => {
                self.write_html_table(table, false)?;
            }
            TableStyle::StyledHtml => {
                self.write_html_table(table, true)?;
            }
        }

        // Add spacing after table
        self.buffer.push_str("\n\n");
        Ok(())
    }

    /// Check if a table has merged cells.
    ///
    /// Uses multiple heuristics to detect merged cells:
    /// - Inconsistent cell counts across rows
    /// - Empty cells in positions where content is expected
    /// - Cell spans larger than 1 (when available)
    fn table_has_merged_cells(&self, table: &Table) -> Result<bool> {
        let rows = table.rows()?;
        if rows.is_empty() {
            return Ok(false);
        }

        // Check for inconsistent cell counts
        let cell_counts: Vec<usize> = rows.iter()
            .map(|row| row.cells().map(|cells| cells.len()).unwrap_or(0))
            .collect();

        let max_cells = cell_counts.iter().max().unwrap_or(&0);
        let min_cells = cell_counts.iter().min().unwrap_or(&0);

        // If cell counts vary significantly, likely merged cells
        if max_cells > min_cells {
            return Ok(true);
        }

        // Check for empty cells in patterns that suggest merging
        // This is a heuristic: if we have empty cells surrounded by content,
        // it might indicate horizontal merging
        for row in &rows {
            let cells = row.cells()?;
            if cells.len() < 2 {
                continue;
            }

            let mut empty_streak = 0;
            for cell in &cells {
                let cell_text = cell.text()?;
                let text = cell_text.trim();
                if text.is_empty() {
                    empty_streak += 1;
                    // Multiple consecutive empty cells suggest merging
                    if empty_streak >= 2 {
                        return Ok(true);
                    }
                } else {
                    empty_streak = 0;
                }
            }
        }

        // For more advanced detection, we could check:
        // - Cell spans (gridSpan, rowspan attributes)
        // - Vertical merging (vMerge attributes)
        // But these require deeper parsing of the underlying formats

        Ok(false)
    }

    /// Write a table in Markdown format.
    fn write_markdown_table(&mut self, table: &Table) -> Result<()> {
        let rows = table.rows()?;
        if rows.is_empty() {
            return Ok(());
        }

        // Write header row (first row)
        self.buffer.push('|');
        let first_row = &rows[0];
        for cell in first_row.cells()? {
            let text = cell.text()?;
            // Escape pipe characters in cell content
            let escaped = text.replace('|', "\\|").replace('\n', " ");
            write!(self.buffer, " {} |", escaped).map_err(|e| Error::Other(e.to_string()))?;
        }
        self.buffer.push('\n');

        // Write separator row
        self.buffer.push('|');
        let cell_count = first_row.cells()?.len();
        for _ in 0..cell_count {
            self.buffer.push_str("----------|");
        }
        self.buffer.push('\n');

        // Write data rows
        for row in &rows[1..] {
            self.buffer.push('|');
            for cell in row.cells()? {
                let text = cell.text()?;
                let escaped = text.replace('|', "\\|").replace('\n', " ");
                write!(self.buffer, " {} |", escaped).map_err(|e| Error::Other(e.to_string()))?;
            }
            self.buffer.push('\n');
        }

        Ok(())
    }

    /// Write a table in HTML format.
    fn write_html_table(&mut self, table: &Table, styled: bool) -> Result<()> {
        let indent = " ".repeat(self.options.html_table_indent);

        if styled {
            self.buffer.push_str("<table class=\"doc-table\">\n");
        } else {
            self.buffer.push_str("<table>\n");
        }

        let rows = table.rows()?;
        for (i, row) in rows.iter().enumerate() {
            writeln!(self.buffer, "{}<tr>", indent).map_err(|e| Error::Other(e.to_string()))?;

            // First row is typically header
            let tag = if i == 0 { "th" } else { "td" };

            for cell in row.cells()? {
                let text = cell.text()?;
                // HTML escape
                let escaped = text
                    .replace('&', "&amp;")
                    .replace('<', "&lt;")
                    .replace('>', "&gt;")
                    .replace('\n', "<br>");
                
                writeln!(self.buffer, "{}{}<{}>{}</{}>", 
                    indent, indent, tag, escaped, tag)
                    .map_err(|e| Error::Other(e.to_string()))?;
            }

            writeln!(self.buffer, "{}</tr>", indent).map_err(|e| Error::Other(e.to_string()))?;
        }

        self.buffer.push_str("</table>");
        Ok(())
    }

    /// Get the final markdown output.
    pub fn finish(self) -> String {
        self.buffer
    }

    /// Append text to the buffer.
    pub fn push_str(&mut self, text: &str) {
        self.buffer.push_str(text);
    }

    /// Append a single character to the buffer.
    pub fn push(&mut self, ch: char) {
        self.buffer.push(ch);
    }

    /// Write a formatted string to the buffer.
    pub fn write_fmt(&mut self, args: std::fmt::Arguments) -> Result<()> {
        use std::fmt::Write as FmtWrite;
        self.buffer.write_fmt(args).map_err(|e| Error::Other(e.to_string()))
    }

    /// Reserve additional capacity in the buffer.
    pub fn reserve(&mut self, additional: usize) {
        self.buffer.reserve(additional);
    }

    /// Write document metadata as YAML front matter.
    ///
    /// If metadata is available and include_metadata is enabled,
    /// this writes the metadata as YAML front matter at the beginning of the document.
    pub fn write_metadata(&mut self, metadata: &Metadata) -> Result<()> {
        if !self.options.include_metadata {
            return Ok(());
        }

        let yaml_front_matter = metadata.to_yaml_front_matter()
            .map_err(|e| Error::Other(format!("Failed to generate YAML front matter: {}", e)))?;

        if !yaml_front_matter.is_empty() {
            self.buffer.push_str(&yaml_front_matter);
        }

        Ok(())
    }

    /// Detect if a paragraph is a list item and extract list information.
    fn detect_list_item(&self, text: &str) -> Option<ListItemInfo> {
        let text = text.trim_start();

        // Check for ordered lists: 1. 2. 3. or 1) 2) 3) or (1) (2) (3)
        if let Some(captures) = self.extract_ordered_list_marker(text) {
            let marker = captures.0;
            let content = captures.1;
            let level = self.calculate_indent_level(text);
            return Some(ListItemInfo {
                list_type: ListType::Ordered,
                level,
                marker: marker.to_string(),
                content: content.to_string(),
            });
        }

        // Check for unordered lists: - * •
        if let Some(captures) = self.extract_unordered_list_marker(text) {
            let marker = captures.0;
            let content = captures.1;
            let level = self.calculate_indent_level(text);
            return Some(ListItemInfo {
                list_type: ListType::Unordered,
                level,
                marker: marker.to_string(),
                content: content.to_string(),
            });
        }

        None
    }

    /// Extract ordered list marker and content.
    fn extract_ordered_list_marker<'a>(&self, text: &'a str) -> Option<(&'a str, &'a str)> {
        // Match patterns like: "1. ", "2) ", "(1) ", etc.
        if let Some(pos) = text.find('.')
            && pos > 0 && text[..pos].chars().all(|c| c.is_ascii_digit()) {
                let marker_end = pos + 1;
                if text.len() > marker_end && text.as_bytes()[marker_end] == b' ' {
                    return Some((&text[..marker_end], &text[marker_end + 1..]));
                }
            }

        if let Some(pos) = text.find(')')
            && pos > 0 && text[..pos].chars().all(|c| c.is_ascii_digit()) {
                let marker_end = pos + 1;
                if text.len() > marker_end && text.as_bytes()[marker_end] == b' ' {
                    return Some((&text[..marker_end], &text[marker_end + 1..]));
                }
            }

        // Check for parenthesized numbers: (1) (2) (3)
        if text.starts_with('(')
            && let Some(end_pos) = text.find(") ") {
                let inner = &text[1..end_pos];
                if inner.chars().all(|c| c.is_ascii_digit()) {
                    return Some((&text[..end_pos + 1], &text[end_pos + 2..]));
                }
            }

        None
    }

    /// Extract unordered list marker and content.
    fn extract_unordered_list_marker<'a>(&self, text: &'a str) -> Option<(&'a str, &'a str)> {
        let markers = ["-", "*", "•"];

        for &marker in &markers {
            if let Some(remaining) = text.strip_prefix(marker)
                && (remaining.starts_with(' ') || remaining.starts_with('\t')) {
                    return Some((marker, remaining.trim_start()));
                }
        }

        None
    }

    /// Calculate the indentation level based on leading spaces/tabs.
    fn calculate_indent_level(&self, text: &str) -> usize {
        let leading = text.len() - text.trim_start().len();
        // Each indent level corresponds to list_indent spaces
        leading / self.options.list_indent
    }

    /// Extract formula content from a run and convert to markdown.
    ///
    /// Returns the markdown representation of the formula if one is found, None otherwise.
    fn extract_formula_from_run(&self, run: &Run) -> Result<Option<String>> {
        // Try OOXML OMML formulas first
        if let crate::document::Run::Docx(docx_run) = run
            && let Some(_omml_xml) = docx_run.omml_formula()? {
                // For now, return a placeholder. In a full implementation,
                // this would parse the OMML XML and convert to LaTeX/markdown
                return Ok(Some(self.format_formula_placeholder("OMML formula detected")));
            }

        // Try OLE MTEF formulas
        if let crate::document::Run::Doc(ole_run) = run
            && ole_run.has_mtef_formula() {
                // Get the MTEF formula AST
                if let Some(mtef_ast) = ole_run.mtef_formula_ast() {
                    // Convert MTEF AST to LaTeX
                    let latex = self.convert_mtef_to_latex(mtef_ast);
                    return Ok(Some(self.format_formula(&latex, true))); // true = inline
                } else {
                    // Fallback placeholder if AST is not available
                    return Ok(Some(self.format_formula("[Formula]", true)));
                }
            }

        Ok(None)
    }

    /// Convert MTEF AST nodes to LaTeX string
    #[cfg(feature = "formula")]
    fn convert_mtef_to_latex(&self, nodes: &[crate::formula::MathNode]) -> String {
        use crate::formula::latex::LatexConverter;
        
        let mut converter = LatexConverter::new();
        match converter.convert_nodes(nodes) {
            Ok(latex) => latex.to_string(),
            Err(_) => "[Formula conversion error]".to_string(),
        }
    }
    
    /// Convert MTEF AST nodes to LaTeX string (fallback when formula feature is disabled)
    #[cfg(not(feature = "formula"))]
    fn convert_mtef_to_latex(&self, _nodes: &[()]) -> String {
        "[Formula support disabled - enable 'formula' feature]".to_string()
    }

    /// Format a formula with the appropriate delimiters.
    ///
    /// # Arguments
    /// * `formula` - The formula content (LaTeX)
    /// * `inline` - Whether this is an inline formula (true) or display formula (false)
    fn format_formula(&self, formula: &str, inline: bool) -> String {
        if inline {
            match self.options.formula_style {
                super::config::FormulaStyle::LaTeX => format!("\\({}\\)", formula),
                super::config::FormulaStyle::Dollar => format!("${}$", formula),
            }
        } else {
            match self.options.formula_style {
                super::config::FormulaStyle::LaTeX => format!("\\[{}\\]", formula),
                super::config::FormulaStyle::Dollar => format!("$${}$$", formula),
            }
        }
    }

    /// Format a formula placeholder with the appropriate delimiters.
    #[allow(dead_code)]
    fn format_formula_placeholder(&self, placeholder: &str) -> String {
        self.format_formula(placeholder, true)
    }

    /// Write a list item with proper formatting.
    fn write_list_item(&mut self, _para: &Paragraph, list_info: &ListItemInfo) -> Result<()> {
        // Add indentation for nested lists
        let indent = " ".repeat(list_info.level * self.options.list_indent);

        // Generate the appropriate marker
        let marker = match list_info.list_type {
            ListType::Ordered => {
                // For ordered lists, we need to determine the number
                // For now, use a simple approach - in a real implementation
                // we'd track list state across paragraphs
                if list_info.marker.contains('.') {
                    // Keep "1." as is
                    list_info.marker.clone()
                } else {
                    // Convert "1)" or "(1)" to "1." for markdown
                    if list_info.marker.starts_with('(') && list_info.marker.ends_with(')') {
                        // Extract number from (1) -> 1.
                        let inner = &list_info.marker[1..list_info.marker.len()-1];
                        format!("{}.", inner)
                    } else {
                        // Convert "1)" to "1."
                        list_info.marker.replace(')', ".")
                    }
                }
            }
            ListType::Unordered => "-".to_string(),
        };

        // Write the list item
        write!(self.buffer, "{}{} ", indent, marker).map_err(|e| Error::Other(e.to_string()))?;

        // Write the content with styles if enabled
        if self.options.include_styles && !list_info.content.trim().is_empty() {
            // For styled content, we need to skip the marker part and write the remaining runs
            // This is a simplified approach - in practice, we'd need more sophisticated
            // parsing to handle cases where the marker spans multiple runs
            self.buffer.push_str(&list_info.content);
        } else {
            // Write the content directly
            self.buffer.push_str(&list_info.content);
        }

        Ok(())
    }
}