Add some default shortcodes

This commit is contained in:
Vincent Prouillet 2017-03-29 23:32:40 +09:00
parent a6b8caf6de
commit d03974270d
7 changed files with 58 additions and 17 deletions

View file

@ -26,7 +26,7 @@ mod site;
mod markdown; mod markdown;
mod section; mod section;
pub use site::Site; pub use site::{Site, GUTENBERG_TERA};
pub use config::{Config, get_config}; pub use config::{Config, get_config};
pub use front_matter::{FrontMatter, split_content}; pub use front_matter::{FrontMatter, split_content};
pub use page::{Page, populate_previous_and_next_pages}; pub use page::{Page, populate_previous_and_next_pages};

View file

@ -236,7 +236,7 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
match error { match error {
Some(e) => Err(e), Some(e) => Err(e),
None => Ok(html), None => Ok(html.replace("<p></p>", "")),
} }
} }
@ -245,18 +245,12 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter
mod tests { mod tests {
use std::collections::HashMap; use std::collections::HashMap;
use site::GUTENBERG_TERA;
use tera::Tera; use tera::Tera;
use config::Config; use config::Config;
use super::{markdown_to_html, parse_shortcode}; 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] #[test]
fn test_parse_simple_shortcode_one_arg() { fn test_parse_simple_shortcode_one_arg() {
let (name, args) = parse_shortcode(r#"{{ youtube(id="w7Ft2ymGmfc") }}"#); let (name, args) = parse_shortcode(r#"{{ youtube(id="w7Ft2ymGmfc") }}"#);
@ -326,28 +320,53 @@ mod tests {
} }
#[test] #[test]
fn test_markdown_to_html_simple_shortcode() { fn test_markdown_to_html_with_shortcode() {
let res = markdown_to_html(r#" let res = markdown_to_html(r#"
Hello Hello
{{ youtube(id="w7Ft2ymGmfc") }}
"#, &HashMap::new(), &create_test_tera(), &Config::default()).unwrap(); {{ youtube(id="ub36ffWAqgQ") }}
assert_eq!(res, "<p>Hello\n</p>Youtube video: w7Ft2ymGmfc"); "#, &HashMap::new(), &GUTENBERG_TERA, &Config::default()).unwrap();
assert!(res.contains("<p>Hello</p>\n<div >"));
assert!(res.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
}
#[test]
fn test_markdown_to_html_with_several_shortcode_in_row() {
let res = markdown_to_html(r#"
Hello
{{ youtube(id="ub36ffWAqgQ") }}
{{ youtube(id="ub36ffWAqgQ", autoplay=true) }}
{{ vimeo(id="210073083") }}
{{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}
"#, &HashMap::new(), &GUTENBERG_TERA, &Config::default()).unwrap();
assert!(res.contains("<p>Hello</p>\n<div >"));
assert!(res.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ""#));
assert!(res.contains(r#"<iframe src="https://www.youtube.com/embed/ub36ffWAqgQ?autoplay=1""#));
assert!(res.contains(r#"//player.vimeo.com/video/210073083""#));
} }
#[test] #[test]
fn test_markdown_to_html_shortcode_in_code_block() { fn test_markdown_to_html_shortcode_in_code_block() {
let res = markdown_to_html(r#"```{{ youtube(id="w7Ft2ymGmfc") }}```"#, &HashMap::new(), &create_test_tera(), &Config::default()).unwrap(); let res = markdown_to_html(r#"```{{ youtube(id="w7Ft2ymGmfc") }}```"#, &HashMap::new(), &GUTENBERG_TERA, &Config::default()).unwrap();
assert_eq!(res, "<p><code>{{ youtube(id=&quot;w7Ft2ymGmfc&quot;) }}</code></p>\n"); assert_eq!(res, "<p><code>{{ youtube(id=&quot;w7Ft2ymGmfc&quot;) }}</code></p>\n");
} }
#[test] #[test]
fn test_markdown_to_html_shortcode_with_body() { fn test_markdown_to_html_shortcode_with_body() {
let mut tera = Tera::default();
tera.extend(&GUTENBERG_TERA).unwrap();
tera.add_raw_template("shortcodes/quote.html", "<blockquote>{{ body }} - {{ author}}</blockquote>").unwrap();
let res = markdown_to_html(r#" let res = markdown_to_html(r#"
Hello Hello
{% quote(author="Keats") %} {% quote(author="Keats") %}
A quote A quote
{% end %} {% end %}
"#, &HashMap::new(), &create_test_tera(), &Config::default()).unwrap(); "#, &HashMap::new(), &tera, &Config::default()).unwrap();
assert_eq!(res, "<p>Hello\n</p>Quote: A quote - Keats"); assert_eq!(res, "<p>Hello\n</p><blockquote>A quote - Keats</blockquote>");
} }
} }

View file

@ -16,12 +16,16 @@ use section::{Section};
lazy_static! { lazy_static! {
static ref GUTENBERG_TERA: Tera = { pub static ref GUTENBERG_TERA: Tera = {
let mut tera = Tera::default(); let mut tera = Tera::default();
tera.add_raw_templates(vec![ tera.add_raw_templates(vec![
("rss.xml", include_str!("templates/rss.xml")), ("rss.xml", include_str!("templates/rss.xml")),
("sitemap.xml", include_str!("templates/sitemap.xml")), ("sitemap.xml", include_str!("templates/sitemap.xml")),
("robots.txt", include_str!("templates/robots.txt")), ("robots.txt", include_str!("templates/robots.txt")),
("shortcodes/youtube.html", include_str!("templates/shortcodes/youtube.html")),
("shortcodes/vimeo.html", include_str!("templates/shortcodes/vimeo.html")),
("shortcodes/gist.html", include_str!("templates/shortcodes/gist.html")),
]).unwrap(); ]).unwrap();
tera tera
}; };

View file

@ -0,0 +1,3 @@
<div>
<script src="{{ url }}.js{% if file %}?file={{file}}{% endif %}"></script>
</div>

View file

@ -0,0 +1,4 @@
<div {% if class %}class="{{class}}"{% endif %}>
<iframe src="//player.vimeo.com/video/{{id}}" webkitallowfullscreen mozallowfullscreen allowfullscreen>
</iframe>
</div>

View file

@ -0,0 +1,4 @@
<div {% if class %}class="{{class}}"{% endif %}>
<iframe src="https://www.youtube.com/embed/{{id}}{% if autoplay %}?autoplay=1{% endif %}" webkitallowfullscreen mozallowfullscreen allowfullscreen>
</iframe>
</div>

View file

@ -5,3 +5,10 @@ date = "2017-04-01"
+++ +++
A simple page A simple page
{{ youtube(id="e1C9kpMV2e8") }}
{{ youtube(id="e1C9kpMV2e8", autoplay=true) }}
{{ vimeo(id="210073083") }}
{{ gist(url="https://gist.github.com/Keats/32d26f699dcc13ebd41b") }}