summaryrefslogtreecommitdiff
path: root/src/model/content.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/model/content.rs')
-rw-r--r--src/model/content.rs33
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;