diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2016-12-24 08:28:56 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2016-12-24 08:28:56 +0100 |
commit | b83ad3582fef454983fd491e02e3d243a1d0a52d (patch) | |
tree | de6fe3eb3d89a7cb72f6800ad4e5aee29b337a42 | |
parent | df8b05d0a4243b0d0021871e5cab4269d89b1e0c (diff) | |
download | askama-b83ad3582fef454983fd491e02e3d243a1d0a52d.tar.gz askama-b83ad3582fef454983fd491e02e3d243a1d0a52d.tar.bz2 askama-b83ad3582fef454983fd491e02e3d243a1d0a52d.zip |
Implement very basic template parser
Diffstat (limited to '')
-rw-r--r-- | askama_codegen/src/lib.rs | 54 | ||||
-rw-r--r-- | askama_test/tests/simple.rs | 2 |
2 files changed, 47 insertions, 9 deletions
diff --git a/askama_codegen/src/lib.rs b/askama_codegen/src/lib.rs index b325bae..1e7fe71 100644 --- a/askama_codegen/src/lib.rs +++ b/askama_codegen/src/lib.rs @@ -7,10 +7,12 @@ extern crate proc_macro; extern crate quote; extern crate syn; +use nom::IResult; use proc_macro::TokenStream; use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; +use std::str; fn get_path_from_attrs(attrs: &Vec<syn::Attribute>) -> String { for attr in attrs { @@ -51,6 +53,28 @@ fn get_template_source(tpl_file: &str) -> String { s } +fn take_content(i: &[u8]) -> IResult<&[u8], &[u8]> { + 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], &i[..]); + } else if i[j + 1] == '{' as u8 { + return IResult::Done(&i[j..], &i[..j]); + } else if i[j + 1] == '%' as u8 { + return IResult::Done(&i[j..], &i[..j]); + } + } + } + IResult::Done(&i[..0], &i[..]) +} + +named!(var_expr, delimited!(tag!("{{"), take_until!("}}"), tag!("}}"))); + +named!(parse_template< Vec<&[u8]> >, many1!(alt!(take_content | var_expr))); + #[proc_macro_derive(Template, attributes(template))] pub fn derive_template(input: TokenStream) -> TokenStream { let source = input.to_string(); @@ -65,14 +89,28 @@ pub fn derive_template(input: TokenStream) -> TokenStream { let path = get_path_from_attrs(&ast.attrs); let src = get_template_source(&path); - let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); - let expanded = quote! { - impl #impl_generics askama::Template for #name #ty_generics #where_clause { - fn render(&self) -> String { - #src.into() - } - } + let root = match parse_template(src.as_bytes()) { + IResult::Done(_, res) => res, + _ => panic!("problems parsing template source"), }; - expanded.parse().unwrap() + let mut code = String::new(); + code.push_str("impl askama::Template for "); + code.push_str(name.as_ref()); + code.push_str(" {\n"); + code.push_str(" fn render(&self) -> String {\n"); + code.push_str(" let mut buf = String::new();\n"); + code.push_str(" buf.push_str(\""); + code.push_str(str::from_utf8(root[0]).unwrap()); + code.push_str("\");\n"); + code.push_str(" buf.push_str(&self."); + code.push_str(str::from_utf8(root[1]).unwrap()); + code.push_str(");\n"); + code.push_str(" buf.push_str(\""); + code.push_str(str::from_utf8(root[2]).unwrap()); + code.push_str("\");\n"); + code.push_str(" buf"); + code.push_str(" }\n"); + code.push_str("}\n\n"); + code.parse().unwrap() } diff --git a/askama_test/tests/simple.rs b/askama_test/tests/simple.rs index 67bb126..9dbfc9c 100644 --- a/askama_test/tests/simple.rs +++ b/askama_test/tests/simple.rs @@ -15,5 +15,5 @@ struct TestTemplate { #[test] fn it_works() { let s = TestTemplate { var: "foo".to_string() }.render(); - assert_eq!(s, "hello world, foo"); + assert_eq!(s, "hello world, foo\n"); } |