diff --git a/src/eval/template.rs b/src/eval/template.rs index addbb466f..0ab49d046 100644 --- a/src/eval/template.rs +++ b/src/eval/template.rs @@ -20,7 +20,7 @@ pub struct Template(Rc>); #[derive(Clone)] enum TemplateNode { /// A word space. - Space, + Space(Vec), /// A line break. Linebreak, /// A paragraph break. @@ -28,11 +28,11 @@ enum TemplateNode { /// A page break. Pagebreak(bool), /// Plain text. - Text(EcoString), + Text(EcoString, Vec), /// Spacing. Spacing(GenAxis, Linear), /// An inline node builder. - Inline(Rc LayoutNode>), + Inline(Rc LayoutNode>, Vec), /// An block node builder. Block(Rc LayoutNode>), /// Save the current state. @@ -43,6 +43,13 @@ enum TemplateNode { Modify(Rc), } +/// A template node decoration. +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub enum Decoration { + /// A link. + Link(EcoString), +} + impl Template { /// Create a new, empty template. pub fn new() -> Self { @@ -55,7 +62,7 @@ impl Template { F: Fn(&State) -> T + 'static, T: Into, { - let node = TemplateNode::Inline(Rc::new(move |s| f(s).into())); + let node = TemplateNode::Inline(Rc::new(move |s| f(s).into()), vec![]); Self(Rc::new(vec![node])) } @@ -71,7 +78,7 @@ impl Template { /// Add a word space to the template. pub fn space(&mut self) { - self.make_mut().push(TemplateNode::Space); + self.make_mut().push(TemplateNode::Space(vec![])); } /// Add a line break to the template. @@ -91,7 +98,7 @@ impl Template { /// Add text to the template. pub fn text(&mut self, text: impl Into) { - self.make_mut().push(TemplateNode::Text(text.into())); + self.make_mut().push(TemplateNode::Text(text.into(), vec![])); } /// Add text, but in monospace. @@ -107,6 +114,19 @@ impl Template { self.make_mut().push(TemplateNode::Spacing(axis, spacing)); } + /// Add a decoration to the last template node. + 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); @@ -201,7 +221,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())); + Rc::make_mut(&mut self.0).push(TemplateNode::Text(rhs.into(), vec![])); self } } @@ -210,7 +230,7 @@ impl Add