diff --git a/src/eval/template.rs b/src/eval/template.rs index 11ea3f56c..fe3d0ccaf 100644 --- a/src/eval/template.rs +++ b/src/eval/template.rs @@ -22,7 +22,7 @@ pub struct Template(Rc>); #[derive(Clone)] enum TemplateNode { /// A word space. - Space(Vec), + Space, /// A line break. Linebreak, /// A paragraph break. @@ -30,11 +30,13 @@ enum TemplateNode { /// A page break. Pagebreak(bool), /// Plain text. - Text(EcoString, Vec), + Text(EcoString), /// Spacing. Spacing(GenAxis, Linear), + /// A decorated template. + Decorated(Decoration, Template), /// An inline node builder. - Inline(Rc InlineNode>, Vec), + Inline(Rc InlineNode>), /// An block node builder. Block(Rc BlockNode>), /// Save the current style. @@ -57,7 +59,7 @@ impl Template { F: Fn(&Style) -> T + 'static, T: Into, { - let node = TemplateNode::Inline(Rc::new(move |s| f(s).into()), vec![]); + let node = TemplateNode::Inline(Rc::new(move |s| f(s).into())); Self(Rc::new(vec![node])) } @@ -73,7 +75,7 @@ impl Template { /// Add a word space to the template. pub fn space(&mut self) { - self.make_mut().push(TemplateNode::Space(vec![])); + self.make_mut().push(TemplateNode::Space); } /// Add a line break to the template. @@ -93,7 +95,7 @@ impl Template { /// Add text to the template. pub fn text(&mut self, text: impl Into) { - self.make_mut().push(TemplateNode::Text(text.into(), vec![])); + self.make_mut().push(TemplateNode::Text(text.into())); } /// Add text, but in monospace. @@ -109,19 +111,6 @@ impl Template { self.make_mut().push(TemplateNode::Spacing(axis, spacing)); } - /// Add a decoration to all contained nodes. - pub fn decorate(&mut self, deco: Decoration) { - for node in self.make_mut() { - let decos = match node { - TemplateNode::Space(decos) => decos, - TemplateNode::Text(_, decos) => decos, - TemplateNode::Inline(_, decos) => decos, - _ => continue, - }; - decos.push(deco.clone()); - } - } - /// Register a restorable snapshot. pub fn save(&mut self) { self.make_mut().push(TemplateNode::Save); @@ -154,6 +143,11 @@ impl Template { wrapper } + /// Add a decoration to all contained nodes. + pub fn decorate(self, deco: Decoration) -> Self { + Self(Rc::new(vec![TemplateNode::Decorated(deco, self)])) + } + /// Build the stack node resulting from instantiating the template with the /// given style. pub fn to_stack(&self, style: &Style) -> StackNode { @@ -223,7 +217,7 @@ impl Add for Template { type Output = Self; fn add(mut self, rhs: Str) -> Self::Output { - Rc::make_mut(&mut self.0).push(TemplateNode::Text(rhs.into(), vec![])); + Rc::make_mut(&mut self.0).push(TemplateNode::Text(rhs.into())); self } } @@ -232,7 +226,7 @@ impl Add