Browse Source

Add some default shortcodes

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
d03974270d
7 changed files with 58 additions and 17 deletions
  1. +1
    -1
      src/lib.rs
  2. +34
    -15
      src/markdown.rs
  3. +5
    -1
      src/site.rs
  4. +3
    -0
      src/templates/shortcodes/gist.html
  5. +4
    -0
      src/templates/shortcodes/vimeo.html
  6. +4
    -0
      src/templates/shortcodes/youtube.html
  7. +7
    -0
      test_site/content/posts/simple.md

+ 1
- 1
src/lib.rs View File

@@ -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};


+ 34
- 15
src/markdown.rs View File

@@ -236,7 +236,7 @@ pub fn markdown_to_html(content: &str, permalinks: &HashMap<String, String>, ter

match error {
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 {
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, "<p>Hello\n</p>Youtube video: w7Ft2ymGmfc");

{{ youtube(id="ub36ffWAqgQ") }}
"#, &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]
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");
}

#[test]
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#"
Hello
{% quote(author="Keats") %}
A quote
{% end %}
"#, &HashMap::new(), &create_test_tera(), &Config::default()).unwrap();
assert_eq!(res, "<p>Hello\n</p>Quote: A quote - Keats");
"#, &HashMap::new(), &tera, &Config::default()).unwrap();
assert_eq!(res, "<p>Hello\n</p><blockquote>A quote - Keats</blockquote>");
}
}

+ 5
- 1
src/site.rs View File

@@ -16,12 +16,16 @@ use section::{Section};


lazy_static! {
static ref GUTENBERG_TERA: Tera = {
pub static ref GUTENBERG_TERA: Tera = {
let mut tera = Tera::default();
tera.add_raw_templates(vec![
("rss.xml", include_str!("templates/rss.xml")),
("sitemap.xml", include_str!("templates/sitemap.xml")),
("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();
tera
};


+ 3
- 0
src/templates/shortcodes/gist.html View File

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

+ 4
- 0
src/templates/shortcodes/vimeo.html View File

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

+ 4
- 0
src/templates/shortcodes/youtube.html 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>

+ 7
- 0
test_site/content/posts/simple.md View File

@@ -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") }}

Loading…
Cancel
Save