165 lines
5.8 KiB
Rust
165 lines
5.8 KiB
Rust
use crate::parser::Body;
|
|
use crate::parser::DustTag;
|
|
use crate::parser::Template;
|
|
use crate::parser::TemplateElement;
|
|
use std::collections::HashMap;
|
|
|
|
pub struct InlinePartialTreeElement<'a> {
|
|
parent: Option<&'a InlinePartialTreeElement<'a>>,
|
|
blocks: HashMap<&'a str, &'a Option<Body<'a>>>,
|
|
}
|
|
|
|
impl<'a> InlinePartialTreeElement<'a> {
|
|
pub fn new(
|
|
parent: Option<&'a InlinePartialTreeElement<'a>>,
|
|
blocks: HashMap<&'a str, &'a Option<Body<'a>>>,
|
|
) -> InlinePartialTreeElement<'a> {
|
|
InlinePartialTreeElement {
|
|
parent: parent,
|
|
blocks: blocks,
|
|
}
|
|
}
|
|
|
|
pub fn get_block(&self, name: &str) -> Option<&'a Option<Body<'a>>> {
|
|
match self.blocks.get(name) {
|
|
None => match self.parent {
|
|
None => None,
|
|
Some(parent_tree_element) => parent_tree_element.get_block(name),
|
|
},
|
|
Some(interior) => Some(interior),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn extract_inline_partials<'a>(
|
|
template: &'a Template<'a>,
|
|
) -> HashMap<&'a str, &'a Option<Body<'a>>> {
|
|
let mut blocks: HashMap<&'a str, &'a Option<Body<'a>>> = HashMap::new();
|
|
|
|
extract_inline_partials_from_body(&mut blocks, &template.contents);
|
|
|
|
blocks
|
|
}
|
|
|
|
fn extract_inline_partials_from_body<'a, 'b>(
|
|
blocks: &'b mut HashMap<&'a str, &'a Option<Body<'a>>>,
|
|
body: &'a Body<'a>,
|
|
) {
|
|
for elem in &body.elements {
|
|
match elem {
|
|
TemplateElement::TEIgnoredWhitespace(_) => (),
|
|
TemplateElement::TESpan(_span) => (),
|
|
TemplateElement::TETag(dt) => {
|
|
extract_inline_partials_from_tag(blocks, dt);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn extract_inline_partials_from_tag<'a, 'b>(
|
|
blocks: &'b mut HashMap<&'a str, &'a Option<Body<'a>>>,
|
|
tag: &'a DustTag,
|
|
) {
|
|
match tag {
|
|
DustTag::DTComment(..) => (),
|
|
DustTag::DTSpecial(..) => (),
|
|
DustTag::DTLiteralStringBlock(..) => (),
|
|
DustTag::DTReference(..) => (),
|
|
DustTag::DTSection(container) => {
|
|
match &container.contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
match &container.else_contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
}
|
|
DustTag::DTExists(container) => {
|
|
match &container.contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
match &container.else_contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
}
|
|
DustTag::DTNotExists(container) => {
|
|
match &container.contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
match &container.else_contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
}
|
|
DustTag::DTPartial(..) => (),
|
|
DustTag::DTInlinePartial(named_block) => {
|
|
blocks.insert(&named_block.path.keys[0], &named_block.contents);
|
|
}
|
|
DustTag::DTBlock(..) => (),
|
|
DustTag::DTHelperEquals(parameterized_block) => {
|
|
match ¶meterized_block.contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
match ¶meterized_block.else_contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
}
|
|
DustTag::DTHelperNotEquals(parameterized_block) => {
|
|
match ¶meterized_block.contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
match ¶meterized_block.else_contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
}
|
|
DustTag::DTHelperGreaterThan(parameterized_block) => {
|
|
match ¶meterized_block.contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
match ¶meterized_block.else_contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
}
|
|
DustTag::DTHelperLessThan(parameterized_block) => {
|
|
match ¶meterized_block.contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
match ¶meterized_block.else_contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
}
|
|
DustTag::DTHelperGreaterThanOrEquals(parameterized_block) => {
|
|
match ¶meterized_block.contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
match ¶meterized_block.else_contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
}
|
|
DustTag::DTHelperLessThanOrEquals(parameterized_block) => {
|
|
match ¶meterized_block.contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
match ¶meterized_block.else_contents {
|
|
None => (),
|
|
Some(body) => extract_inline_partials_from_body(blocks, &body),
|
|
};
|
|
}
|
|
}
|
|
}
|