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
use std::marker::PhantomData;

use maud::RenderOnce;
use pulldown_cmark::{ Parser, Event };

use render;

/// The adapter that allows rendering markdown inside a `maud` macro.
///
/// # Examples
///
/// ```
/// # #![feature(plugin)]
/// # #![plugin(maud_macros)]
/// # extern crate maud;
/// # extern crate maud_pulldown_cmark;
/// # use maud_pulldown_cmark::Markdown;
/// # fn main() {
/// let markdown = "
///  1. A list
///  2. With some
///  3. <span>Inline html</span>
/// ";
///
/// let buffer = html! {
///   div {
///     (Markdown::from_string(markdown))
///   }
/// };
///
/// println!("{}", buffer.into_string());
/// # }
/// ```
///
/// ```
/// # #![feature(plugin)]
/// # #![plugin(maud_macros)]
/// # extern crate maud;
/// # extern crate pulldown_cmark;
/// # extern crate maud_pulldown_cmark;
/// # use pulldown_cmark::{ Parser, Event };
/// # use maud_pulldown_cmark::Markdown;
/// # fn main() {
/// let markdown = "
///  1. A list
///  2. With some
///  3. <span>Inline html</span>
/// ";
///
/// let events = Parser::new(markdown).map(|ev| match ev {
///   // Escape inline html
///   Event::Html(html) | Event::InlineHtml(html) => Event::Text(html),
///   _ => ev,
/// });
///
/// let buffer = html! {
///   div {
///     (Markdown::from_events(events))
///   }
/// };
///
/// println!("{}", buffer.into_string());
/// # }
/// ```
pub struct Markdown<'a, I: 'a + Iterator<Item=Event<'a>>> {
  events: I,
  config: render::Config,
  phantom: PhantomData<&'a I>,
}

impl<'a> Markdown<'a, Parser<'a>> {
  /// To allow rendering from a string.
  pub fn from_string(s: &'a str) -> Markdown<'a, Parser<'a>> {
    Markdown {
      events: Parser::new(s),
      config: render::Config {
        header_ids: false,
      },
      phantom: PhantomData,
    }
  }
}

impl<'a, I: 'a + Iterator<Item=Event<'a>>> Markdown<'a, I> {
  /// To allow rendering from a stream of events (useful for modifying the output of the general parser).
  pub fn from_events(events: I) -> Markdown<'a, I> {
    Markdown {
      events: events,
      config: render::Config {
        header_ids: false,
      },
      phantom: PhantomData,
    }
  }

  /// Generate ids for all headers, lowercases the text in the header and replaces spaces with -.
  ///
  /// # Examples
  ///
  /// ```
  /// # #![feature(plugin)]
  /// # #![plugin(maud_macros)]
  /// # extern crate maud;
  /// # extern crate maud_pulldown_cmark;
  /// # use maud_pulldown_cmark::Markdown;
  /// # fn main() {
  // TODO: Work out how to escape the # so this can go on the next line
  /// let markdown = "# Header
  /// ## A Sub Header
  /// ";
  ///
  /// let buffer = html!(
  ///   (Markdown::from_string(markdown).with_header_ids())
  /// );
  ///
  /// assert_eq!(buffer.into_string(), "<h1 id=\"header\">Header</h1>\n<h2 id=\"a-sub-header\">A Sub Header</h2>\n");
  /// # }
  /// ```
  pub fn with_header_ids(self) -> Markdown<'a, I> {
    Markdown {
      config: render::Config {
        header_ids: true,
        ..self.config
      },
      ..self
    }
  }
}

impl<'a, I: 'a + Iterator<Item=Event<'a>>> RenderOnce for Markdown<'a, I> {
  fn render_once_to(self, w: &mut String) {
    render::render_events(self.config, self.events, w);
  }
}

#[cfg(test)]
mod tests {
  use super::Markdown;
  use maud::RenderOnce;
  use pulldown_cmark::{ Parser, Event };

  impl<'a, I: 'a + Iterator<Item=Event<'a>>> Markdown<'a, I> {
    pub fn render(self) -> String {
      let mut buffer = String::new();
      self.render_once_to(&mut buffer);
      buffer
    }
  }

  #[test]
  pub fn test_from_string() {
    let markdown = "
 1. A list
 2. With some
 3. <span>Inline html</span>
    ";

    let buffer = Markdown::from_string(markdown).render();
    assert_eq!(buffer, "<ol>\n<li>A list</li>\n<li>With some</li>\n<li><span>Inline html</span></li>\n</ol>\n");
  }

  #[test]
  pub fn test_from_events() {
    let markdown = "
 1. A list
 2. With some
 3. <span>Inline html</span>
    ";

    let events = Parser::new(markdown).map(|ev| match ev {
      // Escape inline html
      Event::Html(html) | Event::InlineHtml(html) => Event::Text(html),
      _ => ev,
    });

    let buffer = Markdown::from_events(events).render();
    assert_eq!(buffer, "<ol>\n<li>A list</li>\n<li>With some</li>\n<li>&lt;span&gt;Inline html&lt;/span&gt;</li>\n</ol>\n");
  }

  #[test]
  pub fn test_without_header_ids() {
    let markdown = "
# Header
## A Sub Header
    ";

    let buffer = Markdown::from_string(markdown).render();
    assert_eq!(buffer, "<h1>Header</h1>\n<h2>A Sub Header</h2>\n");
  }

  #[test]
  pub fn test_with_header_ids() {
    let markdown = "
# Header
## A Sub Header
    ";

    let buffer = Markdown::from_string(markdown).with_header_ids().render();
    assert_eq!(buffer, "<h1 id=\"header\">Header</h1>\n<h2 id=\"a-sub-header\">A Sub Header</h2>\n");
  }

  #[test]
  pub fn test_with_header_ids_and_linked_inline_image() {
    let markdown = "
# Header [![an image](http://example.com/image)](http://example.com)
    ";

    let buffer = Markdown::from_string(markdown).with_header_ids().render();
    assert_eq!(buffer, "<h1 id=\"header-an-image\">Header <a href=\"http://example.com\"><img src=\"http://example.com/image\" alt=\"an image\" /></a></h1>\n");
  }
}