From d03974270d55f153b17868a843ca7b4aed2b5464 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Wed, 29 Mar 2017 23:32:40 +0900 Subject: [PATCH] Add some default shortcodes --- src/lib.rs | 2 +- src/markdown.rs | 49 +++++++++++++++++++-------- src/site.rs | 6 +++- src/templates/shortcodes/gist.html | 3 ++ src/templates/shortcodes/vimeo.html | 4 +++ src/templates/shortcodes/youtube.html | 4 +++ test_site/content/posts/simple.md | 7 ++++ 7 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 src/templates/shortcodes/gist.html create mode 100644 src/templates/shortcodes/vimeo.html create mode 100644 src/templates/shortcodes/youtube.html diff --git a/src/lib.rs b/src/lib.rs index 22173992..6701114f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ mod site; mod markdown; mod section; -pub use site::Site; +pub use site::{Site, GUTENBERG_TERA}; pub use config::{Config, get_config}; pub use front_matter::{FrontMatter, split_content}; pub use page::{Page, populate_previous_and_next_pages}; diff --git a/src/markdown.rs b/src/markdown.rs index 5e49acd9..c11e4703 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -236,7 +236,7 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap, ter match error { Some(e) => Err(e), - None => Ok(html), + None => Ok(html.replace("

", "")), } } @@ -245,18 +245,12 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap, ter mod tests { use std::collections::HashMap; + use site::GUTENBERG_TERA; use tera::Tera; use config::Config; use super::{markdown_to_html, parse_shortcode}; - fn create_test_tera() -> Tera { - let mut tera = Tera::default(); - tera.add_raw_template("shortcodes/youtube.html", "Youtube video: {{id}}").unwrap(); - tera.add_raw_template("shortcodes/quote.html", "Quote: {{body}} - {{author}}").unwrap(); - tera - } - #[test] fn test_parse_simple_shortcode_one_arg() { let (name, args) = parse_shortcode(r#"{{ youtube(id="w7Ft2ymGmfc") }}"#); @@ -326,28 +320,53 @@ mod tests { } #[test] - fn test_markdown_to_html_simple_shortcode() { + fn test_markdown_to_html_with_shortcode() { let res = markdown_to_html(r#" Hello -{{ youtube(id="w7Ft2ymGmfc") }} - "#, &HashMap::new(), &create_test_tera(), &Config::default()).unwrap(); - assert_eq!(res, "

Hello\n

Youtube video: w7Ft2ymGmfc"); + +{{ youtube(id="ub36ffWAqgQ") }} + "#, &HashMap::new(), &GUTENBERG_TERA, &Config::default()).unwrap(); + assert!(res.contains("

Hello

\n
")); + assert!(res.contains(r#" +
diff --git a/src/templates/shortcodes/youtube.html b/src/templates/shortcodes/youtube.html new file mode 100644 index 00000000..5f10206f --- /dev/null +++ b/src/templates/shortcodes/youtube.html @@ -0,0 +1,4 @@ +
+ +
diff --git a/test_site/content/posts/simple.md b/test_site/content/posts/simple.md index 53ce0ad6..ee8367fc 100644 --- a/test_site/content/posts/simple.md +++ b/test_site/content/posts/simple.md @@ -5,3 +5,10 @@ date = "2017-04-01" +++ A simple page + +{{ youtube(id="e1C9kpMV2e8") }} +{{ youtube(id="e1C9kpMV2e8", autoplay=true) }} + +{{ vimeo(id="210073083") }} + +{{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}