typlite/
common.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
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
//! Common types and interfaces for the conversion system

use cmark_writer::ast::Node;
use cmark_writer::custom_node;
use cmark_writer::CommonMarkWriter;
use cmark_writer::HtmlAttribute;
use cmark_writer::HtmlElement;
use cmark_writer::HtmlWriteResult;
use cmark_writer::HtmlWriter;
use cmark_writer::HtmlWriterOptions;
use cmark_writer::WriteResult;
use cmark_writer::WriterOptions;
use ecow::eco_format;
use ecow::EcoString;
use std::path::PathBuf;

use crate::Result;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListState {
    Ordered,
    Unordered,
}

/// Valid formats for the conversion.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
    #[default]
    Md,
    LaTeX,
    Text,
    #[cfg(feature = "docx")]
    Docx,
}

/// Figure node implementation for all formats
#[derive(Debug, PartialEq, Clone)]
#[custom_node(block = true, html_impl = true)]
pub struct FigureNode {
    /// The main content of the figure, can be any block node
    pub body: Box<Node>,
    /// The caption text for the figure
    pub caption: String,
}

impl FigureNode {
    fn write_custom(&self, writer: &mut CommonMarkWriter) -> WriteResult<()> {
        let mut temp_writer = CommonMarkWriter::with_options(writer.options.clone());
        temp_writer.write(&self.body)?;
        let content = temp_writer.into_string();
        writer.write_str(&content)?;
        writer.write_str("\n")?;
        writer.write_str(&self.caption)?;
        Ok(())
    }

    fn write_html_custom(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()> {
        let body = self.body.clone();
        let node = Node::HtmlElement(HtmlElement {
            tag: EcoString::inline("figure"),
            attributes: vec![HtmlAttribute {
                name: EcoString::inline("class"),
                value: EcoString::inline("figure"),
            }],
            children: vec![*body],
            self_closing: false,
        });
        writer.write_node(&node)?;
        Ok(())
    }
}

/// External Frame node for handling frames stored as external files
#[derive(Debug, PartialEq, Clone)]
#[custom_node(block = true, html_impl = true)]
pub struct ExternalFrameNode {
    /// The path to the external file containing the frame
    pub file_path: PathBuf,
    /// Alternative text for the frame
    pub alt_text: EcoString,
    /// Original SVG data (needed for DOCX that still embeds images)
    pub svg: String,
}

impl ExternalFrameNode {
    fn write_custom(&self, writer: &mut CommonMarkWriter) -> WriteResult<()> {
        // The actual handling is implemented in format-specific writers
        writer.write_str(&format!(
            "![{}]({})",
            self.alt_text,
            self.file_path.display()
        ))?;
        Ok(())
    }

    fn write_html_custom(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()> {
        let node = Node::HtmlElement(HtmlElement {
            tag: EcoString::inline("img"),
            attributes: vec![
                HtmlAttribute {
                    name: EcoString::inline("src"),
                    value: self.file_path.display().to_string().into(),
                },
                HtmlAttribute {
                    name: EcoString::inline("alt"),
                    value: self.alt_text.clone(),
                },
            ],
            children: vec![],
            self_closing: true,
        });
        writer.write_node(&node)?;
        Ok(())
    }
}

/// Highlight node for highlighted text
#[derive(Debug, PartialEq, Clone)]
#[custom_node(block = false, html_impl = true)]
pub struct HighlightNode {
    /// The content to be highlighted
    pub content: Vec<Node>,
}

impl HighlightNode {
    fn write_custom(&self, writer: &mut CommonMarkWriter) -> WriteResult<()> {
        let mut temp_writer = CommonMarkWriter::with_options(writer.options.clone());
        for node in &self.content {
            temp_writer.write(node)?;
        }
        let content = temp_writer.into_string();
        writer.write_str(&format!("=={}==", content))?;
        Ok(())
    }

    fn write_html_custom(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()> {
        let node = Node::HtmlElement(HtmlElement {
            tag: EcoString::inline("mark"),
            attributes: vec![],
            children: self.content.clone(),
            self_closing: false,
        });
        writer.write_node(&node)?;
        Ok(())
    }
}

/// Node for centered content
#[derive(Debug, PartialEq, Clone)]
#[custom_node(block = true, html_impl = true)]
pub struct CenterNode {
    /// The content to be centered
    pub node: Node,
}

impl CenterNode {
    pub fn new(children: Vec<Node>) -> Self {
        CenterNode {
            node: Node::HtmlElement(cmark_writer::ast::HtmlElement {
                tag: EcoString::inline("p"),
                attributes: vec![cmark_writer::ast::HtmlAttribute {
                    name: EcoString::inline("align"),
                    value: EcoString::inline("center"),
                }],
                children,
                self_closing: false,
            }),
        }
    }

    fn write_custom(&self, writer: &mut CommonMarkWriter) -> WriteResult<()> {
        let mut temp_writer = CommonMarkWriter::with_options(writer.options.clone());
        temp_writer.write(&self.node)?;
        let content = temp_writer.into_string();
        writer.write_str(&content)?;
        writer.write_str("\n")?;
        Ok(())
    }

    fn write_html_custom(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()> {
        let mut temp_writer = HtmlWriter::with_options(HtmlWriterOptions {
            strict: false,
            ..Default::default()
        });
        temp_writer.write_node(&self.node)?;
        let content = temp_writer.into_string();
        writer.write_str(&content)?;
        Ok(())
    }
}

/// Inline node for flattened inline content (useful for table cells)
#[derive(Debug, PartialEq, Clone)]
#[custom_node(block = false, html_impl = true)]
pub struct InlineNode {
    /// The inline content nodes
    pub content: Vec<Node>,
}

impl InlineNode {
    fn write_custom(&self, writer: &mut CommonMarkWriter) -> WriteResult<()> {
        for node in &self.content {
            writer.write(node)?;
        }
        Ok(())
    }

    fn write_html_custom(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()> {
        for node in &self.content {
            writer.write_node(node)?;
        }
        Ok(())
    }
}

/// Verbatim node for raw text output
#[derive(Debug, PartialEq, Clone)]
#[custom_node(block = true, html_impl = false)]
pub struct VerbatimNode {
    /// The content to directly output
    pub content: EcoString,
}

impl VerbatimNode {
    fn write_custom(&self, writer: &mut CommonMarkWriter) -> WriteResult<()> {
        writer.write_str(&self.content)?;
        Ok(())
    }
}

/// Alert node for alert messages
#[derive(Debug, PartialEq, Clone)]
#[custom_node(block = true, html_impl = false)]
pub struct AlertNode {
    /// The content of the alert
    pub content: Vec<Node>,
    /// The class of the alert
    pub class: EcoString,
}

impl AlertNode {
    fn write_custom(&self, writer: &mut CommonMarkWriter) -> WriteResult<()> {
        let quote = Node::BlockQuote(vec![
            Node::Paragraph(vec![Node::Text(eco_format!(
                "[!{}]",
                self.class.to_ascii_uppercase()
            ))]),
            Node::Paragraph(vec![Node::Text("".into())]),
        ]);
        let mut tmp_writer = CommonMarkWriter::with_options(WriterOptions {
            escape_special_chars: false,
            ..writer.options.clone()
        });
        tmp_writer.write(&quote)?;
        let mut content = tmp_writer.into_string();
        let quote = Node::BlockQuote(self.content.clone());
        let mut tmp_writer = CommonMarkWriter::with_options(writer.options.clone());
        tmp_writer.write(&quote)?;
        content += tmp_writer.into_string();
        writer.write_str(&content)?;
        Ok(())
    }
}

/// Common writer interface for different formats
pub trait FormatWriter {
    /// Write AST document to output format
    fn write_eco(&mut self, document: &Node, output: &mut EcoString) -> Result<()>;

    /// Write AST document to vector
    fn write_vec(&mut self, document: &Node) -> Result<Vec<u8>>;
}