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
//  Copyright (C) 2015 Steven Allen
//
//  This file is part of gazetta.
//
//  This program is free software: you can redistribute it and/or modify it under the terms of the
//  GNU General Public License as published by the Free Software Foundation version 3 of the
//  License.
//
//  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
//  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
//  the GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along with this program.  If
//  not, see <http://www.gnu.org/licenses/>.
//

use std::collections::HashMap;
use std::borrow::Cow;
use pulldown_cmark::{Parser, Event, OPTION_ENABLE_TABLES, OPTION_ENABLE_FOOTNOTES};
use horrorshow::prelude::*;

/// Markdown renderer
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Markdown<'a> {
    data: &'a str,
    base: &'a str,
}

impl<'a> Markdown<'a> {
    /// Create a new markdown renderer.
    ///
    /// `data` should contain the markdown to be rendered and `base` should specify a relative url
    /// prefix (for relative links and images).
    ///
    /// Note: `base` will only affect markdown links and images, not inline html ones.
    pub fn new(data: &'a str, base: &'a str) -> Markdown<'a> {
        Markdown {
            data: data,
            base: base,
        }
    }
}

impl<'a> RenderOnce for Markdown<'a> {
    #[inline]
    fn render_once(self, tmpl: &mut TemplateBuffer) {
        self.render(tmpl)
    }
}

impl<'a> RenderMut for Markdown<'a> {
    #[inline]
    fn render_mut(&mut self, tmpl: &mut TemplateBuffer) {
        self.render(tmpl)
    }
}

impl<'a> Render for Markdown<'a> {
    #[inline]
    fn render(&self, tmpl: &mut TemplateBuffer) {
        tmpl <<
        RenderMarkdown {
            footnotes: HashMap::new(),
            iter: Parser::new_ext(self.data, OPTION_ENABLE_TABLES | OPTION_ENABLE_FOOTNOTES),
            base: self.base,
        };
    }
}

struct RenderMarkdown<'a, I> {
    iter: I,
    footnotes: HashMap<Cow<'a, str>, u32>,
    base: &'a str,
}

impl<'a, I> RenderMarkdown<'a, I> {
    fn footnote(&mut self, name: Cow<'a, str>) -> u32 {
        let next_idx = (self.footnotes.len() as u32) + 1;
        *self.footnotes.entry(name).or_insert(next_idx)
    }

    fn make_relative<'b>(&self, dest: Cow<'b, str>) -> Cow<'b, str> {
        if dest.starts_with("./") {
            if self.base.is_empty() {
                match dest {
                    Cow::Borrowed(v) => Cow::Borrowed(&v[2..]),
                    Cow::Owned(mut v) => {
                        // There has to be a better way...
                        v.remove(0);
                        v.remove(0);
                        Cow::Owned(v)
                    }
                }
            } else {
                Cow::Owned(format!("{}/{}", self.base, &dest[2..]))
            }
        } else {
            dest
        }
    }
}

impl<'a, I: Iterator<Item = Event<'a>>> RenderOnce for RenderMarkdown<'a, I> {
    fn render_once(mut self, mut tmpl: &mut TemplateBuffer) {
        self.render_mut(tmpl)
    }
}

impl<'a, I: Iterator<Item = Event<'a>>> RenderMut for RenderMarkdown<'a, I> {
    fn render_mut(&mut self, mut tmpl: &mut TemplateBuffer) {
        use pulldown_cmark::Event::*;
        use pulldown_cmark::Tag;

        while let Some(event) = self.iter.next() {
            // manually reborrow
            let tmpl = &mut *tmpl;
            match event {
                Start(tag) => {
                    // Because rust doesn't reborrow? (WTF?)
                    let s: &mut Self = &mut *self;
                    match tag {
                        Tag::FootnoteDefinition(name) => tmpl << html! {
                            div(class="footnote", id=format_args!("footnote-{}", name)) {
                                sup(class="footnote-label") : s.footnote(name);
                                : s;
                            }
                        },
                        Tag::Paragraph => tmpl << html! { p : s },
                        Tag::Rule => {
                            // Eat the end tag.
                            match s.iter.next() {
                                Some(End(Tag::Rule)) => (),
                                _ => panic!("stuff inside horizontal rule tag?"),
                            }
                            tmpl << html! { hr; }
                        }
                        Tag::BlockQuote => tmpl << html! { blockquote : s },
                        Tag::Table(_) => tmpl << html! { table : s },
                        Tag::TableHead => tmpl << html! { thead { tr : s } },
                        Tag::TableRow => tmpl << html! { tr : s },
                        Tag::TableCell => tmpl << html! { td : s },
                        Tag::List(Some(0)) => tmpl << html! { ol : s },
                        Tag::List(Some(start)) => tmpl << html! { ol(start = start) : s },
                        Tag::List(None) => tmpl << html! { ul : s },
                        Tag::Item => tmpl << html! { li : s },
                        Tag::Emphasis => tmpl << html! { em: s },
                        Tag::Strong => tmpl << html! { strong: s },
                        Tag::Code => tmpl << html! { code: s },
                        Tag::Header(level) => match level {
                            1 => tmpl << html! { h1 : s },
                            2 => tmpl << html! { h2 : s },
                            3 => tmpl << html! { h3 : s },
                            4 => tmpl << html! { h4 : s },
                            5 => tmpl << html! { h5 : s },
                            6 => tmpl << html! { h6 : s },
                            _ => panic!(),
                        },
                        Tag::Link(dest, title) => tmpl << html! {
                            // TODO: Escape href?
                            a(href = &*s.make_relative(dest),
                              title? = if !title.is_empty() { Some(&*title) } else { None }) : s
                        },
                        Tag::Image(dest, title) => tmpl << html! {
                            img(src = &*s.make_relative(dest),
                                title? = if !title.is_empty() { Some(&*title) } else { None },
                                alt = FnRenderer::new(|tmpl| {
                                    let mut nest = 0;
                                    while let Some(event) = s.iter.next() {
                                        let tmpl = &mut *tmpl;
                                        match event {
                                            Start(_) => nest += 1,
                                            End(_) if nest == 0 => break,
                                            End(_) => nest -= 1,
                                            Text(txt) => tmpl << &*txt,
                                            SoftBreak | HardBreak => tmpl << " ",
                                            FootnoteReference(_) | InlineHtml(_) | Html(_) => (),
                                        }
                                    }
                                }))
                        },
                        Tag::CodeBlock(info) => {
                            // TODO Highlight code.
                            let lang = &*info.split(' ').next().unwrap();
                            // Why? Because the format_args and lifetimes...
                            match format_args!("language-{}", lang) {
                                f => tmpl << html! {
                                    pre {
                                        code(class? = if !lang.is_empty() {Some(f)} else {None}) : s
                                    }
                                }
                            }
                        }
                    }
                }
                End(_) => break,
                FootnoteReference(name) => tmpl << html! {
                    sup(class="footnote-reference") {
                        a(href=format_args!("{}/#footnote-{}", self.base, name)) : self.footnote(name);
                    }
                },
                Text(text) => tmpl << &*text,
                Html(html) | InlineHtml(html) => tmpl << Raw(html),
                SoftBreak => tmpl << "\n",
                HardBreak => tmpl << html! { br },
            };
        }
    }
}