diff options
Diffstat (limited to 'src/model/realize.rs')
| -rw-r--r-- | src/model/realize.rs | 96 |
1 files changed, 46 insertions, 50 deletions
diff --git a/src/model/realize.rs b/src/model/realize.rs index 634a31fd..51d69fdc 100644 --- a/src/model/realize.rs +++ b/src/model/realize.rs @@ -1,4 +1,4 @@ -use super::{Content, MetaNode, Node, NodeId, Recipe, Selector, StyleChain, Vt}; +use super::{Content, ElemFunc, Element, MetaElem, Recipe, Selector, StyleChain, Vt}; use crate::diag::SourceResult; use crate::doc::Meta; use crate::util::hash128; @@ -35,28 +35,28 @@ pub fn realize( ) -> SourceResult<Option<Content>> { // Pre-process. if target.needs_preparation() { - let mut node = target.clone(); + let mut elem = target.clone(); if target.can::<dyn Locatable>() || target.label().is_some() { - let id = vt.provider.identify(hash128(target)); - node.set_stable_id(id); + let location = vt.provider.locate(hash128(target)); + elem.set_location(location); } - if let Some(node) = node.with_mut::<dyn Synthesize>() { - node.synthesize(vt, styles); + if let Some(elem) = elem.with_mut::<dyn Synthesize>() { + elem.synthesize(vt, styles); } - node.mark_prepared(); + elem.mark_prepared(); - if node.stable_id().is_some() { - let span = node.span(); - let meta = Meta::Node(node.clone()); + if elem.location().is_some() { + let span = elem.span(); + let meta = Meta::Elem(elem.clone()); return Ok(Some( - (node + MetaNode::new().pack().spanned(span)) - .styled(MetaNode::set_data(vec![meta])), + (elem + MetaElem::new().pack().spanned(span)) + .styled(MetaElem::set_data(vec![meta])), )); } - return Ok(Some(node)); + return Ok(Some(elem)); } // Find out how many recipes there are. @@ -77,17 +77,17 @@ pub fn realize( // Realize if there was no matching recipe. if let Some(showable) = target.with::<dyn Show>() { - let guard = Guard::Base(target.id()); + let guard = Guard::Base(target.func()); if realized.is_none() && !target.is_guarded(guard) { realized = Some(showable.show(vt, styles)?); } } - // Finalize only if this is the first application for this node. - if let Some(node) = target.with::<dyn Finalize>() { + // Finalize only if this is the first application for this element. + if let Some(elem) = target.with::<dyn Finalize>() { if target.is_pristine() { if let Some(already) = realized { - realized = Some(node.finalize(already, styles)); + realized = Some(elem.finalize(already, styles)); } } } @@ -103,8 +103,8 @@ fn try_apply( guard: Guard, ) -> SourceResult<Option<Content>> { match &recipe.selector { - Some(Selector::Node(id, _)) => { - if target.id() != *id { + Some(Selector::Elem(element, _)) => { + if target.func() != *element { return Ok(None); } @@ -124,22 +124,17 @@ fn try_apply( return Ok(None); }; - let make = |s| { - let mut content = item!(text)(s); - content.copy_modifiers(target); - content - }; - + let make = |s: &str| target.clone().with_field("text", s); let mut result = vec![]; let mut cursor = 0; for m in regex.find_iter(&text) { let start = m.start(); if cursor < start { - result.push(make(text[cursor..start].into())); + result.push(make(&text[cursor..start])); } - let piece = make(m.as_str().into()).guarded(guard); + let piece = make(m.as_str()).guarded(guard); let transformed = recipe.apply_vt(vt, piece)?; result.push(transformed); cursor = m.end(); @@ -150,7 +145,7 @@ fn try_apply( } if cursor < text.len() { - result.push(make(text[cursor..].into())); + result.push(make(&text[cursor..])); } Ok(Some(Content::sequence(result))) @@ -163,55 +158,56 @@ fn try_apply( } } -/// Makes this node locatable through `vt.locate`. +/// Makes this element locatable through `vt.locate`. pub trait Locatable {} -/// Synthesize fields on a node. This happens before execution of any show rule. +/// Synthesize fields on an element. This happens before execution of any show +/// rule. pub trait Synthesize { - /// Prepare the node for show rule application. + /// Prepare the element for show rule application. fn synthesize(&mut self, vt: &Vt, styles: StyleChain); } -/// The base recipe for a node. +/// The base recipe for an element. pub trait Show { - /// Execute the base recipe for this node. + /// Execute the base recipe for this element. fn show(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Content>; } -/// Post-process a node after it was realized. +/// Post-process an element after it was realized. pub trait Finalize { - /// Finalize the fully realized form of the node. Use this for effects that + /// Finalize the fully realized form of the element. Use this for effects that /// should work even in the face of a user-defined show rule, for example - /// the linking behaviour of a link node. + /// the linking behaviour of a link element. fn finalize(&self, realized: Content, styles: StyleChain) -> Content; } -/// How a node interacts with other nodes. +/// How the element interacts with other elements. pub trait Behave { - /// The node's interaction behaviour. + /// The element's interaction behaviour. fn behaviour(&self) -> Behaviour; - /// Whether this weak node is larger than a previous one and thus picked as - /// the maximum when the levels are the same. + /// Whether this weak element is larger than a previous one and thus picked + /// as the maximum when the levels are the same. #[allow(unused_variables)] fn larger(&self, prev: &Content) -> bool { false } } -/// How a node interacts with other nodes in a stream. +/// How an element interacts with other elements in a stream. #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum Behaviour { - /// A weak node which only survives when a supportive node is before and - /// after it. Furthermore, per consecutive run of weak nodes, only one - /// survives: The one with the lowest weakness level (or the larger one if - /// there is a tie). + /// A weak element which only survives when a supportive element is before + /// and after it. Furthermore, per consecutive run of weak elements, only + /// one survives: The one with the lowest weakness level (or the larger one + /// if there is a tie). Weak(usize), - /// A node that enables adjacent weak nodes to exist. The default. + /// An element that enables adjacent weak elements to exist. The default. Supportive, - /// A node that destroys adjacent weak nodes. + /// An element that destroys adjacent weak elements. Destructive, - /// A node that does not interact at all with other nodes, having the + /// An element that does not interact at all with other elements, having the /// same effect as if it didn't exist. Ignorant, } @@ -221,6 +217,6 @@ pub enum Behaviour { pub enum Guard { /// The nth recipe from the top of the chain. Nth(usize), - /// The [base recipe](Show) for a kind of node. - Base(NodeId), + /// The [base recipe](Show) for a kind of element. + Base(ElemFunc), } |
