2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
|
2016-05-24 14:00:03 +00:00
|
|
|
use hoedown::{Markdown, Html as MdHtml};
|
|
|
|
use hoedown::renderer::html::Flags as HtmlFlags;
|
|
|
|
use hoedown::renderer::Render;
|
|
|
|
|
2018-10-30 17:40:52 +00:00
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::ResultExt;
|
|
|
|
use failure::Error;
|
|
|
|
use failure::err_msg;
|
2016-05-23 12:11:32 +00:00
|
|
|
|
|
|
|
pub type HTML = String;
|
|
|
|
|
|
|
|
pub fn to_html(buffer: &str) -> Result<HTML> {
|
2016-05-24 14:00:03 +00:00
|
|
|
let md = Markdown::new(buffer);
|
|
|
|
let mut html = MdHtml::new(HtmlFlags::empty(), 0);
|
|
|
|
html.render(&md)
|
|
|
|
.to_str()
|
|
|
|
.map(String::from)
|
2018-10-30 17:40:52 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.context(err_msg("Markdown rendering error"))
|
|
|
|
.map_err(Error::from)
|
2016-05-23 12:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod iter {
|
2018-10-30 17:40:52 +00:00
|
|
|
use failure::Fallible as Result;
|
2016-05-23 12:11:32 +00:00
|
|
|
use libimagstore::store::Entry;
|
|
|
|
use super::HTML;
|
|
|
|
use super::to_html;
|
|
|
|
|
|
|
|
pub struct ToHtmlIterator<I: Iterator<Item = Entry>> {
|
|
|
|
i: I
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I: Iterator<Item = Entry>> ToHtmlIterator<I> {
|
|
|
|
|
2017-02-05 14:02:38 +00:00
|
|
|
pub fn new(i: I) -> ToHtmlIterator<I> {
|
2016-05-23 12:11:32 +00:00
|
|
|
ToHtmlIterator { i: i }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I: Iterator<Item = Entry>> Iterator for ToHtmlIterator<I> {
|
|
|
|
type Item = Result<HTML>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.i.next().map(|entry| to_html(&entry.get_content()[..]))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-24 14:17:02 +00:00
|
|
|
impl<I: Iterator<Item = Entry>> From<I> for ToHtmlIterator<I> {
|
|
|
|
|
|
|
|
fn from(obj: I) -> ToHtmlIterator<I> {
|
|
|
|
ToHtmlIterator::new(obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-29 15:08:27 +00:00
|
|
|
|
|
|
|
/// Iterate over `(Entry, Result<HTML>)` tuples
|
|
|
|
pub struct WithHtmlIterator<I: Iterator<Item = Entry>> {
|
|
|
|
i: I
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I: Iterator<Item = Entry>> WithHtmlIterator<I> {
|
|
|
|
|
2017-02-05 14:29:22 +00:00
|
|
|
pub fn new(i: I) -> WithHtmlIterator<I> {
|
2016-05-29 15:08:27 +00:00
|
|
|
WithHtmlIterator { i: i }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I: Iterator<Item = Entry>> Iterator for WithHtmlIterator<I> {
|
|
|
|
type Item = (Entry, Result<HTML>);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.i.next().map(|entry| {
|
|
|
|
let html = to_html(&entry.get_content()[..]);
|
|
|
|
(entry, html)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:11:32 +00:00
|
|
|
}
|