diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-07 12:21:12 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-07 12:46:05 +0100 |
| commit | efd1853d069fbd1476e82d015da4d0d04cfaccc0 (patch) | |
| tree | 842b745c134306539d10c61be9485794fe8dc7dc /src/model/content.rs | |
| parent | eb951c008beea502042db4a3a0e8d1f8b51f6f52 (diff) | |
Show it!
- New show rule syntax
- Set if syntax
- Removed wrap syntax
Diffstat (limited to 'src/model/content.rs')
| -rw-r--r-- | src/model/content.rs | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/src/model/content.rs b/src/model/content.rs index 7b09c697..0257f4da 100644 --- a/src/model/content.rs +++ b/src/model/content.rs @@ -5,13 +5,14 @@ use std::iter::{self, Sum}; use std::ops::{Add, AddAssign}; use std::sync::Arc; +use comemo::Tracked; use siphasher::sip128::{Hasher128, SipHasher}; use typst_macros::node; -use super::{Args, Key, Property, Selector, StyleEntry, StyleMap, Vm}; -use crate as typst; +use super::{Args, Key, Property, Recipe, Selector, StyleEntry, StyleMap, Value, Vm}; use crate::diag::{SourceResult, StrResult}; -use crate::util::{EcoString, ReadableTypeId}; +use crate::util::ReadableTypeId; +use crate::World; /// Composable representation of styled content. /// @@ -27,11 +28,6 @@ impl Content { SequenceNode(vec![]).pack() } - /// Create content from a string of text. - pub fn text(text: impl Into<EcoString>) -> Self { - item!(text)(text.into()) - } - /// Create a new sequence node from multiples nodes. pub fn sequence(seq: Vec<Self>) -> Self { match seq.as_slice() { @@ -65,6 +61,11 @@ impl Content { Arc::get_mut(&mut self.0)?.as_any_mut().downcast_mut::<T>() } + /// Access a field on this content. + pub fn field(&self, name: &str) -> Option<Value> { + self.0.field(name) + } + /// Whether this content has the given capability. pub fn has<C>(&self) -> bool where @@ -97,6 +98,19 @@ impl Content { self.styled_with_entry(StyleEntry::Property(Property::new(key, value))) } + /// Style this content with a recipe, eagerly applying it if possible. + pub fn styled_with_recipe( + self, + world: Tracked<dyn World>, + recipe: Recipe, + ) -> SourceResult<Self> { + if recipe.pattern.is_none() { + recipe.transform.apply(world, recipe.span, || Value::Content(self)) + } else { + Ok(self.styled_with_entry(StyleEntry::Recipe(recipe))) + } + } + /// Style this content with a style entry. pub fn styled_with_entry(mut self, entry: StyleEntry) -> Self { if let Some(styled) = self.try_downcast_mut::<StyledNode>() { @@ -242,6 +256,9 @@ pub trait Node: 'static { where Self: Sized; + /// Access a field on this node. + fn field(&self, name: &str) -> Option<Value>; + /// A unique identifier of the node type. fn id(&self) -> NodeId; |
