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
use pulldown_cmark::{Alignment, CodeBlockKind, Event, Options, Parser, Tag};
use sauron::{
    html::{
        attributes::{attr, checked, class, href, id, src, title, type_},
        *,
    },
    Node,
};
use std::collections::HashMap;

/// parse markdown and return sauron virtual Node
pub fn markdown<MSG>(src: &str) -> Node<MSG> {
    let mut elems = render_markdown(src);
    if elems.len() == 1 {
        elems.pop().unwrap()
    } else {
        div(vec![], elems)
    }
}

/// Renders a string of Markdown to HTML with the default options (footnotes
/// disabled, tables enabled).
pub fn render_markdown<'a, MSG>(src: &'a str) -> Vec<Node<MSG>> {
    let mut elems = vec![];
    let mut spine = vec![];
    let mut numbers: HashMap<String, usize> = HashMap::new();

    // Add a child to the previous encountered element
    macro_rules! add_child {
        ($child:expr) => {{
            let l = spine.len();
            assert_ne!(l, 0);
            spine[l - 1]
                .as_element_mut()
                .expect("expecting an element")
                .add_children(vec![$child]);
        }};
    }

    for ev in Parser::new_ext(src, Options::all()) {
        match ev {
            Event::Start(tag) => {
                spine.push(make_tag(tag, &mut numbers));
            }
            Event::Text(content) => {
                add_child!(text(content));
            }
            Event::SoftBreak => add_child!(text("\n")),
            Event::HardBreak => add_child!(br(vec![], vec![])),
            Event::Code(code_str) => {
                add_child!(code(vec![], vec![text(code_str)]))
            }
            Event::Html(html) => {
                if let Ok(nodes) = sauron_parse::parse_simple(&html) {
                    for node in nodes {
                        add_child!(node);
                    }
                }
            }
            Event::FootnoteReference(name) => {
                let len = numbers.len() + 1;
                let number = numbers.entry(name.to_string()).or_insert(len);
                add_child!(sup(
                    vec![class("footnote-reference")],
                    vec![a(
                        vec![href(format!("#{}", name))],
                        vec![text(number)]
                    )]
                ));
            }
            Event::Rule => {
                add_child!(hr(vec![], vec![]));
            }
            Event::TaskListMarker(value) => {
                add_child!(input(
                    vec![type_("checkbox"), checked(value)],
                    vec![]
                ));
            }
            Event::End(tag) => {
                let l = spine.len();
                assert!(l >= 1);
                let mut top = spine.pop().unwrap();
                match tag {
                    Tag::CodeBlock(_codeblock) => {
                        top = pre(vec![], vec![top]);
                    }
                    Tag::Table(aligns) => {
                        if let Some(element) = top.as_element_mut() {
                            for r in element.children_mut() {
                                if let Some(r) = r.as_element_mut() {
                                    for (i, c) in
                                        r.children_mut().iter_mut().enumerate()
                                    {
                                        if let Some(tag) = c.as_element_mut() {
                                            match aligns[i] {
                                                Alignment::None => {}
                                                Alignment::Left => tag
                                                    .add_attributes(vec![
                                                        class("text-left"),
                                                    ]),
                                                Alignment::Center => tag
                                                    .add_attributes(vec![
                                                        class("text-center"),
                                                    ]),
                                                Alignment::Right => tag
                                                    .add_attributes(vec![
                                                        class("text-right"),
                                                    ]),
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Tag::TableHead => {
                        if let Some(element) = top.as_element_mut() {
                            for c in element.children_mut() {
                                if let Some(tag) = c.as_element_mut() {
                                    tag.set_tag("th");
                                    tag.add_attributes(vec![attr(
                                        "scope", "col",
                                    )]);
                                }
                            }
                        }
                    }
                    _ => (),
                }
                if l == 1 {
                    elems.push(top);
                } else {
                    spine[l - 2]
                        .as_element_mut()
                        .expect("expecting element")
                        .add_children(vec![top]);
                }
            }
        }
    }
    elems
}

fn make_tag<MSG>(t: Tag, numbers: &mut HashMap<String, usize>) -> Node<MSG> {
    match t {
        Tag::Paragraph => p(vec![], vec![]),
        Tag::Heading(n) => {
            assert!(n > 0);
            assert!(n < 7);
            match n {
                1 => h1(vec![], vec![]),
                2 => h2(vec![], vec![]),
                3 => h3(vec![], vec![]),
                4 => h4(vec![], vec![]),
                5 => h5(vec![], vec![]),
                6 => h6(vec![], vec![]),
                _ => unreachable!(),
            }
        }
        Tag::BlockQuote => blockquote(vec![class("blockquote")], vec![]),
        Tag::CodeBlock(codeblock) => match codeblock {
            CodeBlockKind::Indented => code(vec![], vec![]),
            CodeBlockKind::Fenced(fence) => {
                code(vec![class(fence.to_string())], vec![])
            }
        },
        Tag::List(None) => ul(vec![], vec![]),
        Tag::List(Some(1)) => ol(vec![], vec![]),
        Tag::List(Some(ref start)) => ol(vec![attr("start", *start)], vec![]),
        Tag::Item => li(vec![], vec![]),
        Tag::Table(_alignment) => table(vec![class("table")], vec![]),
        Tag::TableHead => th(vec![], vec![]),
        Tag::TableRow => tr(vec![], vec![]),
        Tag::TableCell => td(vec![], vec![]),
        Tag::Emphasis => span(vec![class("font-italic")], vec![]),
        Tag::Strong => span(vec![class("font-weight-bold")], vec![]),
        Tag::Strikethrough => s(vec![], vec![]),
        Tag::Link(_, ref _href, ref _title) => a(
            vec![href(_href.to_string()), title(_title.to_string())],
            vec![],
        ),
        Tag::Image(_, ref _src, ref _title) => img(
            vec![src(_src.to_string()), title(_title.to_string())],
            vec![],
        ),
        Tag::FootnoteDefinition(name) => {
            let len = numbers.len() + 1;
            let number = *numbers.entry(name.to_string()).or_insert(len);
            div(
                vec![class("footnote-definition"), id(name.to_string())],
                vec![sup(vec![class("footnote-label")], vec![text(number)])],
            )
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use sauron::Render;

    #[test]
    fn source_code() {
        let md = r#"
```rust
    fn main(){
        println!("Hello world!");
    }
```
        "#;
        let expected = "<pre>\n    <code class=\"rust\">    fn main(){\n        println!(\"Hello world!\");\n    }\n</code>\n</pre>";
        let view: Node<()> = markdown(md);

        let mut buffer = String::new();
        view.render(&mut buffer).unwrap();
        println!("view: {}", buffer);
        assert_eq!(expected, buffer);
    }

    #[test]
    fn code() {
        let md = r#"
This is has some `code` and other..
        "#;
        let expected = "<p>\n    This is has some \n    <code>code</code>\n     and other..\n</p>";
        let view: Node<()> = markdown(md);

        let mut buffer = String::new();
        view.render(&mut buffer).unwrap();
        println!("view: {}", buffer);
        assert_eq!(expected, buffer);
    }

    #[test]
    fn footnotes() {
        let md = r#"
### [Footnotes](https://github.com/markdown-it/markdown-it-footnote)

Footnote 1 link[^first].

Footnote 2 link[^second].

Inline footnote^[Text of inline footnote] definition.

Duplicated footnote reference[^second].

[^first]: Footnote **can have markup**

    and multiple paragraphs.

[^second]: Footnote text.
        "#;

        let expected = "<div>\n    <h3>\n        <a href=\"https://github.com/markdown-it/markdown-it-footnote\" title=\"\">Footnotes</a>\n    </h3>\n    <p>\n        Footnote 1 link\n        <sup class=\"footnote-reference\">\n            <a href=\"#first\">1</a>\n        </sup>\n        .\n    </p>\n    <p>\n        Footnote 2 link\n        <sup class=\"footnote-reference\">\n            <a href=\"#second\">2</a>\n        </sup>\n        .\n    </p>\n    <p>\n        Inline footnote^\n        [\n        Text of inline footnote\n        ]\n         definition.\n    </p>\n    <p>\n        Duplicated footnote reference\n        <sup class=\"footnote-reference\">\n            <a href=\"#second\">2</a>\n        </sup>\n        .\n    </p>\n    <div class=\"footnote-definition\" id=\"first\">\n        <sup class=\"footnote-label\">1</sup>\n        <p>\n            Footnote \n            <span class=\"font-weight-bold\">can have markup</span>\n        </p>\n    </div>\n    <pre>\n        <code>and multiple paragraphs.\n</code>\n    </pre>\n    <div class=\"footnote-definition\" id=\"second\">\n        <sup class=\"footnote-label\">2</sup>\n        <p>Footnote text.</p>\n    </div>\n</div>";
        let view: Node<()> = markdown(md);

        let mut buffer = String::new();
        view.render(&mut buffer).unwrap();
        println!("view: {}", buffer);
        assert_eq!(expected, buffer);
    }

    #[test]
    fn test_md_with_html() {
        let md = r#"
[Hello](link.html)
<img src="img.jpeg"/>"#;

        let expected =
            "<p>\n    <a href=\"link.html\" title=\"\">Hello</a>\n    \n\n    <img src=\"img.jpeg\"></img>\n</p>";
        let view: Node<()> = markdown(md);

        let mut buffer = String::new();
        view.render(&mut buffer).unwrap();
        println!("view: {}", buffer);
        assert_eq!(expected, buffer);
    }

    #[test]
    fn test_md() {
        let md = r#"
An h1 header
============
look like:
  * this one
  * that one
  * the other one"#;
        let view: Node<()> = markdown(md);

        let expected = r#"<div>
    <h1>An h1 header</h1>
    <p>look like:</p>
    <ul>
        <li>this one</li>
        <li>that one</li>
        <li>the other one</li>
    </ul>
</div>"#;

        let mut buffer = String::new();
        view.render(&mut buffer).unwrap();
        println!("view: {}", buffer);
        assert_eq!(expected, buffer);
    }

    #[test]
    fn test_md_links() {
        let md = r#"
[link text](http://dev.nodeca.com)

[link with title](http://nodeca.github.io/pica/demo/ "title text!")"#;
        let view: Node<()> = markdown(md);
        let expected = r#"<div>
    <p>
        <a href="http://dev.nodeca.com" title="">link text</a>
    </p>
    <p>
        <a href="http://nodeca.github.io/pica/demo/" title="title text!">link with title</a>
    </p>
</div>"#;

        let mut buffer = String::new();
        view.render(&mut buffer).unwrap();
        println!("view: {}", buffer);
        assert_eq!(expected, buffer);
    }

    #[test]
    fn test_md_tables() {
        let md = r#"
## Tables

| Option | Description |
|:------ | -----------:|
| data   | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext    | extension to be used for dest files. |
}
"#;
        let view: Node<()> = markdown(md);
        let expected = r#"<div>
    <h2>Tables</h2>
    <table class="table">
        <th>
            <th scope="col" class="text-left">Option</th>
            <th scope="col" class="text-right">Description</th>
        </th>
        <tr>
            <td class="text-left">data</td>
            <td class="text-right">path to data files to supply the data that will be passed into templates.</td>
        </tr>
        <tr>
            <td class="text-left">engine</td>
            <td class="text-right">engine to be used for processing templates. Handlebars is the default.</td>
        </tr>
        <tr>
            <td class="text-left">ext</td>
            <td class="text-right">extension to be used for dest files.</td>
        </tr>
        <tr>
            <td class="text-left">}</td>
            <td class="text-right"></td>
        </tr>
    </table>
</div>"#;

        let mut buffer = String::new();
        view.render(&mut buffer).unwrap();
        println!("view: {}", buffer);
        assert_eq!(expected, buffer);
    }
}