use std::borrow::Cow::{Borrowed, Owned}; use self::cmark::{Event, Options, Parser, Tag}; use pulldown_cmark as cmark; use slug::slugify; use syntect::easy::HighlightLines; use syntect::html::{ start_highlighted_html_snippet, styled_line_to_highlighted_html, IncludeBackground, }; use config::highlighting::{get_highlighter, SYNTAX_SET, THEME_SET}; use errors::Result; use link_checker::check_url; use utils::site::resolve_internal_link; use context::RenderContext; use table_of_contents::{make_table_of_contents, Header, TempHeader}; const CONTINUE_READING: &str = "
\n"; #[derive(Debug)] pub struct Rendered { pub body: String, pub summary_len: Option"),
// Tag::Link is handled elsewhere
_ => return false,
}
true
}
fn end_tag(temp_header: &mut TempHeader, tag: &Tag) -> bool {
match tag {
Tag::Emphasis => temp_header.add_html("
"),
Tag::Strong => temp_header.add_html(""),
Tag::Code => temp_header.add_html(""),
Tag::Link(_, _) => temp_header.add_html(""),
_ => return false,
}
true
}
/// returns true if event have been processed
fn push_to_temp_header(event: &Event, temp_header: &mut TempHeader) -> bool {
match event {
Event::Start(tag) => start_tag(temp_header, tag),
Event::End(tag) => end_tag(temp_header, tag),
_ => false,
}
}
pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result or other things already there
temp_header.add_text(&text);
header_created = true;
return Event::Html(Borrowed(""));
}
// if we are in the middle of a code block
if let Some((ref mut highlighter, in_extra)) = highlighter {
let highlighted = if in_extra {
if let Some(ref extra) = context.config.extra_syntax_set {
highlighter.highlight(&text, &extra)
} else {
unreachable!("Got a highlighter from extra syntaxes but no extra?");
}
} else {
highlighter.highlight(&text, &SYNTAX_SET)
};
//let highlighted = &highlighter.highlight(&text, ss);
let html = styled_line_to_highlighted_html(&highlighted, background);
return Event::Html(Owned(html));
}
// Business as usual
Event::Text(text)
}
Event::Start(Tag::CodeBlock(ref info)) => {
if !context.config.highlight_code {
return Event::Html(Borrowed(""));
}
let theme = &THEME_SET.themes[&context.config.highlight_theme];
highlighter = Some(get_highlighter(info, &context.config));
// This selects the background color the same way that start_coloured_html_snippet does
let color =
theme.settings.background.unwrap_or(::syntect::highlighting::Color::WHITE);
background = IncludeBackground::IfDifferent(color);
let snippet = start_highlighted_html_snippet(theme);
Event::Html(Owned(snippet.0))
}
Event::End(Tag::CodeBlock(_)) => {
if !context.config.highlight_code {
return Event::Html(Borrowed("
\n"));
}
// reset highlight and close the code block
highlighter = None;
Event::Html(Borrowed(""))
}
Event::Start(Tag::Image(src, title)) => {
if is_colocated_asset_link(&src) {
return Event::Start(Tag::Image(
Owned(format!("{}{}", context.current_page_permalink, src)),
title,
));
}
Event::Start(Tag::Image(src, title))
}
Event::Start(Tag::Link(link, title)) => {
let fixed_link = match fix_link(&link, context) {
Ok(fixed_link) => fixed_link,
Err(err) => {
error = Some(err);
return Event::Html(Borrowed(""))
}
};
if in_header {
let html = if title.is_empty() {
format!("", fixed_link)
} else {
format!("", fixed_link, title)
};
temp_header.add_html(&html);
return Event::Html(Borrowed(""));
}
Event::Start(Tag::Link(Owned(fixed_link), title))
}
Event::Start(Tag::Header(num)) => {
in_header = true;
temp_header = TempHeader::new(num);
Event::Html(Borrowed(""))
}
Event::End(Tag::Header(_)) => {
// End of a header, reset all the things and return the header string
let id = find_anchor(&anchors, slugify(&temp_header.title), 0);
anchors.push(id.clone());
temp_header.permalink = format!("{}#{}", context.current_page_permalink, id);
temp_header.id = id;
in_header = false;
header_created = false;
let val = temp_header.to_string(context.tera, context.insert_anchor);
headers.push(temp_header.clone());
temp_header = TempHeader::default();
Event::Html(Owned(val))
}
Event::Html(ref markup) if markup.contains("") => {
has_summary = true;
Event::Html(Borrowed(CONTINUE_READING))
}
_ => event,
}
});
cmark::html::push_html(&mut html, parser);
}
if let Some(e) = error {
return Err(e);
} else {
Ok(Rendered {
summary_len: if has_summary { html.find(CONTINUE_READING) } else { None },
body: html,
toc: make_table_of_contents(&headers),
})
}
}