2017-03-07 12:34:31 +00:00
|
|
|
use std::borrow::Cow::Owned;
|
|
|
|
|
|
|
|
use pulldown_cmark as cmark;
|
2017-04-19 13:16:21 +00:00
|
|
|
use self::cmark::{Parser, Event, Tag, Options, OPTION_ENABLE_TABLES, OPTION_ENABLE_FOOTNOTES};
|
2017-04-06 07:07:18 +00:00
|
|
|
use slug::slugify;
|
2017-03-07 12:34:31 +00:00
|
|
|
use syntect::easy::HighlightLines;
|
|
|
|
use syntect::html::{start_coloured_html_snippet, styles_to_coloured_html, IncludeBackground};
|
2017-05-22 11:28:43 +00:00
|
|
|
use tera::{Context as TeraContext};
|
2017-03-27 14:17:33 +00:00
|
|
|
|
2017-07-01 07:47:41 +00:00
|
|
|
use errors::Result;
|
|
|
|
use utils::site::resolve_internal_link;
|
2017-05-22 11:28:43 +00:00
|
|
|
use front_matter::InsertAnchor;
|
2017-07-01 07:47:41 +00:00
|
|
|
use context::Context;
|
|
|
|
use highlighting::{SYNTAX_SET, THEME_SET};
|
|
|
|
use short_code::{SHORTCODE_RE, ShortCode, parse_shortcode, render_simple_shortcode};
|
|
|
|
use table_of_contents::{TempHeader, Header, make_table_of_contents};
|
2017-03-07 12:34:31 +00:00
|
|
|
|
2017-05-22 11:28:43 +00:00
|
|
|
|
2017-06-16 04:00:48 +00:00
|
|
|
pub fn markdown_to_html(content: &str, context: &Context) -> Result<(String, Vec<Header>)> {
|
2017-03-27 14:17:33 +00:00
|
|
|
// We try to be smart about highlighting code as it can be time-consuming
|
|
|
|
// If the global config disables it, then we do nothing. However,
|
|
|
|
// if we see a code block in the content, we assume that this page needs
|
|
|
|
// to be highlighted. It could potentially have false positive if the content
|
|
|
|
// has ``` in it but that seems kind of unlikely
|
2017-05-22 11:28:43 +00:00
|
|
|
let should_highlight = if context.highlight_code {
|
2017-03-27 14:17:33 +00:00
|
|
|
content.contains("```")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
// Set while parsing
|
|
|
|
let mut error = None;
|
|
|
|
let mut highlighter: Option<HighlightLines> = None;
|
|
|
|
let mut shortcode_block = None;
|
|
|
|
// shortcodes live outside of paragraph so we need to ensure we don't close
|
|
|
|
// a paragraph that has already been closed
|
|
|
|
let mut added_shortcode = false;
|
|
|
|
// Don't transform things that look like shortcodes in code blocks
|
|
|
|
let mut in_code_block = false;
|
2017-04-06 07:07:18 +00:00
|
|
|
// If we get text in header, we need to insert the id and a anchor
|
|
|
|
let mut in_header = false;
|
2017-04-22 02:40:11 +00:00
|
|
|
// pulldown_cmark can send several text events for a title if there are markdown
|
|
|
|
// specific characters like `!` in them. We only want to insert the anchor the first time
|
|
|
|
let mut header_already_inserted = false;
|
2017-06-16 04:00:48 +00:00
|
|
|
let mut anchors: Vec<String> = vec![];
|
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
// the rendered html
|
|
|
|
let mut html = String::new();
|
2017-04-06 07:07:18 +00:00
|
|
|
|
|
|
|
// We might have cases where the slug is already present in our list of anchor
|
|
|
|
// for example an article could have several titles named Example
|
|
|
|
// We add a counter after the slug if the slug is already present, which
|
|
|
|
// means we will have example, example-1, example-2 etc
|
2017-04-22 03:35:11 +00:00
|
|
|
fn find_anchor(anchors: &[String], name: String, level: u8) -> String {
|
2017-04-06 07:07:18 +00:00
|
|
|
if level == 0 && !anchors.contains(&name) {
|
|
|
|
return name.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
let new_anchor = format!("{}-{}", name, level + 1);
|
|
|
|
if !anchors.contains(&new_anchor) {
|
|
|
|
return new_anchor;
|
|
|
|
}
|
|
|
|
|
|
|
|
find_anchor(anchors, name, level + 1)
|
|
|
|
}
|
2017-03-27 14:17:33 +00:00
|
|
|
|
2017-06-16 04:00:48 +00:00
|
|
|
let mut headers = vec![];
|
|
|
|
// Defaults to a 0 level so not a real header
|
|
|
|
// It should be an Option ideally but not worth the hassle to update
|
|
|
|
let mut temp_header = TempHeader::default();
|
|
|
|
|
2017-04-19 13:16:21 +00:00
|
|
|
let mut opts = Options::empty();
|
|
|
|
opts.insert(OPTION_ENABLE_TABLES);
|
|
|
|
opts.insert(OPTION_ENABLE_FOOTNOTES);
|
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
{
|
2017-04-19 13:16:21 +00:00
|
|
|
let parser = Parser::new_ext(content, opts).map(|event| match event {
|
2017-03-07 12:34:31 +00:00
|
|
|
Event::Text(text) => {
|
|
|
|
// if we are in the middle of a code block
|
2017-03-27 14:17:33 +00:00
|
|
|
if let Some(ref mut highlighter) = highlighter {
|
2017-03-07 12:34:31 +00:00
|
|
|
let highlighted = &highlighter.highlight(&text);
|
|
|
|
let html = styles_to_coloured_html(highlighted, IncludeBackground::Yes);
|
2017-03-27 14:17:33 +00:00
|
|
|
return Event::Html(Owned(html));
|
|
|
|
}
|
|
|
|
|
|
|
|
if in_code_block {
|
|
|
|
return Event::Text(text);
|
2017-03-07 12:34:31 +00:00
|
|
|
}
|
2017-03-27 14:17:33 +00:00
|
|
|
|
|
|
|
// Shortcode without body
|
2017-04-22 03:35:11 +00:00
|
|
|
if shortcode_block.is_none() && text.starts_with("{{") && text.ends_with("}}") && SHORTCODE_RE.is_match(&text) {
|
|
|
|
let (name, args) = parse_shortcode(&text);
|
|
|
|
added_shortcode = true;
|
2017-05-22 11:28:43 +00:00
|
|
|
match render_simple_shortcode(context.tera, &name, &args) {
|
2017-04-22 03:35:11 +00:00
|
|
|
Ok(s) => return Event::Html(Owned(format!("</p>{}", s))),
|
|
|
|
Err(e) => {
|
|
|
|
error = Some(e);
|
|
|
|
return Event::Html(Owned("".to_string()));
|
2017-03-27 14:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// non-matching will be returned normally below
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shortcode with a body
|
|
|
|
if shortcode_block.is_none() && text.starts_with("{%") && text.ends_with("%}") {
|
|
|
|
if SHORTCODE_RE.is_match(&text) {
|
|
|
|
let (name, args) = parse_shortcode(&text);
|
|
|
|
shortcode_block = Some(ShortCode::new(&name, args));
|
|
|
|
}
|
|
|
|
// Don't return anything
|
|
|
|
return Event::Text(Owned("".to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have some text while in a shortcode, it's either the body
|
|
|
|
// or the end tag
|
|
|
|
if shortcode_block.is_some() {
|
|
|
|
if let Some(ref mut shortcode) = shortcode_block {
|
|
|
|
if text.trim() == "{% end %}" {
|
|
|
|
added_shortcode = true;
|
2017-05-22 11:28:43 +00:00
|
|
|
match shortcode.render(context.tera) {
|
2017-03-27 14:17:33 +00:00
|
|
|
Ok(s) => return Event::Html(Owned(format!("</p>{}", s))),
|
|
|
|
Err(e) => {
|
|
|
|
error = Some(e);
|
|
|
|
return Event::Html(Owned("".to_string()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
shortcode.append(&text);
|
|
|
|
return Event::Html(Owned("".to_string()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 07:07:18 +00:00
|
|
|
if in_header {
|
2017-04-22 02:40:11 +00:00
|
|
|
if header_already_inserted {
|
|
|
|
return Event::Text(text);
|
|
|
|
}
|
2017-04-06 07:07:18 +00:00
|
|
|
let id = find_anchor(&anchors, slugify(&text), 0);
|
|
|
|
anchors.push(id.clone());
|
2017-05-22 11:28:43 +00:00
|
|
|
let anchor_link = if context.should_insert_anchor() {
|
|
|
|
let mut c = TeraContext::new();
|
|
|
|
c.add("id", &id);
|
|
|
|
context.tera.render("anchor-link.html", &c).unwrap()
|
2017-04-06 08:07:53 +00:00
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
2017-06-16 04:00:48 +00:00
|
|
|
// update the header and add it to the list
|
|
|
|
temp_header.id = id.clone();
|
|
|
|
temp_header.title = text.clone().into_owned();
|
|
|
|
temp_header.permalink = format!("{}#{}", context.current_page_permalink, id);
|
|
|
|
headers.push(temp_header.clone());
|
|
|
|
temp_header = TempHeader::default();
|
|
|
|
|
2017-04-22 02:40:11 +00:00
|
|
|
header_already_inserted = true;
|
2017-05-22 11:28:43 +00:00
|
|
|
let event = match context.insert_anchor {
|
|
|
|
InsertAnchor::Left => Event::Html(Owned(format!(r#"id="{}">{}{}"#, id, anchor_link, text))),
|
|
|
|
InsertAnchor::Right => Event::Html(Owned(format!(r#"id="{}">{}{}"#, id, text, anchor_link))),
|
|
|
|
InsertAnchor::None => Event::Html(Owned(format!(r#"id="{}">{}"#, id, text)))
|
|
|
|
};
|
|
|
|
return event;
|
2017-04-06 07:07:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
// Business as usual
|
|
|
|
Event::Text(text)
|
2017-03-07 12:34:31 +00:00
|
|
|
},
|
|
|
|
Event::Start(Tag::CodeBlock(ref info)) => {
|
2017-03-27 14:17:33 +00:00
|
|
|
in_code_block = true;
|
|
|
|
if !should_highlight {
|
|
|
|
return Event::Html(Owned("<pre><code>".to_owned()));
|
|
|
|
}
|
2017-05-22 11:28:43 +00:00
|
|
|
let theme = &THEME_SET.themes[&context.highlight_theme];
|
2017-06-20 04:29:14 +00:00
|
|
|
highlighter = SYNTAX_SET.with(|ss| {
|
|
|
|
let syntax = info
|
|
|
|
.split(' ')
|
|
|
|
.next()
|
|
|
|
.and_then(|lang| ss.find_syntax_by_token(lang))
|
|
|
|
.unwrap_or_else(|| ss.find_syntax_plain_text());
|
|
|
|
Some(HighlightLines::new(syntax, theme))
|
|
|
|
});
|
2017-03-22 11:59:49 +00:00
|
|
|
let snippet = start_coloured_html_snippet(theme);
|
2017-03-27 14:17:33 +00:00
|
|
|
Event::Html(Owned(snippet))
|
2017-03-07 12:34:31 +00:00
|
|
|
},
|
|
|
|
Event::End(Tag::CodeBlock(_)) => {
|
2017-03-27 14:17:33 +00:00
|
|
|
in_code_block = false;
|
|
|
|
if !should_highlight{
|
|
|
|
return Event::Html(Owned("</code></pre>\n".to_owned()))
|
|
|
|
}
|
2017-03-07 12:34:31 +00:00
|
|
|
// reset highlight and close the code block
|
2017-03-27 14:17:33 +00:00
|
|
|
highlighter = None;
|
|
|
|
Event::Html(Owned("</pre>".to_owned()))
|
2017-03-07 12:34:31 +00:00
|
|
|
},
|
2017-03-30 05:48:07 +00:00
|
|
|
// Need to handle relative links
|
|
|
|
Event::Start(Tag::Link(ref link, ref title)) => {
|
2017-05-01 07:04:41 +00:00
|
|
|
if in_header {
|
|
|
|
return Event::Html(Owned("".to_owned()));
|
|
|
|
}
|
2017-03-30 05:48:07 +00:00
|
|
|
if link.starts_with("./") {
|
2017-05-22 11:28:43 +00:00
|
|
|
match resolve_internal_link(link, context.permalinks) {
|
2017-05-17 10:04:26 +00:00
|
|
|
Ok(url) => {
|
2017-04-06 07:33:59 +00:00
|
|
|
return Event::Start(Tag::Link(Owned(url), title.clone()));
|
|
|
|
},
|
2017-05-17 10:04:26 +00:00
|
|
|
Err(_) => {
|
2017-03-30 05:48:07 +00:00
|
|
|
error = Some(format!("Relative link {} not found.", link).into());
|
|
|
|
return Event::Html(Owned("".to_string()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-22 03:35:11 +00:00
|
|
|
Event::Start(Tag::Link(link.clone(), title.clone()))
|
2017-03-30 05:48:07 +00:00
|
|
|
},
|
2017-05-01 07:04:41 +00:00
|
|
|
Event::End(Tag::Link(_, _)) => {
|
|
|
|
if in_header {
|
|
|
|
return Event::Html(Owned("".to_owned()));
|
|
|
|
}
|
|
|
|
event
|
|
|
|
}
|
2017-03-30 05:48:07 +00:00
|
|
|
// need to know when we are in a code block to disable shortcodes in them
|
2017-03-27 14:17:33 +00:00
|
|
|
Event::Start(Tag::Code) => {
|
|
|
|
in_code_block = true;
|
|
|
|
event
|
|
|
|
},
|
|
|
|
Event::End(Tag::Code) => {
|
|
|
|
in_code_block = false;
|
|
|
|
event
|
|
|
|
},
|
2017-04-06 07:07:18 +00:00
|
|
|
Event::Start(Tag::Header(num)) => {
|
|
|
|
in_header = true;
|
2017-06-16 04:00:48 +00:00
|
|
|
temp_header = TempHeader::new(num);
|
2017-04-06 07:07:18 +00:00
|
|
|
// ugly eh
|
2017-04-22 03:35:11 +00:00
|
|
|
Event::Html(Owned(format!("<h{} ", num)))
|
2017-04-06 07:07:18 +00:00
|
|
|
},
|
|
|
|
Event::End(Tag::Header(_)) => {
|
|
|
|
in_header = false;
|
2017-04-22 02:40:11 +00:00
|
|
|
header_already_inserted = false;
|
2017-04-06 07:07:18 +00:00
|
|
|
event
|
|
|
|
},
|
2017-03-30 05:48:07 +00:00
|
|
|
// If we added shortcodes, don't close a paragraph since there's none
|
2017-03-27 14:17:33 +00:00
|
|
|
Event::End(Tag::Paragraph) => {
|
|
|
|
if added_shortcode {
|
|
|
|
added_shortcode = false;
|
|
|
|
return Event::Html(Owned("".to_owned()));
|
|
|
|
}
|
|
|
|
event
|
|
|
|
},
|
2017-03-30 05:48:07 +00:00
|
|
|
// Ignore softbreaks inside shortcodes
|
2017-03-27 14:17:33 +00:00
|
|
|
Event::SoftBreak => {
|
|
|
|
if shortcode_block.is_some() {
|
|
|
|
return Event::Html(Owned("".to_owned()));
|
|
|
|
}
|
|
|
|
event
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
// println!("event = {:?}", event);
|
|
|
|
event
|
|
|
|
},
|
|
|
|
});
|
2017-03-07 12:34:31 +00:00
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
cmark::html::push_html(&mut html, parser);
|
2017-03-07 12:34:31 +00:00
|
|
|
}
|
2017-03-23 05:11:24 +00:00
|
|
|
|
2017-03-27 14:17:33 +00:00
|
|
|
match error {
|
|
|
|
Some(e) => Err(e),
|
2017-06-16 04:00:48 +00:00
|
|
|
None => Ok((html.replace("<p></p>", ""), make_table_of_contents(headers))),
|
2017-03-27 14:17:33 +00:00
|
|
|
}
|
2017-03-07 12:34:31 +00:00
|
|
|
}
|