From bbc1a24631ad0a4f72e881e64da7d03aaaa4ef36 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 31 May 2020 21:26:31 -0400 Subject: [PATCH] Added back in reference. --- src/renderer/renderer.rs | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 34f04fa..3aa1d2d 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -1,8 +1,10 @@ use crate::parser::template; use crate::parser::Body; use crate::parser::DustTag; +use crate::parser::Filter; use crate::parser::PartialNameElement; use crate::parser::Path; +use crate::parser::Special; use crate::parser::Template; use crate::parser::TemplateElement; use crate::renderer::breadcrumb_tree::BreadcrumbTree; @@ -11,6 +13,7 @@ use crate::renderer::context_element::ContextElement; use crate::renderer::context_element::IntoContextElement; use crate::renderer::errors::CompileError; use crate::renderer::errors::RenderError; +use crate::renderer::errors::WalkError; use crate::renderer::inline_partial_tree::extract_inline_partials; use crate::renderer::inline_partial_tree::InlinePartialTreeElement; use crate::renderer::tree_walking::walk_path; @@ -132,6 +135,32 @@ impl<'a> DustRenderer<'a> { blocks: &'a BlockContext<'a>, ) -> Result { match tag { + 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 { + Err(WalkError::CantWalk) => return Ok("".to_owned()), + Ok(final_val) => { + return if final_val.is_truthy() { + final_val.render(&Self::preprocess_filters(&reference.filters)) + } else { + Ok("".to_owned()) + }; + } + } + } _ => panic!("Unsupported tag"), } Ok("".to_owned()) @@ -281,6 +310,21 @@ impl<'a> DustRenderer<'a> { } } } + + fn preprocess_filters(filters: &Vec) -> Vec { + let mut final_filters: Vec = 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 + } } struct BlockContext<'a> {