diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-11 22:38:07 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-11 22:38:07 +0100 |
commit | f9f4f57221e4488f3ecf26752e6c53d60167a5af (patch) | |
tree | e76898c9a4f86b5dceaa4707620f2245f448b6d2 /askama_derive | |
parent | c8d9a6326d69471145ce6ec7742389a3f9186f1a (diff) | |
download | askama-f9f4f57221e4488f3ecf26752e6c53d60167a5af.tar.gz askama-f9f4f57221e4488f3ecf26752e6c53d60167a5af.tar.bz2 askama-f9f4f57221e4488f3ecf26752e6c53d60167a5af.zip |
Move get_template_source() into askama_derive
Diffstat (limited to '')
-rw-r--r-- | askama_derive/src/generator.rs | 6 | ||||
-rw-r--r-- | askama_derive/src/lib.rs | 36 | ||||
-rw-r--r-- | askama_derive/templates/b.html | 1 |
3 files changed, 36 insertions, 7 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 7bc7b41..15a2e61 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -1,7 +1,7 @@ -use super::Context; +use super::{get_template_source, Context}; use input::TemplateInput; use parser::{self, Cond, Expr, MatchParameter, MatchVariant, Node, Target, When, WS}; -use shared::{filters, path}; +use shared::filters; use proc_macro2::Span; @@ -494,7 +494,7 @@ impl<'a> Generator<'a> { let path = self.input .config .find_template(path, Some(&self.input.path)); - let src = path::get_template_source(&path); + let src = get_template_source(&path); let nodes = parser::parse(&src); let nested = { let mut gen = self.child(); diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index c36da67..1fd7570 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -14,9 +14,10 @@ mod parser; use input::{Print, Source, TemplateInput}; use parser::{Expr, Macro, Node}; use proc_macro::TokenStream; -use shared::{path, Config}; +use shared::Config; use std::collections::HashMap; +use std::fs; use std::path::{Path, PathBuf}; #[proc_macro_derive(Template, attributes(template))] @@ -36,7 +37,7 @@ fn build_template(ast: &syn::DeriveInput) -> String { let input = TemplateInput::new(ast); let source: String = match input.source { Source::Source(ref s) => s.clone(), - Source::Path(_) => path::get_template_source(&input.path), + Source::Path(_) => get_template_source(&input.path), }; let mut sources = HashMap::new(); @@ -70,12 +71,12 @@ fn find_used_templates(input: &TemplateInput, map: &mut HashMap<PathBuf, String> match n { Node::Extends(Expr::StrLit(extends)) => { let extends = input.config.find_template(extends, Some(&path)); - let source = path::get_template_source(&extends); + let source = get_template_source(&extends); check.push((extends, source)); } Node::Import(_, import, _) => { let import = input.config.find_template(import, Some(&path)); - let source = path::get_template_source(&import); + let source = get_template_source(&import); check.push((import, source)); } _ => {} @@ -158,3 +159,30 @@ impl<'a> Context<'a> { } } } + +fn get_template_source(tpl_path: &Path) -> String { + match fs::read_to_string(tpl_path) { + Err(_) => panic!( + "unable to open template file '{}'", + tpl_path.to_str().unwrap() + ), + Ok(mut source) => { + if source.ends_with('\n') { + let _ = source.pop(); + } + source + } + } +} + +#[cfg(test)] +mod tests { + use super::get_template_source; + use Config; + + #[test] + fn get_source() { + let path = Config::new().find_template("b.html", None); + assert_eq!(get_template_source(&path), "bar"); + } +} diff --git a/askama_derive/templates/b.html b/askama_derive/templates/b.html new file mode 100644 index 0000000..5716ca5 --- /dev/null +++ b/askama_derive/templates/b.html @@ -0,0 +1 @@ +bar |