Skip to content

Commit

Permalink
Improve comments & reorganize code in input.rs
Browse files Browse the repository at this point in the history
Signed-off-by: max <gmx.sht@gmail.com>
  • Loading branch information
PizzasBear committed Dec 8, 2023
1 parent 3614d5c commit 9cd8891
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
8 changes: 6 additions & 2 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,9 @@ impl<'a> Generator<'a> {
)?;
}

// Since nodes must not outlive the Generator, we instantiate a nested `Generator` here to
// handle the include's nodes. Unfortunately we can't easily share the `includes` cache.
// We clone the context of the child in order to preserve their macros and imports.
// But also add all the imports and macros from this template that don't override the
// child's ones to preserve this template's context.

let child_ctx = &mut self.contexts[path.as_path()].clone();
for (name, mac) in &ctx.macros {
Expand All @@ -856,6 +857,9 @@ impl<'a> Generator<'a> {
.entry(name)
.or_insert_with(|| import.clone());
}

// Create a new generator for the child, and call it like in `impl_template` as if it were
// a full template, while preserving the context.
let heritage = if !child_ctx.blocks.is_empty() || child_ctx.extends.is_some() {
Some(Heritage::new(child_ctx, self.contexts))
} else {
Expand Down
55 changes: 31 additions & 24 deletions askama_derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,17 @@ impl TemplateInput<'_> {
let mut nested = vec![parsed.nodes()];
while let Some(nodes) = nested.pop() {
for n in nodes {
let mut add_to_check = |path: PathBuf| -> Result<(), CompileError> {
if !map.contains_key(&path) {
map.insert(path.clone(), Parsed::default());
let source = get_template_source(&path)?;
check.push((path, source));
}
Ok(())
};
use Node::*;
match n {
Node::Extends(extends) if top => {
Extends(extends) if top => {
let extends = self.config.find_template(extends.path, Some(&path))?;
let dependency_path = (path.clone(), extends.clone());
if dependency_graph.contains(&dependency_path) {
Expand All @@ -133,49 +142,47 @@ impl TemplateInput<'_> {
.into());
}
dependency_graph.push(dependency_path);
if !map.contains_key(&extends) {
map.insert(extends.clone(), Parsed::default());
let source = get_template_source(&extends)?;
check.push((extends, source));
}
add_to_check(extends)?;
}
Node::Macro(m) if top => {
Macro(m) if top => {
nested.push(&m.nodes);
}
Node::Import(import) if top => {
Import(import) if top => {
let import = self.config.find_template(import.path, Some(&path))?;
if !map.contains_key(&import) {
map.insert(import.clone(), Parsed::default());
let source = get_template_source(&import)?;
check.push((import, source));
}
add_to_check(import)?;
}
Node::Include(include) => {
Include(include) => {
let include = self.config.find_template(include.path, Some(&path))?;
if !map.contains_key(&include) {
map.insert(include.clone(), Parsed::default());
let source = get_template_source(&include)?;
check.push((include, source));
}
add_to_check(include)?;
}
Node::BlockDef(b) => {
BlockDef(b) => {
nested.push(&b.nodes);
}
Node::If(i) => {
If(i) => {
for cond in &i.branches {
nested.push(&cond.nodes);
}
}
Node::Loop(l) => {
Loop(l) => {
nested.push(&l.body);
nested.push(&l.else_nodes);
}
Node::Match(m) => {
Match(m) => {
for arm in &m.arms {
nested.push(&arm.nodes);
}
}
_ => {}
Lit(_)
| Comment(_)
| Expr(_, _)
| Call(_)
| Extends(_)
| Let(_)
| Import(_)
| Macro(_)
| Raw(_)
| Continue(_)
| Break(_) => {}
}
}
top = false;
Expand Down

0 comments on commit 9cd8891

Please sign in to comment.