// // imag - the personal information management suite for the commandline // Copyright (C) 2015-2018 Matthias Beyer and contributors // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; version // 2.1 of the License. // // This library 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 // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // use hoedown::{Markdown, Html as MdHtml}; use hoedown::renderer::html::Flags as HtmlFlags; use hoedown::renderer::Render; use error::Result; use error::MarkdownErrorKind; use error::ResultExt; pub type HTML = String; pub fn to_html(buffer: &str) -> Result { let md = Markdown::new(buffer); let mut html = MdHtml::new(HtmlFlags::empty(), 0); html.render(&md) .to_str() .map(String::from) .chain_err(|| MarkdownErrorKind::MarkdownRenderError) } pub mod iter { use error::Result; use libimagstore::store::Entry; use super::HTML; use super::to_html; pub struct ToHtmlIterator> { i: I } impl> ToHtmlIterator { pub fn new(i: I) -> ToHtmlIterator { ToHtmlIterator { i: i } } } impl> Iterator for ToHtmlIterator { type Item = Result; fn next(&mut self) -> Option { self.i.next().map(|entry| to_html(&entry.get_content()[..])) } } impl> From for ToHtmlIterator { fn from(obj: I) -> ToHtmlIterator { ToHtmlIterator::new(obj) } } /// Iterate over `(Entry, Result)` tuples pub struct WithHtmlIterator> { i: I } impl> WithHtmlIterator { pub fn new(i: I) -> WithHtmlIterator { WithHtmlIterator { i: i } } } impl> Iterator for WithHtmlIterator { type Item = (Entry, Result); fn next(&mut self) -> Option { self.i.next().map(|entry| { let html = to_html(&entry.get_content()[..]); (entry, html) }) } } }