diff options
-rw-r--r-- | Cargo.lock | 32 | ||||
-rw-r--r-- | askama/Cargo.toml | 4 | ||||
-rw-r--r-- | askama_derive/Cargo.toml | 2 | ||||
-rw-r--r-- | askama_derive/src/parser.rs | 37 | ||||
-rw-r--r-- | testing/templates/json.html | 1 | ||||
-rw-r--r-- | testing/tests/simple.rs | 14 |
6 files changed, 69 insertions, 21 deletions
@@ -2,29 +2,45 @@ name = "askama_testing" version = "0.1.0" dependencies = [ - "askama 0.3.2", + "askama 0.3.3", ] [[package]] name = "askama" -version = "0.3.2" +version = "0.3.3" dependencies = [ - "askama_derive 0.3.2", + "askama_derive 0.3.3", ] [[package]] name = "askama_derive" -version = "0.3.2" +version = "0.3.3" dependencies = [ - "nom 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "nom 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] +name = "libc" +version = "0.2.29" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "memchr" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "nom" -version = "3.0.0" +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)", +] [[package]] name = "quote" @@ -55,7 +71,9 @@ version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] -"checksum nom 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5388009f277d5d889ee01f0ba92e3620c759402cdf52a8d40e9ac06969ee3c88" +"checksum libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "8a014d9226c2cc402676fbe9ea2e15dd5222cd1dd57f576b5b283178c944a264" +"checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4" +"checksum nom 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "06989cbd367e06f787a451f3bc67d8c3e0eaa10b461cc01152ffab24261a31b1" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" diff --git a/askama/Cargo.toml b/askama/Cargo.toml index fe29473..8cdef77 100644 --- a/askama/Cargo.toml +++ b/askama/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "askama" -version = "0.3.2" +version = "0.3.3" authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"] description = "Type-safe, compiled Jinja-like templates for Rust" documentation = "https://docs.rs/askama" @@ -16,4 +16,4 @@ readme = "../README.md" travis-ci = { repository = "djc/askama" } [dependencies] -askama_derive = { path = "../askama_derive", version = "0.3.2" } +askama_derive = { path = "../askama_derive", version = "0.3.3" } diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index f916d85..4aad3fb 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "askama_derive" -version = "0.3.2" +version = "0.3.3" authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"] description = "Procedural macro package for Askama" homepage = "https://github.com/djc/askama" diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs index 9a9c89e..61ccb09 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -61,20 +61,35 @@ fn split_ws_parts(s: &[u8]) -> Node { str::from_utf8(res.2).unwrap()) } +enum ContentState { + Any, + Brace(usize), + End(usize), +} + fn take_content(i: &[u8]) -> IResult<&[u8], Node> { - if i.len() < 1 || i[0] == b'{' { - return IResult::Error(error_position!(nom::ErrorKind::TakeUntil, i)); - } - for (j, c) in i.iter().enumerate() { - if *c == b'{' { - if i.len() < j + 2 { - return IResult::Done(&i[..0], split_ws_parts(&i[..])); - } else if i[j + 1] == b'{' || i[j + 1] == b'%' || i[j + 1] == b'#' { - return IResult::Done(&i[j..], split_ws_parts(&i[..j])); - } + use parser::ContentState::*; + let mut state = Any; + for (idx, c) in i.iter().enumerate() { + state = match (state, *c) { + (Any, b'{') => Brace(idx), + (Any, _) => Any, + (Brace(start), b'{') | + (Brace(start), b'%') | + (Brace(start), b'#') => End(start), + (Brace(_), _) => Any, + (End(_), _) => panic!("cannot happen"), + }; + if let End(_) = state { + break; } } - IResult::Done(&i[..0], split_ws_parts(&i[..])) + match state { + Any | + Brace(_) => IResult::Done(&i[..0], split_ws_parts(i)), + End(0) => IResult::Error(nom::ErrorKind::Custom(0)), + End(start) => IResult::Done(&i[start..], split_ws_parts(&i[..start])), + } } fn identifier(input: &[u8]) -> IResult<&[u8], &str> { diff --git a/testing/templates/json.html b/testing/templates/json.html new file mode 100644 index 0000000..e711c10 --- /dev/null +++ b/testing/templates/json.html @@ -0,0 +1 @@ +{"foo": "{{ foo }}", "bar": "{{ bar }}"} diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs index 980321f..c924e85 100644 --- a/testing/tests/simple.rs +++ b/testing/tests/simple.rs @@ -140,3 +140,17 @@ fn test_generics() { let t = GenericsTemplate { t: "a", u: 42 }; assert_eq!(t.render(), "a42"); } + + +#[derive(Template)] +#[template(path = "json.html")] +struct JsonTemplate<'a> { + foo: &'a str, + bar: &'a str, +} + +#[test] +fn test_json() { + let t = JsonTemplate { foo: "a", bar: "b" }; + assert_eq!(t.render(), "{\"foo\": \"a\", \"bar\": \"b\"}"); +} |