Browse Source

Do not ignore shortcodes without args

index-subcmd
Vincent Prouillet 6 years ago
parent
commit
ca5bdc8f3c
7 changed files with 56 additions and 35 deletions
  1. +5
    -0
      CHANGELOG.md
  2. +29
    -29
      Cargo.lock
  3. +1
    -0
      components/rendering/src/markdown.rs
  4. +14
    -6
      components/rendering/src/short_code.rs
  5. +4
    -0
      components/site/test_site/content/posts/python.md
  6. +1
    -0
      components/site/test_site/templates/shortcodes/basic.html
  7. +2
    -0
      components/site/tests/site.rs

+ 5
- 0
CHANGELOG.md View File

@@ -1,5 +1,10 @@
# Changelog

## 0.2.2 (unreleased)

- Fix shortcodes without arguments being ignored


## 0.2.1 (2017-10-17)

- Fix `base-url` argument to `gutenberg build` being called `base`


+ 29
- 29
Cargo.lock View File

@@ -12,7 +12,7 @@ name = "aho-corasick"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
@@ -87,7 +87,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
@@ -239,8 +239,8 @@ dependencies = [
"chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"errors 0.1.0",
"rendering 0.1.0",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]

@@ -253,7 +253,7 @@ dependencies = [
"front_matter 0.1.0",
"rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rendering 0.1.0",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"slug 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"tera 0.10.10 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -359,8 +359,8 @@ dependencies = [
"errors 0.1.0",
"lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"tera 0.10.10 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -585,7 +585,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "memchr"
version = "1.0.1"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -704,7 +704,7 @@ name = "nom"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
@@ -793,8 +793,8 @@ dependencies = [
"content 0.1.0",
"errors 0.1.0",
"front_matter 0.1.0",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"tera 0.10.10 (registry+https://github.com/rust-lang/crates.io-index)",
"utils 0.1.0",
]
@@ -827,7 +827,7 @@ dependencies = [
"base64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"xml-rs 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

@@ -922,7 +922,7 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -942,8 +942,8 @@ dependencies = [
"lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"pulldown-cmark 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"slug 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syntect 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"templates 0.1.0",
@@ -1007,12 +1007,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "serde"
version = "1.0.15"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "serde_derive"
version = "1.0.15"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1037,7 +1037,7 @@ dependencies = [
"dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
@@ -1057,8 +1057,8 @@ dependencies = [
"pagination 0.1.0",
"rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"sass-rs 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"taxonomies 0.1.0",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"templates 0.1.0",
@@ -1132,8 +1132,8 @@ dependencies = [
"onig 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"plist 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1189,8 +1189,8 @@ dependencies = [
"content 0.1.0",
"errors 0.1.0",
"front_matter 0.1.0",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"slug 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tera 0.10.10 (registry+https://github.com/rust-lang/crates.io-index)",
"utils 0.1.0",
@@ -1230,7 +1230,7 @@ dependencies = [
"lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"pest 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"slug 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1307,7 +1307,7 @@ name = "toml"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
@@ -1542,7 +1542,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum magenta 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4bf0336886480e671965f794bc9b6fce88503563013d1bfb7a502c81fe3ac527"
"checksum magenta-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "40d014c7011ac470ae28e2f76a02bfea4a8480f73e701353b49ad7a8d75f4699"
"checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376"
"checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4"
"checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a"
"checksum mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0"
"checksum miniz-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "609ce024854aeb19a0ef7567d348aaa5a746b32fb72e336df7fcc16869d7e2b4"
"checksum mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a637d1ca14eacae06296a008fa7ad955347e34efcb5891cfd8ba05491a37907e"
@@ -1587,8 +1587,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum sass-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49e6025ff142eb235157aea8d6ee8085d9f5b80e280cb54b911f6b778e64075a"
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum sequence_trie 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c915714ca833b1d4d6b8f6a9d72a3ff632fe45b40a8d184ef79c81bec6327eed"
"checksum serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "6a7046c9d4c6c522d10b2d098f9bebe2bef227e0e74044d8c1bfcf6b476af799"
"checksum serde_derive 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "1afcaae083fd1c46952a315062326bc9957f182358eb7da03b57ef1c688f7aa9"
"checksum serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "e11a631f964d4e6572712ea12075fb1d65eeef42b0058884195b430ac1e26809"
"checksum serde_derive 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "1a51d54c805fbc8e12b603d1ba51eaed3195862976be468888ab0e4995d0000e"
"checksum serde_derive_internals 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bd381f6d01a6616cdba8530492d453b7761b456ba974e98768a18cad2cd76f58"
"checksum serde_json 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ee28c1d94a7745259b767ca9e5b95d55bafbd3205ca3acb978cad84a6ed6bc62"
"checksum sha1 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cc30b1e1e8c40c121ca33b86c23308a090d19974ef001b4bf6e61fd1a0fb095c"


+ 1
- 0
components/rendering/src/markdown.rs View File

@@ -104,6 +104,7 @@ pub fn markdown_to_html(content: &str, context: &Context) -> Result<(String, Vec
// Shortcode without body
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;
match render_simple_shortcode(context.tera, &name, &args) {
Ok(s) => return Event::Html(Owned(format!("</p>{}", s))),


+ 14
- 6
components/rendering/src/short_code.rs View File

@@ -6,7 +6,7 @@ use tera::{Tera, Context};
use errors::{Result, ResultExt};

lazy_static!{
pub static ref SHORTCODE_RE: Regex = Regex::new(r#"\{(?:%|\{)\s+([[:alnum:]]+?)\(([[:alnum:]]+?="?.+?"?)\)\s+(?:%|\})\}"#).unwrap();
pub static ref SHORTCODE_RE: Regex = Regex::new(r#"\{(?:%|\{)\s+([[:alnum:]]+?)\(([[:alnum:]]+?="?.+?"?)?\)\s+(?:%|\})\}"#).unwrap();
}

/// A shortcode that has a body
@@ -50,10 +50,12 @@ pub fn parse_shortcode(input: &str) -> (String, HashMap<String, String>) {
let caps = SHORTCODE_RE.captures(input).unwrap();
// caps[0] is the full match
let name = &caps[1];
let arg_list = &caps[2];
for arg in arg_list.split(',') {
let bits = arg.split('=').collect::<Vec<_>>();
args.insert(bits[0].trim().to_string(), bits[1].replace("\"", ""));

if let Some(arg_list) = caps.get(2) {
for arg in arg_list.as_str().split(',') {
let bits = arg.split('=').collect::<Vec<_>>();
args.insert(bits[0].trim().to_string(), bits[1].replace("\"", ""));
}
}

(name.to_string(), args)
@@ -75,6 +77,13 @@ pub fn render_simple_shortcode(tera: &Tera, name: &str, args: &HashMap<String, S
mod tests {
use super::parse_shortcode;

#[test]
fn can_parse_simple_shortcode_no_arg() {
let (name, args) = parse_shortcode(r#"{{ basic() }}"#);
assert_eq!(name, "basic");
assert!(args.is_empty());
}

#[test]
fn can_parse_simple_shortcode_one_arg() {
let (name, args) = parse_shortcode(r#"{{ youtube(id="w7Ft2ymGmfc") }}"#);
@@ -97,5 +106,4 @@ mod tests {
assert_eq!(args["id"], "w7Ft2ymGmfc");
assert_eq!(args["autoplay"], "true");
}

}

+ 4
- 0
components/site/test_site/content/posts/python.md View File

@@ -5,3 +5,7 @@ date = "2017-03-01"
+++

Same filename but different path

{{ basic() }}



+ 1
- 0
components/site/test_site/templates/shortcodes/basic.html View File

@@ -0,0 +1 @@
<h4>Basic shortcode</h4>

+ 2
- 0
components/site/tests/site.rs View File

@@ -107,6 +107,8 @@ fn can_build_site_without_live_reload() {
assert!(file_exists!(public, "a-fixed-url/index.html"));

assert!(file_exists!(public, "posts/python/index.html"));
// Shortcodes work
assert!(file_contains!(public, "posts/python/index.html", "Basic shortcode"));
assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
assert!(file_exists!(public, "posts/with-assets/index.html"));
assert!(file_exists!(public, "posts/no-section/simple/index.html"));


Loading…
Cancel
Save