diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-11 22:30:10 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-11 22:30:10 +0100 |
commit | c8d9a6326d69471145ce6ec7742389a3f9186f1a (patch) | |
tree | c51c0f0c6034525a00fc809d62c9a112006febdd /askama_derive/src | |
parent | 987354946cde37ddd05d5316f7b581c9dd6f0131 (diff) | |
download | askama-c8d9a6326d69471145ce6ec7742389a3f9186f1a.tar.gz askama-c8d9a6326d69471145ce6ec7742389a3f9186f1a.tar.bz2 askama-c8d9a6326d69471145ce6ec7742389a3f9186f1a.zip |
Move find_template_from_path() into Config
Diffstat (limited to 'askama_derive/src')
-rw-r--r-- | askama_derive/src/generator.rs | 4 | ||||
-rw-r--r-- | askama_derive/src/input.rs | 7 | ||||
-rw-r--r-- | askama_derive/src/lib.rs | 22 |
3 files changed, 19 insertions, 14 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 3fce6c9..7bc7b41 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -491,7 +491,9 @@ impl<'a> Generator<'a> { fn handle_include(&mut self, ctx: &'a Context, ws: WS, path: &str) { self.flush_ws(ws); - let path = path::find_template_from_path(path, Some(&self.input.path)); + let path = self.input + .config + .find_template(path, Some(&self.input.path)); let src = path::get_template_source(&path); let nodes = parser::parse(&src); let nested = { diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs index 6e349da..14531cf 100644 --- a/askama_derive/src/input.rs +++ b/askama_derive/src/input.rs @@ -2,7 +2,7 @@ use proc_macro2::TokenStream; use quote::ToTokens; -use shared::path; +use shared::Config; use std::path::PathBuf; @@ -10,6 +10,7 @@ use syn; pub struct TemplateInput<'a> { pub ast: &'a syn::DeriveInput, + pub config: Config, pub source: Source, pub print: Print, pub escaping: EscapeMode, @@ -96,9 +97,10 @@ impl<'a> TemplateInput<'a> { // Validate the `source` and `ext` value together, since they are // related. In case `source` was used instead of `path`, the value // of `ext` is merged into a synthetic `path` value here. + let config = Config::new(); let source = source.expect("template path or source not found in attributes"); let path = match (&source, &ext) { - (&Source::Path(ref path), None) => path::find_template_from_path(path, None), + (&Source::Path(ref path), None) => config.find_template(path, None), (&Source::Source(_), Some(ext)) => PathBuf::from(format!("{}.{}", ast.ident, ext)), (&Source::Path(_), Some(_)) => { panic!("'ext' attribute cannot be used with 'path' attribute") @@ -124,6 +126,7 @@ impl<'a> TemplateInput<'a> { TemplateInput { ast, + config, source, print, escaping: escaping.unwrap_or_else(|| EscapeMode::from_path(&path)), diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 4bdcb0a..c36da67 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -14,10 +14,10 @@ mod parser; use input::{Print, Source, TemplateInput}; use parser::{Expr, Macro, Node}; use proc_macro::TokenStream; -use shared::path; +use shared::{path, Config}; use std::collections::HashMap; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; #[proc_macro_derive(Template, attributes(template))] pub fn derive_template(input: TokenStream) -> TokenStream { @@ -40,7 +40,7 @@ fn build_template(ast: &syn::DeriveInput) -> String { }; let mut sources = HashMap::new(); - find_used_templates(&mut sources, input.path.clone(), source); + find_used_templates(&input, &mut sources, source); let mut parsed = HashMap::new(); for (path, src) in &sources { @@ -49,7 +49,7 @@ fn build_template(ast: &syn::DeriveInput) -> String { let mut contexts = HashMap::new(); for (path, nodes) in &parsed { - contexts.insert(*path, Context::new(path, nodes)); + contexts.insert(*path, Context::new(&input.config, path, nodes)); } if input.print == Print::Ast || input.print == Print::All { @@ -63,18 +63,18 @@ fn build_template(ast: &syn::DeriveInput) -> String { code } -fn find_used_templates(map: &mut HashMap<PathBuf, String>, path: PathBuf, source: String) { - let mut check = vec![(path, source)]; +fn find_used_templates(input: &TemplateInput, map: &mut HashMap<PathBuf, String>, source: String) { + let mut check = vec![(input.path.clone(), source)]; while let Some((path, source)) = check.pop() { for n in parser::parse(&source) { match n { Node::Extends(Expr::StrLit(extends)) => { - let extends = path::find_template_from_path(extends, Some(&path)); + let extends = input.config.find_template(extends, Some(&path)); let source = path::get_template_source(&extends); check.push((extends, source)); } Node::Import(_, import, _) => { - let import = path::find_template_from_path(import, Some(&path)); + let import = input.config.find_template(import, Some(&path)); let source = path::get_template_source(&import); check.push((import, source)); } @@ -94,7 +94,7 @@ pub(crate) struct Context<'a> { } impl<'a> Context<'a> { - fn new<'n>(path: &PathBuf, nodes: &'n [Node<'n>]) -> Context<'n> { + fn new<'n>(config: &Config, path: &Path, nodes: &'n [Node<'n>]) -> Context<'n> { let mut extends = None; let mut blocks = Vec::new(); let mut macros = HashMap::new(); @@ -105,7 +105,7 @@ impl<'a> Context<'a> { Node::Extends(Expr::StrLit(extends_path)) => match extends { Some(_) => panic!("multiple extend blocks found"), None => { - extends = Some(path::find_template_from_path(extends_path, Some(path))); + extends = Some(config.find_template(extends_path, Some(path))); } }, def @ Node::BlockDef(_, _, _, _) => { @@ -115,7 +115,7 @@ impl<'a> Context<'a> { macros.insert(*name, m); } Node::Import(_, import_path, scope) => { - let path = path::find_template_from_path(import_path, Some(path)); + let path = config.find_template(import_path, Some(path)); imports.insert(*scope, path); } _ => {} |