typlite/writer/
text.rs

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
//! Text writer implementation - produces plain text output

use cmark_writer::ast::Node;
use ecow::EcoString;

use crate::common::{ExternalFrameNode, FigureNode, FormatWriter};
use crate::Result;

/// Text writer implementation
#[derive(Default)]
pub struct TextWriter {}

impl TextWriter {
    pub fn new() -> Self {
        Self {}
    }

    fn write_node(node: &Node, output: &mut EcoString) -> Result<()> {
        match node {
            Node::Document(blocks) => {
                for block in blocks {
                    Self::write_node(block, output)?;
                    output.push_str("\n");
                }
            }
            Node::Paragraph(inlines) => {
                for inline in inlines {
                    Self::write_node(inline, output)?;
                }
                output.push_str("\n");
            }
            Node::Heading {
                level: _,
                content,
                heading_type: _,
            } => {
                for inline in content {
                    Self::write_node(inline, output)?;
                }
                output.push_str("\n");
            }
            Node::BlockQuote(content) => {
                for block in content {
                    Self::write_node(block, output)?;
                }
            }
            Node::CodeBlock {
                language: _,
                content,
                block_type: _,
            } => {
                output.push_str(content);
                output.push_str("\n\n");
            }
            Node::OrderedList { start: _, items } => {
                for item in items.iter() {
                    match item {
                        cmark_writer::ast::ListItem::Ordered { content, .. }
                        | cmark_writer::ast::ListItem::Unordered { content } => {
                            for block in content {
                                Self::write_node(block, output)?;
                            }
                        }
                        _ => {}
                    }
                }
            }
            Node::UnorderedList(items) => {
                for item in items {
                    match item {
                        cmark_writer::ast::ListItem::Ordered { content, .. }
                        | cmark_writer::ast::ListItem::Unordered { content } => {
                            for block in content {
                                Self::write_node(block, output)?;
                            }
                        }
                        _ => {}
                    }
                }
            }
            Node::Table {
                headers,
                rows,
                alignments: _,
            } => {
                // Write headers
                for header in headers {
                    Self::write_node(header, output)?;
                    output.push(' ');
                }
                output.push_str("\n");

                // Write rows
                for row in rows {
                    for cell in row {
                        Self::write_node(cell, output)?;
                        output.push(' ');
                    }
                    output.push_str("\n");
                }
                output.push_str("\n");
            }
            Node::Text(text) => {
                output.push_str(text);
            }
            Node::Emphasis(content) | Node::Strong(content) | Node::Strikethrough(content) => {
                for inline in content {
                    Self::write_node(inline, output)?;
                }
            }
            Node::Link {
                url: _,
                title: _,
                content,
            } => {
                for inline in content {
                    Self::write_node(inline, output)?;
                }
            }
            Node::Image {
                url: _,
                title: _,
                alt,
            } => {
                if !alt.is_empty() {
                    for inline in alt {
                        Self::write_node(inline, output)?;
                    }
                }
            }
            Node::InlineCode(code) => {
                output.push_str(code);
            }
            Node::HardBreak => {
                output.push_str("\n");
            }
            Node::SoftBreak => {
                output.push(' ');
            }
            Node::ThematicBreak => {
                output.push_str("\n");
            }
            Node::HtmlElement(element) => {
                for child in &element.children {
                    Self::write_node(child, output)?;
                }
            }
            node if node.is_custom_type::<FigureNode>() => {
                if let Some(figure_node) = node.as_custom_type::<FigureNode>() {
                    Self::write_node(&figure_node.body, output)?;
                    if !figure_node.caption.is_empty() {
                        output.push_str("\n");
                        output.push_str(&figure_node.caption);
                    }
                }
            }
            node if node.is_custom_type::<ExternalFrameNode>() => {
                if let Some(external_frame) = node.as_custom_type::<ExternalFrameNode>() {
                    if !external_frame.alt_text.is_empty() {
                        output.push_str(&external_frame.alt_text);
                    }
                }
            }
            node if node.is_custom_type::<crate::common::HighlightNode>() => {
                if let Some(highlight) = node.as_custom_type::<crate::common::HighlightNode>() {
                    for child in &highlight.content {
                        Self::write_node(child, output)?;
                    }
                }
            }
            _ => {}
        }
        Ok(())
    }
}

impl FormatWriter for TextWriter {
    fn write_eco(&mut self, document: &Node, output: &mut EcoString) -> Result<()> {
        Self::write_node(document, output)
    }

    fn write_vec(&mut self, document: &Node) -> Result<Vec<u8>> {
        let mut output = EcoString::new();
        Self::write_node(document, &mut output)?;
        Ok(output.as_str().as_bytes().to_vec())
    }
}