2020-04-10 20:27:27 -04:00
|
|
|
use crate::parser::template;
|
2020-05-31 21:18:54 -04:00
|
|
|
use crate::parser::Body;
|
|
|
|
use crate::parser::DustTag;
|
2020-05-31 21:26:31 -04:00
|
|
|
use crate::parser::Filter;
|
2020-05-31 21:18:54 -04:00
|
|
|
use crate::parser::PartialNameElement;
|
2020-05-25 15:40:42 -04:00
|
|
|
use crate::parser::Path;
|
2020-05-31 21:26:31 -04:00
|
|
|
use crate::parser::Special;
|
2020-04-10 20:27:27 -04:00
|
|
|
use crate::parser::Template;
|
2020-05-31 21:18:54 -04:00
|
|
|
use crate::parser::TemplateElement;
|
2020-05-31 18:18:21 -04:00
|
|
|
use crate::renderer::breadcrumb_tree::BreadcrumbTreeElement;
|
2020-04-28 19:09:02 -04:00
|
|
|
use crate::renderer::context_element::ContextElement;
|
2020-05-30 16:34:32 -04:00
|
|
|
use crate::renderer::context_element::IntoContextElement;
|
2020-04-11 18:25:48 -04:00
|
|
|
use crate::renderer::errors::CompileError;
|
2020-05-31 21:18:54 -04:00
|
|
|
use crate::renderer::errors::RenderError;
|
2020-05-31 21:26:31 -04:00
|
|
|
use crate::renderer::errors::WalkError;
|
2020-05-31 21:18:54 -04:00
|
|
|
use crate::renderer::inline_partial_tree::extract_inline_partials;
|
|
|
|
use crate::renderer::inline_partial_tree::InlinePartialTreeElement;
|
2020-06-06 23:08:21 -04:00
|
|
|
use crate::renderer::iteration_context::IterationContext;
|
2020-06-06 20:48:29 -04:00
|
|
|
use crate::renderer::parameters_context::ParametersContext;
|
2020-05-31 18:18:21 -04:00
|
|
|
use crate::renderer::tree_walking::walk_path;
|
2020-05-31 21:18:54 -04:00
|
|
|
use std::borrow::Borrow;
|
2020-05-23 17:57:19 -04:00
|
|
|
use std::collections::HashMap;
|
2020-05-31 23:47:20 -04:00
|
|
|
use std::rc::Rc;
|
2020-04-10 20:27:27 -04:00
|
|
|
|
2020-04-10 20:55:44 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2020-04-10 20:58:55 -04:00
|
|
|
pub struct DustRenderer<'a> {
|
2020-04-11 18:25:48 -04:00
|
|
|
templates: HashMap<String, &'a Template<'a>>,
|
2020-04-10 20:55:44 -04:00
|
|
|
}
|
|
|
|
|
2020-05-31 19:01:51 -04:00
|
|
|
pub fn compile_template<'a>(source: &'a str) -> Result<Template<'a>, CompileError> {
|
|
|
|
let (_remaining, parsed_template) = template(source).map_err(|err| CompileError {
|
|
|
|
message: "Failed to compile template".to_owned(),
|
|
|
|
})?;
|
|
|
|
Ok(parsed_template)
|
2020-04-10 20:27:27 -04:00
|
|
|
}
|
2020-04-10 20:55:44 -04:00
|
|
|
|
2020-04-10 20:58:55 -04:00
|
|
|
impl<'a> DustRenderer<'a> {
|
|
|
|
pub fn new() -> DustRenderer<'a> {
|
|
|
|
DustRenderer {
|
2020-04-11 18:25:48 -04:00
|
|
|
templates: HashMap::new(),
|
2020-04-10 20:55:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 19:01:51 -04:00
|
|
|
pub fn load_source(&mut self, template: &'a Template, name: String) {
|
|
|
|
self.templates.insert(name, template);
|
2020-04-10 20:55:44 -04:00
|
|
|
}
|
2020-04-10 20:58:55 -04:00
|
|
|
|
2020-05-31 21:18:54 -04:00
|
|
|
pub fn render<C>(&'a self, name: &str, context: Option<&C>) -> Result<String, RenderError>
|
|
|
|
where
|
|
|
|
C: IntoContextElement,
|
|
|
|
{
|
2020-06-06 22:24:27 -04:00
|
|
|
let breadcrumbs = context
|
|
|
|
.map(|ctx| vec![BreadcrumbTreeElement::from_borrowed(ctx)])
|
|
|
|
.unwrap_or(Vec::new());
|
2020-05-31 21:18:54 -04:00
|
|
|
self.render_template(name, breadcrumbs.as_ref(), None)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn render_template(
|
|
|
|
&'a self,
|
|
|
|
name: &str,
|
2020-06-06 22:24:27 -04:00
|
|
|
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
|
2020-05-31 21:18:54 -04:00
|
|
|
blocks: Option<&'a InlinePartialTreeElement<'a>>,
|
|
|
|
) -> Result<String, RenderError> {
|
|
|
|
let main_template = match self.templates.get(name) {
|
|
|
|
Some(tmpl) => tmpl,
|
|
|
|
None => {
|
|
|
|
return Err(RenderError::TemplateNotFound(name.to_owned()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let extracted_inline_partials = extract_inline_partials(main_template);
|
|
|
|
let new_blocks = InlinePartialTreeElement::new(blocks, extracted_inline_partials);
|
|
|
|
let new_block_context = BlockContext {
|
|
|
|
breadcrumbs: breadcrumbs,
|
|
|
|
blocks: &new_blocks,
|
|
|
|
};
|
|
|
|
self.render_body(&main_template.contents, breadcrumbs, &new_block_context)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_maybe_body(
|
|
|
|
&'a self,
|
|
|
|
body: &'a Option<Body>,
|
2020-06-06 22:24:27 -04:00
|
|
|
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
|
2020-05-31 21:18:54 -04:00
|
|
|
blocks: &'a BlockContext<'a>,
|
|
|
|
) -> Result<String, RenderError> {
|
|
|
|
match body {
|
|
|
|
None => Ok("".to_owned()),
|
|
|
|
Some(body) => Ok(self.render_body(body, breadcrumbs, blocks)?),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_body(
|
|
|
|
&'a self,
|
|
|
|
body: &'a Body,
|
2020-06-06 22:24:27 -04:00
|
|
|
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
|
2020-05-31 21:18:54 -04:00
|
|
|
blocks: &'a BlockContext<'a>,
|
|
|
|
) -> Result<String, RenderError> {
|
|
|
|
let mut output = String::new();
|
|
|
|
for elem in &body.elements {
|
|
|
|
match elem {
|
|
|
|
TemplateElement::TEIgnoredWhitespace(_) => {}
|
|
|
|
TemplateElement::TESpan(span) => output.push_str(span.contents),
|
|
|
|
TemplateElement::TETag(dt) => {
|
|
|
|
output.push_str(&self.render_tag(dt, breadcrumbs, blocks)?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For rendering a dynamic partial's name or an rvalue template
|
|
|
|
pub fn render_partial_name(
|
|
|
|
&'a self,
|
|
|
|
body: &'a Vec<PartialNameElement>,
|
2020-06-06 22:24:27 -04:00
|
|
|
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
|
2020-05-31 21:18:54 -04:00
|
|
|
) -> Result<String, RenderError> {
|
|
|
|
let converted_to_template_elements: Vec<TemplateElement<'a>> =
|
|
|
|
body.into_iter().map(|e| e.into()).collect();
|
|
|
|
// Simple templates like partial names and reference rvalues
|
|
|
|
// cannot contain blocks or inline partials, so we use a blank
|
|
|
|
// BlockContext.
|
|
|
|
let empty_block_context = BlockContext {
|
2020-06-06 22:24:27 -04:00
|
|
|
breadcrumbs: &Vec::new(),
|
2020-05-31 21:18:54 -04:00
|
|
|
blocks: &InlinePartialTreeElement::new(None, HashMap::new()),
|
|
|
|
};
|
|
|
|
self.render_body(
|
|
|
|
&Body {
|
|
|
|
elements: converted_to_template_elements,
|
|
|
|
},
|
|
|
|
breadcrumbs,
|
|
|
|
&empty_block_context,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_tag(
|
|
|
|
&'a self,
|
|
|
|
tag: &'a DustTag,
|
2020-06-06 22:24:27 -04:00
|
|
|
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
|
2020-05-31 21:18:54 -04:00
|
|
|
blocks: &'a BlockContext<'a>,
|
|
|
|
) -> Result<String, RenderError> {
|
|
|
|
match tag {
|
2020-05-31 21:26:31 -04:00
|
|
|
DustTag::DTComment(_comment) => (),
|
|
|
|
DustTag::DTSpecial(special) => {
|
|
|
|
return Ok(match special {
|
|
|
|
Special::Space => " ",
|
|
|
|
Special::NewLine => "\n",
|
|
|
|
Special::CarriageReturn => "\r",
|
|
|
|
Special::LeftCurlyBrace => "{",
|
|
|
|
Special::RightCurlyBrace => "}",
|
|
|
|
}
|
|
|
|
.to_owned())
|
|
|
|
}
|
|
|
|
DustTag::DTLiteralStringBlock(literal) => return Ok((*literal).to_owned()),
|
|
|
|
DustTag::DTReference(reference) => {
|
|
|
|
let val = walk_path(breadcrumbs, &reference.path.keys)
|
|
|
|
.map(|ice| ice.into_context_element(self, breadcrumbs));
|
|
|
|
match val {
|
2020-05-31 22:17:58 -04:00
|
|
|
Err(WalkError::CantWalk) | Ok(None) => return Ok("".to_owned()),
|
|
|
|
Ok(Some(final_val)) => {
|
2020-05-31 23:47:20 -04:00
|
|
|
return if final_val.get_context_element_reference().is_truthy() {
|
|
|
|
final_val
|
|
|
|
.get_context_element_reference()
|
|
|
|
.render(&Self::preprocess_filters(&reference.filters))
|
2020-05-31 21:26:31 -04:00
|
|
|
} else {
|
|
|
|
Ok("".to_owned())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 21:30:36 -04:00
|
|
|
DustTag::DTSection(container) => {
|
2020-06-06 20:48:29 -04:00
|
|
|
let injected_context = ParametersContext::new(self, breadcrumbs, &container.params);
|
2020-05-31 21:30:36 -04:00
|
|
|
let val = walk_path(breadcrumbs, &container.path.keys)
|
|
|
|
.map(|ice| ice.into_context_element(self, breadcrumbs));
|
|
|
|
match val {
|
2020-06-06 23:08:21 -04:00
|
|
|
Err(WalkError::CantWalk) | Ok(None) => {
|
2020-06-06 20:48:29 -04:00
|
|
|
let new_breadcrumbs = self.new_breadcrumbs_section(
|
|
|
|
breadcrumbs,
|
|
|
|
None,
|
|
|
|
Some(&injected_context),
|
|
|
|
&container.explicit_context,
|
|
|
|
None,
|
|
|
|
);
|
2020-06-06 23:08:21 -04:00
|
|
|
return self.render_maybe_body(
|
|
|
|
&container.else_contents,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
blocks,
|
|
|
|
);
|
2020-05-31 21:30:36 -04:00
|
|
|
}
|
2020-06-06 23:08:21 -04:00
|
|
|
Ok(Some(final_val)) => {
|
|
|
|
let context_element = final_val.get_context_element_reference();
|
|
|
|
return if context_element.is_truthy() {
|
|
|
|
match &container.contents {
|
|
|
|
None => Ok("".to_owned()),
|
|
|
|
Some(body) => {
|
|
|
|
let loop_elements: Vec<&dyn ContextElement> =
|
|
|
|
context_element.get_loop_elements();
|
|
|
|
if loop_elements.is_empty() {
|
|
|
|
// Scalar value
|
|
|
|
let new_breadcrumbs = self.new_breadcrumbs_section(
|
|
|
|
breadcrumbs,
|
|
|
|
None,
|
|
|
|
Some(&injected_context),
|
|
|
|
&container.explicit_context,
|
|
|
|
Some(context_element),
|
|
|
|
);
|
|
|
|
self.render_body(
|
|
|
|
body,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
blocks,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Array-like value
|
|
|
|
let total_length = loop_elements.len();
|
|
|
|
let rendered_results: Result<Vec<String>, RenderError> =
|
|
|
|
loop_elements
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, array_elem)| {
|
|
|
|
let index_context =
|
|
|
|
IterationContext::new(i, total_length);
|
|
|
|
let new_breadcrumbs = self
|
|
|
|
.new_breadcrumbs_section(
|
|
|
|
breadcrumbs,
|
|
|
|
Some(&index_context),
|
|
|
|
Some(&injected_context),
|
|
|
|
&container.explicit_context,
|
|
|
|
Some(array_elem),
|
|
|
|
);
|
|
|
|
self.render_body(
|
|
|
|
&body,
|
|
|
|
new_breadcrumbs
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(breadcrumbs),
|
|
|
|
blocks,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let rendered_slice: &[String] = &rendered_results?;
|
|
|
|
return Ok(rendered_slice.join(""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Oddly enough if the value is falsey (like
|
|
|
|
// an empty array or null), Dust uses the
|
|
|
|
// original context before walking the path as
|
|
|
|
// the context for rendering the else block
|
|
|
|
let new_breadcrumbs = self.new_breadcrumbs_section(
|
|
|
|
breadcrumbs,
|
|
|
|
None,
|
|
|
|
Some(&injected_context),
|
|
|
|
&container.explicit_context,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
self.render_maybe_body(
|
|
|
|
&container.else_contents,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
blocks,
|
|
|
|
)
|
|
|
|
};
|
2020-05-31 21:30:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 23:15:12 -04:00
|
|
|
DustTag::DTExists(container) => {
|
|
|
|
let new_breadcrumbs = self.new_breadcrumbs_partial(
|
|
|
|
breadcrumbs,
|
|
|
|
breadcrumbs,
|
|
|
|
None,
|
|
|
|
&container.explicit_context,
|
|
|
|
);
|
|
|
|
let val = walk_path(breadcrumbs, &container.path.keys)
|
|
|
|
.map(|ice| ice.into_context_element(self, breadcrumbs));
|
|
|
|
return match val {
|
|
|
|
Ok(Some(v)) if v.get_context_element_reference().is_truthy() => self
|
|
|
|
.render_maybe_body(
|
|
|
|
&container.contents,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
blocks,
|
|
|
|
),
|
|
|
|
_ => self.render_maybe_body(
|
|
|
|
&container.else_contents,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
blocks,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
DustTag::DTNotExists(container) => {
|
|
|
|
let new_breadcrumbs = self.new_breadcrumbs_partial(
|
|
|
|
breadcrumbs,
|
|
|
|
breadcrumbs,
|
|
|
|
None,
|
|
|
|
&container.explicit_context,
|
|
|
|
);
|
|
|
|
let val = walk_path(breadcrumbs, &container.path.keys)
|
|
|
|
.map(|ice| ice.into_context_element(self, breadcrumbs));
|
|
|
|
return match val {
|
|
|
|
Ok(Some(v)) if v.get_context_element_reference().is_truthy() => self
|
|
|
|
.render_maybe_body(
|
|
|
|
&container.else_contents,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
blocks,
|
|
|
|
),
|
|
|
|
_ => self.render_maybe_body(
|
|
|
|
&container.contents,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
blocks,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
2020-06-06 23:17:50 -04:00
|
|
|
DustTag::DTPartial(partial) => {
|
|
|
|
let partial_name = self.render_partial_name(&partial.name, breadcrumbs)?;
|
|
|
|
if partial.params.is_empty() {
|
|
|
|
let new_breadcrumbs = self.new_breadcrumbs_partial(
|
|
|
|
breadcrumbs,
|
|
|
|
breadcrumbs,
|
|
|
|
None,
|
|
|
|
&partial.explicit_context,
|
|
|
|
);
|
|
|
|
let rendered_content = self.render_template(
|
|
|
|
&partial_name,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
Some(blocks.blocks),
|
|
|
|
)?;
|
|
|
|
return Ok(rendered_content);
|
|
|
|
} else {
|
|
|
|
let injected_context =
|
|
|
|
ParametersContext::new(self, breadcrumbs, &partial.params);
|
|
|
|
let new_breadcrumbs = self.new_breadcrumbs_partial(
|
|
|
|
breadcrumbs,
|
|
|
|
breadcrumbs,
|
|
|
|
Some(&injected_context),
|
|
|
|
&partial.explicit_context,
|
|
|
|
);
|
|
|
|
let rendered_content = self.render_template(
|
|
|
|
&partial_name,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
Some(blocks.blocks),
|
|
|
|
)?;
|
|
|
|
return Ok(rendered_content);
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 23:18:28 -04:00
|
|
|
DustTag::DTInlinePartial(_named_block) => {
|
|
|
|
// Inline partials are blank during rendering (they get injected into blocks)
|
|
|
|
return Ok("".to_owned());
|
|
|
|
}
|
|
|
|
DustTag::DTBlock(named_block) => {
|
|
|
|
let new_breadcrumbs = self.new_breadcrumbs_partial(
|
|
|
|
breadcrumbs,
|
|
|
|
blocks.breadcrumbs,
|
|
|
|
None,
|
|
|
|
&named_block.explicit_context,
|
|
|
|
);
|
|
|
|
return match blocks.blocks.get_block(named_block.path.keys[0]) {
|
|
|
|
None => self.render_maybe_body(
|
|
|
|
&named_block.contents,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
blocks,
|
|
|
|
),
|
|
|
|
Some(inline_partial) => self.render_maybe_body(
|
|
|
|
inline_partial,
|
|
|
|
new_breadcrumbs.as_ref().unwrap_or(breadcrumbs),
|
|
|
|
blocks,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
2020-05-31 21:18:54 -04:00
|
|
|
_ => panic!("Unsupported tag"),
|
|
|
|
}
|
|
|
|
Ok("".to_owned())
|
|
|
|
}
|
|
|
|
|
2020-05-25 22:50:10 -04:00
|
|
|
fn new_breadcrumbs_section<'b>(
|
2020-05-31 21:18:54 -04:00
|
|
|
&'b self,
|
2020-06-06 22:37:29 -04:00
|
|
|
breadcrumbs: &'b Vec<BreadcrumbTreeElement<'b>>,
|
2020-05-30 17:50:27 -04:00
|
|
|
index_context: Option<&'b dyn IntoContextElement>,
|
|
|
|
injected_context: Option<&'b dyn IntoContextElement>,
|
2020-05-25 22:50:10 -04:00
|
|
|
explicit_context: &Option<Path<'b>>,
|
|
|
|
new_context_element: Option<&'b dyn ContextElement>,
|
2020-06-06 22:37:29 -04:00
|
|
|
) -> Option<Vec<BreadcrumbTreeElement<'b>>> {
|
2020-05-25 22:50:10 -04:00
|
|
|
// If none of the additional contexts are present, return None
|
|
|
|
// to signal that the original breadcrumbs should be used
|
|
|
|
// rather than incurring a copy here.
|
|
|
|
match (
|
|
|
|
index_context,
|
|
|
|
injected_context,
|
|
|
|
explicit_context,
|
|
|
|
new_context_element,
|
|
|
|
) {
|
|
|
|
(None, None, None, None) => return None,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:18:21 -04:00
|
|
|
// If there is an explicit context, then drop all the current
|
|
|
|
// context
|
2020-06-06 22:37:29 -04:00
|
|
|
let mut new_stack = match explicit_context {
|
|
|
|
Some(_) => Vec::with_capacity(4),
|
|
|
|
None => breadcrumbs.clone(),
|
2020-05-25 22:50:10 -04:00
|
|
|
};
|
2020-05-31 18:18:21 -04:00
|
|
|
|
2020-06-06 17:52:46 -04:00
|
|
|
explicit_context.as_ref().map(|path| {
|
2020-06-06 22:37:29 -04:00
|
|
|
walk_path(breadcrumbs, &path.keys)
|
|
|
|
.map(|ice| ice.into_context_element(self, breadcrumbs))
|
2020-05-31 22:17:58 -04:00
|
|
|
.ok()
|
|
|
|
.flatten()
|
2020-05-31 18:27:55 -04:00
|
|
|
.map(|val| {
|
2020-05-31 23:47:20 -04:00
|
|
|
if val.get_context_element_reference().is_truthy() {
|
2020-06-06 22:37:29 -04:00
|
|
|
new_stack.push(std::convert::From::from(val))
|
2020-05-31 18:27:55 -04:00
|
|
|
}
|
|
|
|
});
|
2020-06-06 17:52:46 -04:00
|
|
|
});
|
2020-06-06 22:37:29 -04:00
|
|
|
injected_context.map(|ctx| new_stack.push(BreadcrumbTreeElement::from_borrowed(ctx)));
|
2020-06-06 20:24:01 -04:00
|
|
|
new_context_element.map(|ctx| {
|
2020-06-06 22:37:29 -04:00
|
|
|
new_stack.push(BreadcrumbTreeElement::from_borrowed(
|
2020-06-06 20:24:01 -04:00
|
|
|
ctx.from_context_element(),
|
|
|
|
))
|
|
|
|
});
|
2020-06-06 22:37:29 -04:00
|
|
|
index_context.map(|ctx| new_stack.push(BreadcrumbTreeElement::from_borrowed(ctx)));
|
2020-05-31 18:18:21 -04:00
|
|
|
|
2020-06-06 22:37:29 -04:00
|
|
|
Some(new_stack)
|
2020-05-05 20:22:25 -04:00
|
|
|
}
|
2020-05-31 20:41:27 -04:00
|
|
|
|
|
|
|
fn new_breadcrumbs_partial<'b>(
|
2020-05-31 20:43:07 -04:00
|
|
|
&'b self,
|
2020-06-06 22:45:58 -04:00
|
|
|
breadcrumbs: &'b Vec<BreadcrumbTreeElement<'b>>,
|
|
|
|
explicit_context_breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
|
2020-05-31 20:41:27 -04:00
|
|
|
injected_context: Option<&'b dyn IntoContextElement>,
|
|
|
|
explicit_context: &Option<Path<'b>>,
|
2020-06-06 22:45:58 -04:00
|
|
|
) -> Option<Vec<BreadcrumbTreeElement<'b>>> {
|
2020-05-31 20:41:27 -04:00
|
|
|
// If none of the additional contexts are present, return None
|
|
|
|
// to signal that the original breadcrumbs should be used
|
|
|
|
// rather than incurring a copy here.
|
|
|
|
match (injected_context, explicit_context) {
|
|
|
|
(None, None) => return None,
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
// If there is an explicit context, then drop all the current
|
|
|
|
// context
|
2020-06-06 22:45:58 -04:00
|
|
|
let mut new_stack = match explicit_context {
|
|
|
|
Some(_) => Vec::with_capacity(3),
|
|
|
|
None => breadcrumbs.clone(),
|
2020-05-31 20:41:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
injected_context.map(|ctx| {
|
|
|
|
// Special case: when there is no explicit context, the
|
|
|
|
// injected context gets inserted 1 spot behind the
|
|
|
|
// current context. Otherwise, the injected context gets
|
|
|
|
// added after the current context but before the explicit
|
|
|
|
// context.
|
|
|
|
match explicit_context {
|
2020-06-06 23:08:21 -04:00
|
|
|
None => new_stack.insert(
|
|
|
|
Self::get_index_of_first_non_pseudo_element(&new_stack).unwrap_or(0),
|
|
|
|
BreadcrumbTreeElement::from_borrowed(ctx),
|
|
|
|
),
|
|
|
|
_ => new_stack.push(BreadcrumbTreeElement::from_borrowed(ctx)),
|
2020-05-31 20:41:27 -04:00
|
|
|
}
|
|
|
|
});
|
2020-06-06 22:45:58 -04:00
|
|
|
|
2020-06-06 17:52:46 -04:00
|
|
|
explicit_context.as_ref().map(|path| {
|
2020-05-31 20:41:27 -04:00
|
|
|
// TODO: should resolving the value here use
|
|
|
|
// explicit_context_maybe_breadcrumbs or
|
|
|
|
// maybe_breadcrumbs?
|
2020-06-06 22:45:58 -04:00
|
|
|
walk_path(explicit_context_breadcrumbs, &path.keys)
|
|
|
|
.map(|ice| ice.into_context_element(self, breadcrumbs))
|
2020-05-31 22:17:58 -04:00
|
|
|
.ok()
|
|
|
|
.flatten()
|
2020-05-31 20:41:27 -04:00
|
|
|
.map(|val| {
|
2020-06-06 15:20:43 -04:00
|
|
|
if val.get_context_element_reference().is_truthy() {
|
2020-06-06 22:45:58 -04:00
|
|
|
new_stack.push(std::convert::From::from(val));
|
2020-05-31 20:41:27 -04:00
|
|
|
}
|
|
|
|
});
|
2020-06-06 17:52:46 -04:00
|
|
|
});
|
2020-05-31 20:41:27 -04:00
|
|
|
|
2020-06-06 22:45:58 -04:00
|
|
|
Some(new_stack)
|
2020-05-31 20:41:27 -04:00
|
|
|
}
|
2020-05-31 21:26:31 -04:00
|
|
|
|
|
|
|
fn preprocess_filters(filters: &Vec<Filter>) -> Vec<Filter> {
|
|
|
|
let mut final_filters: Vec<Filter> = filters
|
|
|
|
.into_iter()
|
|
|
|
.filter(|f| f != &&Filter::DisableHtmlEncode)
|
|
|
|
.map(|f| f.clone())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// If the user has not specified any escaping filter (|s or
|
|
|
|
// |h), automatically add an html escape filter
|
|
|
|
if !filters.iter().any(|f| f == &Filter::DisableHtmlEncode) {
|
|
|
|
final_filters.push(Filter::HtmlEncode);
|
|
|
|
}
|
|
|
|
final_filters
|
|
|
|
}
|
2020-06-06 22:45:58 -04:00
|
|
|
|
2020-06-06 23:08:21 -04:00
|
|
|
fn get_index_of_first_non_pseudo_element<'b>(
|
|
|
|
breadcrumbs: &'b Vec<BreadcrumbTreeElement<'b>>,
|
|
|
|
) -> Option<usize> {
|
|
|
|
breadcrumbs.iter().rposition(|b| {
|
|
|
|
std::borrow::Borrow::<dyn IntoContextElement + 'b>::borrow(b).is_pseudo_element()
|
|
|
|
})
|
2020-06-06 22:45:58 -04:00
|
|
|
}
|
2020-04-11 20:31:44 -04:00
|
|
|
}
|
2020-05-31 21:18:54 -04:00
|
|
|
|
|
|
|
struct BlockContext<'a> {
|
|
|
|
/// The breadcrumbs at the time of entering the current partial
|
2020-06-06 22:24:27 -04:00
|
|
|
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
|
2020-05-31 21:18:54 -04:00
|
|
|
blocks: &'a InlinePartialTreeElement<'a>,
|
|
|
|
}
|