Struct Content
pub struct Content { /* private fields */ }
Expand description
A piece of document content.
This type is at the heart of Typst. All markup you write and most functions you call produce content values. You can create a content value by enclosing markup in square brackets. This is also how you pass content to functions.
§Example
Type of *Hello!* is
#type([*Hello!*])
Content can be added with the +
operator,
joined together and multiplied with integers. Wherever
content is expected, you can also pass a string or {none}
.
§Representation
Content consists of elements with fields. When constructing an element with its element function, you provide these fields as arguments and when you have a content value, you can access its fields with field access syntax.
Some fields are required: These must be provided when constructing an element and as a consequence, they are always available through field access on content of that type. Required fields are marked as such in the documentation.
Most fields are optional: Like required fields, they can be passed to the element function to configure them for a single element. However, these can also be configured with set rules to apply them to all elements within a scope. Optional fields are only available with field access syntax when they were explicitly passed to the element function, not when they result from a set rule.
Each element has a default appearance. However, you can also completely customize its appearance with a show rule. The show rule is passed the element. It can access the element’s field and produce arbitrary content from it.
In the web app, you can hover over a content variable to see exactly which
elements the content is composed of and what fields they have.
Alternatively, you can inspect the output of the [repr
] function.
Implementations§
§impl Content
impl Content
pub fn new<T>(elem: T) -> Contentwhere
T: NativeElement,
pub fn new<T>(elem: T) -> Contentwhere
T: NativeElement,
Creates a new content from an element.
pub fn span(&self) -> Span
pub fn span(&self) -> Span
Get the span of the content.
pub fn labelled_at(&self) -> Span
pub fn labelled_at(&self) -> Span
Get the span where the label is attached.
pub fn located(self, loc: Location) -> Content
pub fn located(self, loc: Location) -> Content
Assigns a location to the content.
This identifies the content and e.g. makes it linkable by
.linked(Destination::Location(loc))
.
Useful in combination with [Location::variant
].
pub fn set_location(&mut self, location: Location)
pub fn set_location(&mut self, location: Location)
Set the location of the content.
pub fn is_guarded(&self, index: RecipeIndex) -> bool
pub fn is_guarded(&self, index: RecipeIndex) -> bool
Check whether a show rule recipe is disabled.
pub fn guarded(self, index: RecipeIndex) -> Content
pub fn guarded(self, index: RecipeIndex) -> Content
Disable a show rule recipe.
pub fn is_prepared(&self) -> bool
pub fn is_prepared(&self) -> bool
Whether this content has already been prepared.
pub fn mark_prepared(&mut self)
pub fn mark_prepared(&mut self)
Mark this content as prepared.
pub fn get(
&self,
id: u8,
styles: Option<StyleChain<'_>>,
) -> Result<Value, FieldAccessError>
pub fn get( &self, id: u8, styles: Option<StyleChain<'_>>, ) -> Result<Value, FieldAccessError>
Get a field by ID.
This is the preferred way to access fields. However, you can only use it
if you have set the field IDs yourself or are using the field IDs
generated by the #[elem]
macro.
pub fn get_by_name(&self, name: &str) -> Result<Value, FieldAccessError>
pub fn get_by_name(&self, name: &str) -> Result<Value, FieldAccessError>
Get a field by name.
If you have access to the field IDs of the element, use Self::get
instead.
pub fn field(&self, id: u8) -> Result<Value, EcoString>
pub fn field(&self, id: u8) -> Result<Value, EcoString>
Get a field by ID, returning a missing field error if it does not exist.
This is the preferred way to access fields. However, you can only use it
if you have set the field IDs yourself or are using the field IDs
generated by the #[elem]
macro.
pub fn field_by_name(&self, name: &str) -> Result<Value, EcoString>
pub fn field_by_name(&self, name: &str) -> Result<Value, EcoString>
Get a field by name, returning a missing field error if it does not exist.
If you have access to the field IDs of the element, use Self::field
instead.
pub fn materialize(&mut self, styles: StyleChain<'_>)
pub fn materialize(&mut self, styles: StyleChain<'_>)
Resolve all fields with the styles and save them in-place.
pub fn sequence(iter: impl IntoIterator<Item = Content>) -> Content
pub fn sequence(iter: impl IntoIterator<Item = Content>) -> Content
Create a new sequence element from multiples elements.
pub fn is<T>(&self) -> boolwhere
T: NativeElement,
pub fn is<T>(&self) -> boolwhere
T: NativeElement,
Whether the contained element is of type T
.
pub fn to_packed<T>(&self) -> Option<&Packed<T>>where
T: NativeElement,
pub fn to_packed<T>(&self) -> Option<&Packed<T>>where
T: NativeElement,
Downcasts the element to a packed value.
pub fn to_packed_mut<T>(&mut self) -> Option<&mut Packed<T>>where
T: NativeElement,
pub fn to_packed_mut<T>(&mut self) -> Option<&mut Packed<T>>where
T: NativeElement,
Downcasts the element to a mutable packed value.
pub fn into_packed<T>(self) -> Result<Packed<T>, Content>where
T: NativeElement,
pub fn into_packed<T>(self) -> Result<Packed<T>, Content>where
T: NativeElement,
Downcasts the element into an owned packed value.
pub fn unpack<T>(self) -> Result<T, Content>where
T: NativeElement,
pub fn unpack<T>(self) -> Result<T, Content>where
T: NativeElement,
Extract the raw underlying element.
pub fn can<C>(&self) -> boolwhere
C: 'static + ?Sized,
pub fn can<C>(&self) -> boolwhere
C: 'static + ?Sized,
Whether the contained element has the given capability.
pub fn with<C>(&self) -> Option<&C>where
C: 'static + ?Sized,
pub fn with<C>(&self) -> Option<&C>where
C: 'static + ?Sized,
Cast to a trait object if the contained element has the given capability.
pub fn with_mut<C>(&mut self) -> Option<&mut C>where
C: 'static + ?Sized,
pub fn with_mut<C>(&mut self) -> Option<&mut C>where
C: 'static + ?Sized,
Cast to a mutable trait object if the contained element has the given capability.
pub fn sequence_recursive_for_each<'a>(
&'a self,
f: &mut impl FnMut(&'a Content),
)
pub fn sequence_recursive_for_each<'a>( &'a self, f: &mut impl FnMut(&'a Content), )
Also auto expands sequence of sequences into flat sequence
pub fn styled_with_recipe(
self,
engine: &mut Engine<'_>,
context: Tracked<'_, Context<'_>>,
recipe: Recipe,
) -> Result<Content, EcoVec<SourceDiagnostic>>
pub fn styled_with_recipe( self, engine: &mut Engine<'_>, context: Tracked<'_, Context<'_>>, recipe: Recipe, ) -> Result<Content, EcoVec<SourceDiagnostic>>
Style this content with a recipe, eagerly applying it if possible.
pub fn styled_with_map(self, styles: Styles) -> Content
pub fn styled_with_map(self, styles: Styles) -> Content
Style this content with a full style map.
pub fn style_in_place(&mut self, styles: Styles)
pub fn style_in_place(&mut self, styles: Styles)
Style this content with a full style map in-place.
pub fn query(&self, selector: Selector) -> Vec<Content>
pub fn query(&self, selector: Selector) -> Vec<Content>
Queries the content tree for all elements that match the given selector.
Elements produced in show
rules will not be included in the results.
pub fn query_first(&self, selector: &Selector) -> Option<Content>
pub fn query_first(&self, selector: &Selector) -> Option<Content>
Queries the content tree for the first element that match the given selector.
Elements produced in show
rules will not be included in the results.
pub fn plain_text(&self) -> EcoString
pub fn plain_text(&self) -> EcoString
Extracts the plain text of this content.
§impl Content
impl Content
pub fn underlined(self) -> Content
pub fn underlined(self) -> Content
Underline this content.
§impl Content
impl Content
pub fn func(&self) -> Element
pub fn func(&self) -> Element
The content’s element function. This function can be used to create the element contained in this content. It can be used in set and show rules for the element. Can be compared with global functions to check whether you have a specific kind of element.
pub fn at(&self, field: Str, default: Option<Value>) -> Result<Value, EcoString>
pub fn at(&self, field: Str, default: Option<Value>) -> Result<Value, EcoString>
Access the specified field on the content. Returns the default value if the field does not exist or fails with an error if no default value was specified.
Trait Implementations§
§impl AddAssign<&Content> for Content
impl AddAssign<&Content> for Content
§fn add_assign(&mut self, rhs: &Content)
fn add_assign(&mut self, rhs: &Content)
+=
operation. Read more§impl AddAssign for Content
impl AddAssign for Content
§fn add_assign(&mut self, rhs: Content)
fn add_assign(&mut self, rhs: Content)
+=
operation. Read more§impl<T> From<T> for Contentwhere
T: NativeElement,
impl<T> From<T> for Contentwhere
T: NativeElement,
§impl FromValue for Content
impl FromValue for Content
§fn from_value(value: Value) -> Result<Content, HintedString>
fn from_value(value: Value) -> Result<Content, HintedString>
Self
.§impl NativeScope for Content
impl NativeScope for Content
§fn constructor() -> Option<&'static NativeFuncData>
fn constructor() -> Option<&'static NativeFuncData>
§impl NativeType for Content
impl NativeType for Content
§impl Reflect for Content
impl Reflect for Content
§fn error(found: &Value) -> HintedString
fn error(found: &Value) -> HintedString
§impl Serialize for Content
impl Serialize for Content
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Auto Trait Implementations§
impl Freeze for Content
impl !RefUnwindSafe for Content
impl Send for Content
impl Sync for Content
impl Unpin for Content
impl !UnwindSafe for Content
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CallHasher for T
impl<T> CallHasher for T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters
when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
§impl<F, W, T, D> Deserialize<With<T, W>, D> for F
impl<F, W, T, D> Deserialize<With<T, W>, D> for F
§fn deserialize(
&self,
deserializer: &mut D,
) -> Result<With<T, W>, <D as Fallible>::Error>
fn deserialize( &self, deserializer: &mut D, ) -> Result<With<T, W>, <D as Fallible>::Error>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Filterable for T
impl<T> Filterable for T
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle
.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other
into Self
, while performing the appropriate scaling,
rounding and clamping.§impl<T> FromValue<Spanned<Value>> for Twhere
T: FromValue,
impl<T> FromValue<Spanned<Value>> for Twhere
T: FromValue,
§fn from_value(value: Spanned<Value>) -> Result<T, HintedString>
fn from_value(value: Spanned<Value>) -> Result<T, HintedString>
Self
.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T
.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters
when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoResult for Twhere
T: IntoValue,
impl<T> IntoResult for Twhere
T: IntoValue,
§fn into_result(self, _: Span) -> Result<Value, EcoVec<SourceDiagnostic>>
fn into_result(self, _: Span) -> Result<Value, EcoVec<SourceDiagnostic>>
Source§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self
into T
, while performing the appropriate scaling,
rounding and clamping.§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors
fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds
error is returned which contains
the unclamped color. Read more