summaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-03-11 12:59:55 +0100
committerLaurenz <laurmaedje@gmail.com>2022-03-11 23:36:06 +0100
commit5ac7eb3860ebd3247f6486c227e816894cb8fd91 (patch)
treea29a868c681c7de39f15f2d9d3f031db1861b90a /src/eval
parent5ce2a006b6d45d29be15e4562ae3ab4fc1b8e97c (diff)
Rename template to content
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/array.rs2
-rw-r--r--src/eval/capture.rs4
-rw-r--r--src/eval/class.rs19
-rw-r--r--src/eval/content.rs (renamed from src/eval/template.rs)160
-rw-r--r--src/eval/layout.rs2
-rw-r--r--src/eval/mod.rs68
-rw-r--r--src/eval/module.rs4
-rw-r--r--src/eval/ops.rs25
-rw-r--r--src/eval/show.rs8
-rw-r--r--src/eval/styles.rs4
-rw-r--r--src/eval/value.rs36
11 files changed, 164 insertions, 168 deletions
diff --git a/src/eval/array.rs b/src/eval/array.rs
index 7ff5fb74..294d2871 100644
--- a/src/eval/array.rs
+++ b/src/eval/array.rs
@@ -89,7 +89,7 @@ impl Array {
a.partial_cmp(b).unwrap_or_else(|| {
if result.is_ok() {
result = Err(format!(
- "cannot compare {} with {}",
+ "cannot order {} and {}",
a.type_name(),
b.type_name(),
));
diff --git a/src/eval/capture.rs b/src/eval/capture.rs
index b27aceb7..4e8d7604 100644
--- a/src/eval/capture.rs
+++ b/src/eval/capture.rs
@@ -49,8 +49,8 @@ impl<'a> CapturesVisitor<'a> {
// through the expressions that contain them).
Some(Expr::Ident(ident)) => self.capture(ident),
- // Blocks and templates create a scope.
- Some(Expr::Code(_) | Expr::Template(_)) => {
+ // Code and content blocks create a scope.
+ Some(Expr::Code(_) | Expr::Content(_)) => {
self.internal.enter();
for child in node.children() {
self.visit(child);
diff --git a/src/eval/class.rs b/src/eval/class.rs
index 2cced74d..05191667 100644
--- a/src/eval/class.rs
+++ b/src/eval/class.rs
@@ -2,24 +2,25 @@ use std::any::TypeId;
use std::fmt::{self, Debug, Formatter, Write};
use std::hash::{Hash, Hasher};
-use super::{Args, Func, StyleMap, Template, Value};
+use super::{Args, Content, Func, StyleMap, Value};
use crate::diag::TypResult;
use crate::Context;
/// A class of nodes.
///
/// You can [construct] an instance of a class in Typst code by invoking the
-/// class as a callable. This always produces a template value, but not
+/// class as a callable. This always produces a content value, but not
/// necessarily a simple inline or block node. For example, the `text`
/// constructor does not actually create a [`TextNode`]. Instead it applies
-/// styling to whatever node you pass in and returns it structurally unchanged.
+/// styling to whatever content you pass in and returns it structurally
+/// unchanged.
///
/// The arguments you can pass to a class constructor fall into two categories:
/// Data that is inherent to the instance (e.g. the text/content of a heading)
/// and style properties (e.g. the fill color of a heading). As the latter are
/// often shared by many instances throughout a document, they can also be
/// conveniently configured through class's [`set`] rule. Then, they apply to
-/// all nodes that are instantiated into the template where the `set` was
+/// all nodes that are instantiated into the content block where the `set` was
/// executed.
///
/// ```typst
@@ -55,8 +56,8 @@ impl Class {
construct: |ctx, args| {
let mut styles = StyleMap::new();
T::set(args, &mut styles)?;
- let template = T::construct(ctx, args)?;
- Ok(Value::Template(template.styled_with_map(styles.scoped())))
+ let content = T::construct(ctx, args)?;
+ Ok(Value::Content(content.styled_with_map(styles.scoped())))
},
set: T::set,
}
@@ -80,8 +81,8 @@ impl Class {
/// Construct an instance of the class.
///
/// This parses both property and data arguments (in this order), styles the
- /// template constructed from the data with the style properties and wraps
- /// it in a value.
+ /// content constructed from the data with the style properties and wraps it
+ /// in a value.
pub fn construct(&self, ctx: &mut Context, mut args: Args) -> TypResult<Value> {
let value = (self.construct)(ctx, &mut args)?;
args.finish()?;
@@ -126,7 +127,7 @@ pub trait Construct {
///
/// This is passed only the arguments that remain after execution of the
/// class's set rule.
- fn construct(ctx: &mut Context, args: &mut Args) -> TypResult<Template>;
+ fn construct(ctx: &mut Context, args: &mut Args) -> TypResult<Content>;
}
/// Set style properties of a class.
diff --git a/src/eval/template.rs b/src/eval/content.rs
index 3e88b6d7..b6d60957 100644
--- a/src/eval/template.rs
+++ b/src/eval/content.rs
@@ -20,29 +20,23 @@ use crate::util::EcoString;
///
/// This results from:
/// - anything written between square brackets in Typst
-/// - any class constructor
+/// - any node constructor
///
-/// This enum has two notable variants:
+/// Content is represented as a tree of nodes. There are two nodes of special
+/// interest:
///
-/// 1. A `Styled` template attaches a style map to a template. This map affects
-/// the whole subtemplate. For example, a single bold word could be
-/// represented as a `Styled(Text("Hello"), [TextNode::STRONG: true])`
-/// template.
-///
-/// 2. A `Sequence` template combines multiple arbitrary templates and is the
-/// representation of a "flow" of content. So, when you write `[Hi] + [you]`
-/// in Typst, this type's [`Add`] implementation is invoked and the two
-/// [`Text`](Self::Text) templates are combined into a single
-/// [`Sequence`](Self::Sequence) template.
-///
-/// A sequence may contain nested sequences (meaning this variant effectively
-/// allows templates to form trees). All nested sequences can equivalently be
-/// represented as a single flat sequence, but allowing nesting doesn't hurt
-/// since we can just recurse into the nested sequences. Also, in theory,
-/// this allows better complexity when adding large sequence nodes just like
-/// for something like a text rope.
+/// 1. A `Styled` node attaches a style map to other content. For example, a
+/// single bold word could be represented as a `Styled(Text("Hello"),
+/// [TextNode::STRONG: true])` node.
+
+/// 2. A `Sequence` node content combines other arbitrary content and is the
+/// representation of a "flow" of other nodes. So, when you write `[Hi] +
+/// [you]` in Typst, this type's [`Add`] implementation is invoked and the
+/// two [`Text`](Self::Text) nodes are combined into a single
+/// [`Sequence`](Self::Sequence) node. A sequence may contain nested
+/// sequences.
#[derive(PartialEq, Clone, Hash)]
-pub enum Template {
+pub enum Content {
/// A word space.
Space,
/// A line break.
@@ -71,19 +65,19 @@ pub enum Template {
Page(PageNode),
/// A node that can be realized with styles.
Show(ShowNode),
- /// A template with attached styles.
+ /// Content with attached styles.
Styled(Arc<(Self, StyleMap)>),
- /// A sequence of multiple subtemplates.
+ /// A sequence of multiple nodes.
Sequence(Arc<Vec<Self>>),
}
-impl Template {
- /// Create an empty template.
+impl Content {
+ /// Create empty content.
pub fn new() -> Self {
Self::sequence(vec![])
}
- /// Create a template from an inline-level node.
+ /// Create content from an inline-level node.
pub fn inline<T>(node: T) -> Self
where
T: Layout + Debug + Hash + Sync + Send + 'static,
@@ -91,7 +85,7 @@ impl Template {
Self::Inline(node.pack())
}
- /// Create a template from a block-level node.
+ /// Create content from a block-level node.
pub fn block<T>(node: T) -> Self
where
T: Layout + Debug + Hash + Sync + Send + 'static,
@@ -99,7 +93,7 @@ impl Template {
Self::Block(node.pack())
}
- /// Create a template from a showable node.
+ /// Create content from a showable node.
pub fn show<T>(node: T) -> Self
where
T: Show + Debug + Hash + Sync + Send + 'static,
@@ -107,7 +101,7 @@ impl Template {
Self::Show(node.pack())
}
- /// Style this template with a single property.
+ /// Style this content with a single style property.
pub fn styled<P: Property>(mut self, key: P, value: P::Value) -> Self {
if let Self::Styled(styled) = &mut self {
if let Some((_, map)) = Arc::get_mut(styled) {
@@ -121,7 +115,7 @@ impl Template {
self.styled_with_map(StyleMap::with(key, value))
}
- /// Style this template with a full style map.
+ /// Style this content with a full style map.
pub fn styled_with_map(mut self, styles: StyleMap) -> Self {
if styles.is_empty() {
return self;
@@ -139,17 +133,17 @@ impl Template {
Self::Styled(Arc::new((self, styles)))
}
- /// Style this template in monospace.
+ /// Style this content in monospace.
pub fn monospaced(self) -> Self {
self.styled(TextNode::MONOSPACED, true)
}
- /// Underline this template.
+ /// Underline this content.
pub fn underlined(self) -> Self {
Self::show(DecoNode::<UNDERLINE>(self))
}
- /// Create a new sequence template.
+ /// Create a new sequence nodes from multiples nodes.
pub fn sequence(seq: Vec<Self>) -> Self {
if seq.len() == 1 {
seq.into_iter().next().unwrap()
@@ -158,15 +152,15 @@ impl Template {
}
}
- /// Repeat this template `n` times.
+ /// Repeat this content `n` times.
pub fn repeat(&self, n: i64) -> StrResult<Self> {
let count = usize::try_from(n)
- .map_err(|_| format!("cannot repeat this template {} times", n))?;
+ .map_err(|_| format!("cannot repeat this content {} times", n))?;
Ok(Self::sequence(vec![self.clone(); count]))
}
- /// Layout this template into a collection of pages.
+ /// Layout this content into a collection of pages.
pub fn layout(&self, ctx: &mut Context) -> TypResult<Vec<Arc<Frame>>> {
let sya = Arena::new();
let tpa = Arena::new();
@@ -190,13 +184,13 @@ impl Template {
}
}
-impl Default for Template {
+impl Default for Content {
fn default() -> Self {
Self::new()
}
}
-impl Add for Template {
+impl Add for Content {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
@@ -222,19 +216,19 @@ impl Add for Template {
}
}
-impl AddAssign for Template {
+impl AddAssign for Content {
fn add_assign(&mut self, rhs: Self) {
*self = std::mem::take(self) + rhs;
}
}
-impl Sum for Template {
+impl Sum for Content {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
Self::sequence(iter.collect())
}
}
-impl Layout for Template {
+impl Layout for Content {
fn layout(
&self,
ctx: &mut Context,
@@ -254,18 +248,18 @@ impl Layout for Template {
fn pack(self) -> LayoutNode {
match self {
- Template::Block(node) => node,
+ Content::Block(node) => node,
other => LayoutNode::new(other),
}
}
}
-/// Builds a flow or page nodes from a template.
+/// Builds a flow or page nodes from content.
struct Builder<'a> {
/// An arena where intermediate style chains are stored.
sya: &'a Arena<StyleChain<'a>>,
- /// An arena where intermediate templates are stored.
- tpa: &'a Arena<Template>,
+ /// An arena where intermediate content resulting from show rules is stored.
+ tpa: &'a Arena<Content>,
/// The already built page runs.
pages: Option<StyleVecBuilder<'a, PageNode>>,
/// The currently built list.
@@ -280,7 +274,7 @@ struct Builder<'a> {
impl<'a> Builder<'a> {
/// Prepare the builder.
- fn new(sya: &'a Arena<StyleChain<'a>>, tpa: &'a Arena<Template>, top: bool) -> Self {
+ fn new(sya: &'a Arena<StyleChain<'a>>, tpa: &'a Arena<Content>, top: bool) -> Self {
Self {
sya,
tpa,
@@ -292,33 +286,33 @@ impl<'a> Builder<'a> {
}
}
- /// Process a template.
+ /// Process content.
fn process(
&mut self,
ctx: &mut Context,
- template: &'a Template,
+ content: &'a Content,
styles: StyleChain<'a>,
) -> TypResult<()> {
if let Some(builder) = &mut self.list {
- match template {
- Template::Space => {
- builder.staged.push((template, styles));
+ match content {
+ Content::Space => {
+ builder.staged.push((content, styles));
return Ok(());
}
- Template::Parbreak => {
- builder.staged.push((template, styles));
+ Content::Parbreak => {
+ builder.staged.push((content, styles));
return Ok(());
}
- Template::List(item) if builder.kind == UNORDERED => {
+ Content::List(item) if builder.kind == UNORDERED => {
builder.wide |=
- builder.staged.iter().any(|&(t, _)| *t == Template::Parbreak);
+ builder.staged.iter().any(|&(t, _)| *t == Content::Parbreak);
builder.staged.clear();
builder.items.push(item.clone());
return Ok(());
}
- Template::Enum(item) if builder.kind == ORDERED => {
+ Content::Enum(item) if builder.kind == ORDERED => {
builder.wide |=
- builder.staged.iter().any(|&(t, _)| *t == Template::Parbreak);
+ builder.staged.iter().any(|&(t, _)| *t == Content::Parbreak);
builder.staged.clear();
builder.items.push(item.clone());
return Ok(());
@@ -327,14 +321,14 @@ impl<'a> Builder<'a> {
}
}
- match template {
- Template::Space => {
+ match content {
+ Content::Space => {
self.par.weak(ParChild::Text(' '.into()), 0, styles);
}
- Template::Linebreak => {
+ Content::Linebreak => {
self.par.destructive(ParChild::Text('\n'.into()), styles);
}
- Template::Horizontal(kind) => {
+ Content::Horizontal(kind) => {
let child = ParChild::Spacing(*kind);
if kind.is_fractional() {
self.par.destructive(child, styles);
@@ -342,21 +336,21 @@ impl<'a> Builder<'a> {
self.par.ignorant(child, styles);
}
}
- Template::Text(text) => {
+ Content::Text(text) => {
self.par.supportive(ParChild::Text(text.clone()), styles);
}
- Template::Inline(node) => {
+ Content::Inline(node) => {
self.par.supportive(ParChild::Node(node.clone()), styles);
}
- Template::Parbreak => {
+ Content::Parbreak => {
self.finish_par(styles);
self.flow.weak(FlowChild::Parbreak, 1, styles);
}
- Template::Colbreak => {
+ Content::Colbreak => {
self.finish_par(styles);
self.flow.destructive(FlowChild::Colbreak, styles);
}
- Template::Vertical(kind) => {
+ Content::Vertical(kind) => {
self.finish_par(styles);
let child = FlowChild::Spacing(*kind);
if kind.is_fractional() {
@@ -365,7 +359,7 @@ impl<'a> Builder<'a> {
self.flow.ignorant(child, styles);
}
}
- Template::Block(node) => {
+ Content::Block(node) => {
self.finish_par(styles);
let child = FlowChild::Node(node.clone());
if node.is::<PlaceNode>() {
@@ -375,7 +369,7 @@ impl<'a> Builder<'a> {
}
self.finish_par(styles);
}
- Template::List(item) => {
+ Content::List(item) => {
self.list = Some(ListBuilder {
styles,
kind: UNORDERED,
@@ -384,7 +378,7 @@ impl<'a> Builder<'a> {
staged: vec![],
});
}
- Template::Enum(item) => {
+ Content::Enum(item) => {
self.list = Some(ListBuilder {
styles,
kind: ORDERED,
@@ -393,22 +387,22 @@ impl<'a> Builder<'a> {
staged: vec![],
});
}
- Template::Pagebreak => {
+ Content::Pagebreak => {
self.finish_page(ctx, true, true, styles)?;
}
- Template::Page(page) => {
+ Content::Page(page) => {
self.finish_page(ctx, false, false, styles)?;
if let Some(pages) = &mut self.pages {
pages.push(page.clone(), styles);
}
}
- Template::Show(node) => {
+ Content::Show(node) => {
let id = node.id();
- let template = node.show(ctx, styles)?;
- let stored = self.tpa.alloc(template);
+ let content = node.show(ctx, styles)?;
+ let stored = self.tpa.alloc(content);
self.process(ctx, stored, styles.unscoped(id))?;
}
- Template::Styled(styled) => {
+ Content::Styled(styled) => {
let (sub, map) = styled.as_ref();
let stored = self.sya.alloc(styles);
let styles = map.chain(stored);
@@ -432,7 +426,7 @@ impl<'a> Builder<'a> {
None => {}
}
}
- Template::Sequence(seq) => {
+ Content::Sequence(seq) => {
for sub in seq.iter() {
self.process(ctx, sub, styles)?;
}
@@ -487,15 +481,15 @@ impl<'a> Builder<'a> {
None => return Ok(()),
};
- let template = match kind {
- UNORDERED => Template::show(ListNode::<UNORDERED> { start: 1, wide, items }),
- ORDERED | _ => Template::show(ListNode::<ORDERED> { start: 1, wide, items }),
+ let content = match kind {
+ UNORDERED => Content::show(ListNode::<UNORDERED> { start: 1, wide, items }),
+ ORDERED | _ => Content::show(ListNode::<ORDERED> { start: 1, wide, items }),
};
- let stored = self.tpa.alloc(template);
+ let stored = self.tpa.alloc(content);
self.process(ctx, stored, styles)?;
- for (template, styles) in staged {
- self.process(ctx, template, styles)?;
+ for (content, styles) in staged {
+ self.process(ctx, content, styles)?;
}
Ok(())
@@ -535,10 +529,10 @@ struct ListBuilder<'a> {
kind: ListKind,
items: Vec<ListItem>,
wide: bool,
- staged: Vec<(&'a Template, StyleChain<'a>)>,
+ staged: Vec<(&'a Content, StyleChain<'a>)>,
}
-impl Debug for Template {
+impl Debug for Content {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Space => f.pad("Space"),
diff --git a/src/eval/layout.rs b/src/eval/layout.rs
index 135e5e04..94375c61 100644
--- a/src/eval/layout.rs
+++ b/src/eval/layout.rs
@@ -19,7 +19,7 @@ use crate::Context;
/// Layout return one frame per used region alongside constraints that define
/// whether the result is reusable in other regions.
pub trait Layout {
- /// Layout the node into the given regions, producing constrained frames.
+ /// Layout this node into the given regions, producing constrained frames.
fn layout(
&self,
ctx: &mut Context,
diff --git a/src/eval/mod.rs b/src/eval/mod.rs
index 6a918dbd..79c0ad83 100644
--- a/src/eval/mod.rs
+++ b/src/eval/mod.rs
@@ -11,6 +11,7 @@ mod styles;
mod capture;
mod class;
mod collapse;
+mod content;
mod control;
mod func;
mod layout;
@@ -18,12 +19,12 @@ mod module;
mod ops;
mod scope;
mod show;
-mod template;
pub use array::*;
pub use capture::*;
pub use class::*;
pub use collapse::*;
+pub use content::*;
pub use control::*;
pub use dict::*;
pub use func::*;
@@ -32,7 +33,6 @@ pub use module::*;
pub use scope::*;
pub use show::*;
pub use styles::*;
-pub use template::*;
pub use value::*;
use unicode_segmentation::UnicodeSegmentation;
@@ -58,7 +58,7 @@ pub trait Eval {
pub type EvalResult<T> = Result<T, Control>;
impl Eval for Markup {
- type Output = Template;
+ type Output = Content;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
eval_markup(ctx, scp, &mut self.nodes())
@@ -70,7 +70,7 @@ fn eval_markup(
ctx: &mut Context,
scp: &mut Scopes,
nodes: &mut impl Iterator<Item = MarkupNode>,
-) -> EvalResult<Template> {
+) -> EvalResult<Content> {
let mut seq = Vec::with_capacity(nodes.size_hint().1.unwrap_or_default());
while let Some(node) = nodes.next() {
@@ -92,18 +92,18 @@ fn eval_markup(
});
}
- Ok(Template::sequence(seq))
+ Ok(Content::sequence(seq))
}
impl Eval for MarkupNode {
- type Output = Template;
+ type Output = Content;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
Ok(match self {
- Self::Space => Template::Space,
- Self::Linebreak => Template::Linebreak,
- Self::Parbreak => Template::Parbreak,
- Self::Text(text) => Template::Text(text.clone()),
+ Self::Space => Content::Space,
+ Self::Linebreak => Content::Linebreak,
+ Self::Parbreak => Content::Parbreak,
+ Self::Text(text) => Content::Text(text.clone()),
Self::Strong(strong) => strong.eval(ctx, scp)?,
Self::Emph(emph) => emph.eval(ctx, scp)?,
Self::Raw(raw) => raw.eval(ctx, scp)?,
@@ -117,45 +117,45 @@ impl Eval for MarkupNode {
}
impl Eval for StrongNode {
- type Output = Template;
+ type Output = Content;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
- Ok(Template::show(library::text::StrongNode(
+ Ok(Content::show(library::text::StrongNode(
self.body().eval(ctx, scp)?,
)))
}
}
impl Eval for EmphNode {
- type Output = Template;
+ type Output = Content;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
- Ok(Template::show(library::text::EmphNode(
+ Ok(Content::show(library::text::EmphNode(
self.body().eval(ctx, scp)?,
)))
}
}
impl Eval for RawNode {
- type Output = Template;
+ type Output = Content;
fn eval(&self, _: &mut Context, _: &mut Scopes) -> EvalResult<Self::Output> {
- let template = Template::show(library::text::RawNode {
+ let content = Content::show(library::text::RawNode {
text: self.text.clone(),
block: self.block,
});
Ok(match self.lang {
- Some(_) => template.styled(library::text::RawNode::LANG, self.lang.clone()),
- None => template,
+ Some(_) => content.styled(library::text::RawNode::LANG, self.lang.clone()),
+ None => content,
})
}
}
impl Eval for MathNode {
- type Output = Template;
+ type Output = Content;
fn eval(&self, _: &mut Context, _: &mut Scopes) -> EvalResult<Self::Output> {
- Ok(Template::show(library::math::MathNode {
+ Ok(Content::show(library::math::MathNode {
formula: self.formula.clone(),
display: self.display,
}))
@@ -163,10 +163,10 @@ impl Eval for MathNode {
}
impl Eval for HeadingNode {
- type Output = Template;
+ type Output = Content;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
- Ok(Template::show(library::structure::HeadingNode {
+ Ok(Content::show(library::structure::HeadingNode {
body: self.body().eval(ctx, scp)?,
level: self.level(),
}))
@@ -174,10 +174,10 @@ impl Eval for HeadingNode {
}
impl Eval for ListNode {
- type Output = Template;
+ type Output = Content;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
- Ok(Template::List(library::structure::ListItem {
+ Ok(Content::List(library::structure::ListItem {
number: None,
body: Box::new(self.body().eval(ctx, scp)?),
}))
@@ -185,10 +185,10 @@ impl Eval for ListNode {
}
impl Eval for EnumNode {
- type Output = Template;
+ type Output = Content;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
- Ok(Template::Enum(library::structure::ListItem {
+ Ok(Content::Enum(library::structure::ListItem {
number: self.number(),
body: Box::new(self.body().eval(ctx, scp)?),
}))
@@ -203,7 +203,7 @@ impl Eval for Expr {
Self::Lit(v) => v.eval(ctx, scp),
Self::Ident(v) => v.eval(ctx, scp),
Self::Code(v) => v.eval(ctx, scp),
- Self::Template(v) => v.eval(ctx, scp).map(Value::Template),
+ Self::Content(v) => v.eval(ctx, scp).map(Value::Content),
Self::Array(v) => v.eval(ctx, scp).map(Value::Array),
Self::Dict(v) => v.eval(ctx, scp).map(Value::Dict),
Self::Group(v) => v.eval(ctx, scp),
@@ -222,7 +222,7 @@ impl Eval for Expr {
Self::While(v) => v.eval(ctx, scp),
Self::For(v) => v.eval(ctx, scp),
Self::Import(v) => v.eval(ctx, scp),
- Self::Include(v) => v.eval(ctx, scp).map(Value::Template),
+ Self::Include(v) => v.eval(ctx, scp).map(Value::Content),
Self::Break(v) => v.eval(ctx, scp),
Self::Continue(v) => v.eval(ctx, scp),
Self::Return(v) => v.eval(ctx, scp),
@@ -276,14 +276,14 @@ impl Eval for CodeBlock {
}
}
-impl Eval for TemplateBlock {
- type Output = Template;
+impl Eval for ContentBlock {
+ type Output = Content;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
scp.enter();
- let template = self.body().eval(ctx, scp)?;
+ let content = self.body().eval(ctx, scp)?;
scp.exit();
- Ok(template)
+ Ok(content)
}
}
@@ -716,13 +716,13 @@ impl Eval for ImportExpr {
}
impl Eval for IncludeExpr {
- type Output = Template;
+ type Output = Content;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
let span = self.path().span();
let path = self.path().eval(ctx, scp)?.cast::<EcoString>().at(span)?;
let module = import(ctx, &path, span)?;
- Ok(module.template.clone())
+ Ok(module.content.clone())
}
}
diff --git a/src/eval/module.rs b/src/eval/module.rs
index 478c76b7..4460caec 100644
--- a/src/eval/module.rs
+++ b/src/eval/module.rs
@@ -1,4 +1,4 @@
-use super::{Scope, Template};
+use super::{Content, Scope};
use crate::source::{SourceId, SourceStore};
/// An evaluated module, ready for importing or layouting.
@@ -7,7 +7,7 @@ pub struct Module {
/// The top-level definitions that were bound in this module.
pub scope: Scope,
/// The module's layoutable contents.
- pub template: Template,
+ pub content: Content,
/// The source file revisions this module depends on.
pub deps: Vec<(SourceId, usize)>,
}
diff --git a/src/eval/ops.rs b/src/eval/ops.rs
index acceb626..04a13fd1 100644
--- a/src/eval/ops.rs
+++ b/src/eval/ops.rs
@@ -19,11 +19,11 @@ pub fn join(lhs: Value, rhs: Value) -> StrResult<Value> {
(a, None) => a,
(None, b) => b,
(Str(a), Str(b)) => Str(a + b),
+ (Str(a), Content(b)) => Content(super::Content::Text(a) + b),
+ (Content(a), Str(b)) => Content(a + super::Content::Text(b)),
+ (Content(a), Content(b)) => Content(a + b),
(Array(a), Array(b)) => Array(a + b),
(Dict(a), Dict(b)) => Dict(a + b),
- (Template(a), Template(b)) => Template(a + b),
- (Template(a), Str(b)) => Template(a + super::Template::Text(b)),
- (Str(a), Template(b)) => Template(super::Template::Text(a) + b),
(a, b) => mismatch!("cannot join {} with {}", a, b),
})
}
@@ -81,15 +81,16 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> {
(Fractional(a), Fractional(b)) => Fractional(a + b),
(Str(a), Str(b)) => Str(a + b),
+
+ (Content(a), None) => Content(a),
+ (None, Content(b)) => Content(b),
+ (Content(a), Content(b)) => Content(a + b),
+ (Content(a), Str(b)) => Content(a + super::Content::Text(b)),
+ (Str(a), Content(b)) => Content(super::Content::Text(a) + b),
+
(Array(a), Array(b)) => Array(a + b),
(Dict(a), Dict(b)) => Dict(a + b),
- (Template(a), None) => Template(a),
- (None, Template(b)) => Template(b),
- (Template(a), Template(b)) => Template(a + b),
- (Template(a), Str(b)) => Template(a + super::Template::Text(b)),
- (Str(a), Template(b)) => Template(super::Template::Text(a) + b),
-
(a, b) => {
if let (Dyn(a), Dyn(b)) = (&a, &b) {
// 1D alignments can be summed into 2D alignments.
@@ -177,8 +178,8 @@ pub fn mul(lhs: Value, rhs: Value) -> StrResult<Value> {
(Int(a), Str(b)) => Str(repeat_str(b, a)?),
(Array(a), Int(b)) => Array(a.repeat(b)?),
(Int(a), Array(b)) => Array(b.repeat(a)?),
- (Template(a), Int(b)) => Template(a.repeat(b)?),
- (Int(a), Template(b)) => Template(b.repeat(a)?),
+ (Content(a), Int(b)) => Content(a.repeat(b)?),
+ (Int(a), Content(b)) => Content(b.repeat(a)?),
(a, b) => mismatch!("cannot multiply {} with {}", a, b),
})
@@ -293,9 +294,9 @@ pub fn equal(lhs: &Value, rhs: &Value) -> bool {
(Fractional(a), Fractional(b)) => a == b,
(Color(a), Color(b)) => a == b,
(Str(a), Str(b)) => a == b,
+ (Content(a), Content(b)) => a == b,
(Array(a), Array(b)) => a == b,
(Dict(a), Dict(b)) => a == b,
- (Template(a), Template(b)) => a == b,
(Func(a), Func(b)) => a == b,
(Dyn(a), Dyn(b)) => a == b,
diff --git a/src/eval/show.rs b/src/eval/show.rs
index b0fb8172..e85903d2 100644
--- a/src/eval/show.rs
+++ b/src/eval/show.rs
@@ -3,15 +3,15 @@ use std::fmt::{self, Debug, Formatter};
use std::hash::Hash;
use std::sync::Arc;
-use super::{StyleChain, Template};
+use super::{Content, StyleChain};
use crate::diag::TypResult;
use crate::util::Prehashed;
use crate::Context;
/// A node that can be realized given some styles.
pub trait Show {
- /// Realize the template in the given styles.
- fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template>;
+ /// Realize this node in the given styles.
+ fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content>;
/// Convert to a packed show node.
fn pack(self) -> ShowNode
@@ -42,7 +42,7 @@ impl ShowNode {
}
impl Show for ShowNode {
- fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> {
+ fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> {
self.0.show(ctx, styles)
}
diff --git a/src/eval/styles.rs b/src/eval/styles.rs
index 5a8371a9..20c36ae2 100644
--- a/src/eval/styles.rs
+++ b/src/eval/styles.rs
@@ -3,7 +3,7 @@ use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::sync::Arc;
-use super::{Args, Func, Span, Template, Value};
+use super::{Args, Content, Func, Span, Value};
use crate::diag::{At, TypResult};
use crate::library::layout::PageNode;
use crate::library::text::ParNode;
@@ -414,7 +414,7 @@ impl<'a> StyleChain<'a> {
node: &dyn Any,
ctx: &mut Context,
values: impl IntoIterator<Item = Value>,
- ) -> TypResult<Option<Template>> {
+ ) -> TypResult<Option<Content>> {
Ok(if let Some(recipe) = self.recipes(node.type_id()).next() {
let args = Args::from_values(recipe.span, values);
Some(recipe.func.call(ctx, args)?.cast().at(recipe.span)?)
diff --git a/src/eval/value.rs b/src/eval/value.rs
index 7d41bff5..48b2139f 100644
--- a/src/eval/value.rs
+++ b/src/eval/value.rs
@@ -4,7 +4,7 @@ use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::sync::Arc;
-use super::{ops, Args, Array, Class, Dict, Func, Layout, Template};
+use super::{ops, Args, Array, Class, Content, Dict, Func, Layout};
use crate::diag::{with_alternative, StrResult};
use crate::geom::{Angle, Color, Fractional, Length, Linear, Relative, RgbaColor};
use crate::syntax::Spanned;
@@ -37,12 +37,12 @@ pub enum Value {
Color(Color),
/// A string: `"string"`.
Str(EcoString),
+ /// A content value: `[*Hi* there]`.
+ Content(Content),
/// An array of values: `(1, "hi", 12cm)`.
Array(Array),
/// A dictionary value: `(color: #f79143, pattern: dashed)`.
Dict(Dict),
- /// A template value: `[*Hi* there]`.
- Template(Template),
/// An executable function.
Func(Func),
/// Captured arguments to a function.
@@ -54,20 +54,20 @@ pub enum Value {
}
impl Value {
- /// Create a template value from an inline-level node.
+ /// Create a content value from an inline-level node.
pub fn inline<T>(node: T) -> Self
where
T: Layout + Debug + Hash + Sync + Send + 'static,
{
- Self::Template(Template::inline(node))
+ Self::Content(Content::inline(node))
}
- /// Create a template value from a block-level node.
+ /// Create a content value from a block-level node.
pub fn block<T>(node: T) -> Self
where
T: Layout + Debug + Hash + Sync + Send + 'static,
{
- Self::Template(Template::block(node))
+ Self::Content(Content::block(node))
}
/// The name of the stored value's type.
@@ -85,9 +85,9 @@ impl Value {
Self::Fractional(_) => Fractional::TYPE_NAME,
Self::Color(_) => Color::TYPE_NAME,
Self::Str(_) => EcoString::TYPE_NAME,
+ Self::Content(_) => Content::TYPE_NAME,
Self::Array(_) => Array::TYPE_NAME,
Self::Dict(_) => Dict::TYPE_NAME,
- Self::Template(_) => Template::TYPE_NAME,
Self::Func(_) => Func::TYPE_NAME,
Self::Args(_) => Args::TYPE_NAME,
Self::Class(_) => Class::TYPE_NAME,
@@ -111,16 +111,16 @@ impl Value {
}
/// Return the display representation of the value.
- pub fn display(self) -> Template {
+ pub fn display(self) -> Content {
match self {
- Value::None => Template::new(),
- Value::Int(v) => Template::Text(format_eco!("{}", v)),
- Value::Float(v) => Template::Text(format_eco!("{}", v)),
- Value::Str(v) => Template::Text(v),
- Value::Template(v) => v,
+ Value::None => Content::new(),
+ Value::Int(v) => Content::Text(format_eco!("{}", v)),
+ Value::Float(v) => Content::Text(format_eco!("{}", v)),
+ Value::Str(v) => Content::Text(v),
+ Value::Content(v) => v,
// For values which can't be shown "naturally", we print the
// representation in monospace.
- v => Template::Text(v.repr()).monospaced(),
+ v => Content::Text(v.repr()).monospaced(),
}
}
}
@@ -146,9 +146,9 @@ impl Debug for Value {
Self::Fractional(v) => Debug::fmt(v, f),
Self::Color(v) => Debug::fmt(v, f),
Self::Str(v) => Debug::fmt(v, f),
+ Self::Content(_) => f.pad("<content>"),
Self::Array(v) => Debug::fmt(v, f),
Self::Dict(v) => Debug::fmt(v, f),
- Self::Template(_) => f.pad("<template>"),
Self::Func(v) => Debug::fmt(v, f),
Self::Args(v) => Debug::fmt(v, f),
Self::Class(v) => Debug::fmt(v, f),
@@ -185,9 +185,9 @@ impl Hash for Value {
Self::Fractional(v) => v.hash(state),
Self::Color(v) => v.hash(state),
Self::Str(v) => v.hash(state),
+ Self::Content(v) => v.hash(state),
Self::Array(v) => v.hash(state),
Self::Dict(v) => v.hash(state),
- Self::Template(v) => v.hash(state),
Self::Func(v) => v.hash(state),
Self::Args(v) => v.hash(state),
Self::Class(v) => v.hash(state),
@@ -441,9 +441,9 @@ primitive! { Linear: "relative length", Linear, Length(v) => v.into(), Relative(
primitive! { Fractional: "fractional length", Fractional }
primitive! { Color: "color", Color }
primitive! { EcoString: "string", Str }
+primitive! { Content: "content", Content, None => Content::new() }
primitive! { Array: "array", Array }
primitive! { Dict: "dictionary", Dict }
-primitive! { Template: "template", Template, None => Template::new() }
primitive! { Func: "function", Func, Class(v) => v.constructor() }
primitive! { Args: "arguments", Args }
primitive! { Class: "class", Class }