summaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-02-02 16:02:23 +0100
committerLaurenz <laurmaedje@gmail.com>2022-02-02 16:02:23 +0100
commita7b403fd742941f6b163f06876aec96729db707f (patch)
treeb5062eddbcb814713b65f59d3f59dfe7e2bcaca8 /src/eval
parent0a1916c1e4259aff1306b26c06d4edcf0f190a3b (diff)
Rename `Node` to `Template`
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/class.rs20
-rw-r--r--src/eval/mod.rs94
-rw-r--r--src/eval/ops.rs22
-rw-r--r--src/eval/styles.rs32
-rw-r--r--src/eval/template.rs (renamed from src/eval/node.rs)109
-rw-r--r--src/eval/value.rs34
6 files changed, 154 insertions, 157 deletions
diff --git a/src/eval/class.rs b/src/eval/class.rs
index 91a9daef..4307eecb 100644
--- a/src/eval/class.rs
+++ b/src/eval/class.rs
@@ -1,15 +1,15 @@
use std::fmt::{self, Debug, Formatter, Write};
-use super::{Args, EvalContext, Func, Node, StyleMap, Value};
+use super::{Args, EvalContext, Func, StyleMap, Template, Value};
use crate::diag::TypResult;
-/// A class of [nodes](Node).
+/// 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 some node, but not necessarily one
-/// of fixed type. 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.
+/// class as a callable. This always produces a template, 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.
///
/// The arguments you can pass to a class constructor fall into two categories:
/// Data that is inherent to the instance (e.g. the text of a heading) and style
@@ -50,8 +50,8 @@ impl Class {
construct: |ctx, args| {
let mut styles = StyleMap::new();
T::set(args, &mut styles)?;
- let node = T::construct(ctx, args)?;
- Ok(Value::Node(node.styled_with_map(styles.scoped())))
+ let template = T::construct(ctx, args)?;
+ Ok(Value::Template(template.styled_with_map(styles.scoped())))
},
set: T::set,
}
@@ -65,7 +65,7 @@ impl Class {
/// Construct an instance of the class.
///
/// This parses both property and data arguments (in this order) and styles
- /// the node constructed from the data with the style properties.
+ /// the template constructed from the data with the style properties.
pub fn construct(&self, ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
(self.construct)(ctx, args)
}
@@ -104,7 +104,7 @@ pub trait Construct {
///
/// This is passed only the arguments that remain after execution of the
/// class's set rule.
- fn construct(ctx: &mut EvalContext, args: &mut Args) -> TypResult<Node>;
+ fn construct(ctx: &mut EvalContext, args: &mut Args) -> TypResult<Template>;
}
/// Set style properties of a class.
diff --git a/src/eval/mod.rs b/src/eval/mod.rs
index ebbeec3d..fce8bed4 100644
--- a/src/eval/mod.rs
+++ b/src/eval/mod.rs
@@ -11,18 +11,18 @@ mod styles;
mod capture;
mod class;
mod func;
-mod node;
mod ops;
mod scope;
+mod template;
pub use array::*;
pub use capture::*;
pub use class::*;
pub use dict::*;
pub use func::*;
-pub use node::*;
pub use scope::*;
pub use styles::*;
+pub use template::*;
pub use value::*;
use std::cell::RefMut;
@@ -64,13 +64,13 @@ pub struct Module {
/// The top-level definitions that were bound in this module.
pub scope: Scope,
/// The module's layoutable contents.
- pub node: Node,
+ pub template: Template,
}
impl Module {
- /// Convert this module's node into a layout tree.
+ /// Convert this module's template into a layout tree.
pub fn into_root(self) -> RootNode {
- self.node.into_root()
+ self.template.into_root()
}
}
@@ -143,14 +143,14 @@ impl<'a> EvalContext<'a> {
self.route.push(id);
// Evaluate the module.
- let node = ast.eval(self).trace(|| Tracepoint::Import, span)?;
+ let template = ast.eval(self).trace(|| Tracepoint::Import, span)?;
// Restore the old context.
let new_scopes = mem::replace(&mut self.scopes, prev_scopes);
self.route.pop().unwrap();
// Save the evaluated module.
- let module = Module { scope: new_scopes.top, node };
+ let module = Module { scope: new_scopes.top, template };
self.modules.insert(id, module);
Ok(id)
@@ -170,7 +170,7 @@ impl<'a> EvalContext<'a> {
}
impl Eval for Markup {
- type Output = Node;
+ type Output = Template;
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
eval_markup(ctx, &mut self.nodes())
@@ -181,12 +181,12 @@ impl Eval for Markup {
fn eval_markup(
ctx: &mut EvalContext,
nodes: &mut impl Iterator<Item = MarkupNode>,
-) -> TypResult<Node> {
+) -> TypResult<Template> {
let mut seq = Vec::with_capacity(nodes.size_hint().1.unwrap_or_default());
let mut styles = StyleMap::new();
while let Some(node) = nodes.next() {
- let result = match node {
+ let template = match node {
MarkupNode::Expr(Expr::Set(set)) => {
let class = set.class();
let class = class.eval(ctx)?.cast::<Class>().at(class.span())?;
@@ -206,21 +206,21 @@ fn eval_markup(
_ => node.eval(ctx)?,
};
- seq.push(Styled::new(result, styles.clone()));
+ seq.push(Styled::new(template, styles.clone()));
}
- Ok(Node::Sequence(seq))
+ Ok(Template::Sequence(seq))
}
impl Eval for MarkupNode {
- type Output = Node;
+ type Output = Template;
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
Ok(match self {
- Self::Space => Node::Space,
- Self::Linebreak => Node::Linebreak,
- Self::Parbreak => Node::Parbreak,
- Self::Text(text) => Node::Text(text.clone()),
+ Self::Space => Template::Space,
+ Self::Linebreak => Template::Linebreak,
+ Self::Parbreak => Template::Parbreak,
+ Self::Text(text) => Template::Text(text.clone()),
Self::Strong(strong) => strong.eval(ctx)?,
Self::Emph(emph) => emph.eval(ctx)?,
Self::Raw(raw) => raw.eval(ctx)?,
@@ -234,7 +234,7 @@ impl Eval for MarkupNode {
}
impl Eval for StrongNode {
- type Output = Node;
+ type Output = Template;
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
Ok(self.body().eval(ctx)?.styled(TextNode::STRONG, true))
@@ -242,7 +242,7 @@ impl Eval for StrongNode {
}
impl Eval for EmphNode {
- type Output = Node;
+ type Output = Template;
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
Ok(self.body().eval(ctx)?.styled(TextNode::EMPH, true))
@@ -250,12 +250,12 @@ impl Eval for EmphNode {
}
impl Eval for RawNode {
- type Output = Node;
+ type Output = Template;
fn eval(&self, _: &mut EvalContext) -> TypResult<Self::Output> {
let code = self.highlighted();
Ok(if self.block {
- Node::Block(code.into_block())
+ Template::Block(code.into_block())
} else {
code
})
@@ -263,9 +263,9 @@ impl Eval for RawNode {
}
impl RawNode {
- /// Styled node for a code block, with optional syntax highlighting.
- pub fn highlighted(&self) -> Node {
- let mut sequence: Vec<Styled<Node>> = vec![];
+ /// Styled template for a code block, with optional syntax highlighting.
+ pub fn highlighted(&self) -> Template {
+ let mut seq: Vec<Styled<Template>> = vec![];
let syntax = if let Some(syntax) = self
.lang
@@ -279,7 +279,7 @@ impl RawNode {
) {
None
} else {
- return Node::Text(self.text.clone()).monospaced();
+ return Template::Text(self.text.clone()).monospaced();
};
let foreground = THEME
@@ -294,11 +294,11 @@ impl RawNode {
let mut highlighter = HighlightLines::new(syntax, &THEME);
for (i, line) in self.text.lines().enumerate() {
if i != 0 {
- sequence.push(Styled::bare(Node::Linebreak));
+ seq.push(Styled::bare(Template::Linebreak));
}
for (style, piece) in highlighter.highlight(line, &SYNTAXES) {
- sequence.push(style_piece(piece, foreground, style));
+ seq.push(style_piece(piece, foreground, style));
}
}
}
@@ -311,23 +311,21 @@ impl RawNode {
red.as_ref(),
&highlighter,
&mut |range, style| {
- sequence.push(style_piece(&self.text[range], foreground, style));
+ seq.push(style_piece(&self.text[range], foreground, style));
},
)
}
}
- Node::Sequence(sequence).monospaced()
+ Template::Sequence(seq).monospaced()
}
}
/// Style a piece of text with a syntect style.
-fn style_piece(piece: &str, foreground: Paint, style: SynStyle) -> Styled<Node> {
- let paint = style.foreground.into();
- let node = Node::Text(piece.into());
-
+fn style_piece(piece: &str, foreground: Paint, style: SynStyle) -> Styled<Template> {
let mut styles = StyleMap::new();
+ let paint = style.foreground.into();
if paint != foreground {
styles.set(TextNode::FILL, paint);
}
@@ -344,16 +342,16 @@ fn style_piece(piece: &str, foreground: Paint, style: SynStyle) -> Styled<Node>
styles.set(TextNode::LINES, vec![DecoLine::Underline.into()]);
}
- Styled::new(node, styles)
+ Styled::new(Template::Text(piece.into()), styles)
}
impl Eval for MathNode {
- type Output = Node;
+ type Output = Template;
fn eval(&self, _: &mut EvalContext) -> TypResult<Self::Output> {
- let text = Node::Text(self.formula.trim().into()).monospaced();
+ let text = Template::Text(self.formula.trim().into()).monospaced();
Ok(if self.display {
- Node::Block(text.into_block())
+ Template::Block(text.into_block())
} else {
text
})
@@ -361,10 +359,10 @@ impl Eval for MathNode {
}
impl Eval for HeadingNode {
- type Output = Node;
+ type Output = Template;
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
- Ok(Node::block(library::HeadingNode {
+ Ok(Template::block(library::HeadingNode {
child: self.body().eval(ctx)?.into_block(),
level: self.level(),
}))
@@ -372,10 +370,10 @@ impl Eval for HeadingNode {
}
impl Eval for ListNode {
- type Output = Node;
+ type Output = Template;
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
- Ok(Node::block(library::ListNode {
+ Ok(Template::block(library::ListNode {
child: self.body().eval(ctx)?.into_block(),
kind: library::Unordered,
}))
@@ -383,10 +381,10 @@ impl Eval for ListNode {
}
impl Eval for EnumNode {
- type Output = Node;
+ type Output = Template;
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
- Ok(Node::block(library::ListNode {
+ Ok(Template::block(library::ListNode {
child: self.body().eval(ctx)?.into_block(),
kind: library::Ordered(self.number()),
}))
@@ -402,7 +400,7 @@ impl Eval for Expr {
Self::Ident(v) => v.eval(ctx),
Self::Array(v) => v.eval(ctx).map(Value::Array),
Self::Dict(v) => v.eval(ctx).map(Value::Dict),
- Self::Template(v) => v.eval(ctx).map(Value::Node),
+ Self::Template(v) => v.eval(ctx).map(Value::Template),
Self::Group(v) => v.eval(ctx),
Self::Block(v) => v.eval(ctx),
Self::Call(v) => v.eval(ctx),
@@ -475,13 +473,13 @@ impl Eval for DictExpr {
}
impl Eval for TemplateExpr {
- type Output = Node;
+ type Output = Template;
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
ctx.scopes.enter();
- let node = self.body().eval(ctx)?;
+ let template = self.body().eval(ctx)?;
ctx.scopes.exit();
- Ok(node)
+ Ok(template)
}
}
@@ -899,7 +897,7 @@ impl Eval for IncludeExpr {
let resolved = path.eval(ctx)?.cast::<EcoString>().at(path.span())?;
let file = ctx.import(&resolved, path.span())?;
let module = &ctx.modules[&file];
- Ok(Value::Node(module.node.clone()))
+ Ok(Value::Template(module.template.clone()))
}
}
diff --git a/src/eval/ops.rs b/src/eval/ops.rs
index 649b8467..b5fa9f9a 100644
--- a/src/eval/ops.rs
+++ b/src/eval/ops.rs
@@ -22,9 +22,9 @@ pub fn join(lhs: Value, rhs: Value) -> StrResult<Value> {
(Str(a), Str(b)) => Str(a + b),
(Array(a), Array(b)) => Array(a + b),
(Dict(a), Dict(b)) => Dict(a + b),
- (Node(a), Node(b)) => Node(a + b),
- (Node(a), Str(b)) => Node(a + super::Node::Text(b)),
- (Str(a), Node(b)) => Node(super::Node::Text(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),
})
}
@@ -85,11 +85,11 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> {
(Array(a), Array(b)) => Array(a + b),
(Dict(a), Dict(b)) => Dict(a + b),
- (Node(a), None) => Node(a),
- (None, Node(b)) => Node(b),
- (Node(a), Node(b)) => Node(a + b),
- (Node(a), Str(b)) => Node(a + super::Node::Text(b)),
- (Str(a), Node(b)) => Node(super::Node::Text(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) {
@@ -178,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)?),
- (Node(a), Int(b)) => Node(a.repeat(b)?),
- (Int(a), Node(b)) => Node(b.repeat(a)?),
+ (Template(a), Int(b)) => Template(a.repeat(b)?),
+ (Int(a), Template(b)) => Template(b.repeat(a)?),
(a, b) => mismatch!("cannot multiply {} with {}", a, b),
})
@@ -296,7 +296,7 @@ pub fn equal(lhs: &Value, rhs: &Value) -> bool {
(Str(a), Str(b)) => a == b,
(Array(a), Array(b)) => a == b,
(Dict(a), Dict(b)) => a == b,
- (Node(a), Node(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/styles.rs b/src/eval/styles.rs
index 5276353a..e3dc51bb 100644
--- a/src/eval/styles.rs
+++ b/src/eval/styles.rs
@@ -144,7 +144,7 @@ impl StyleMap {
}
/// Whether two style maps are equal when filtered down to properties of the
- /// node `T`.
+ /// class `T`.
pub fn compatible<T: 'static>(&self, other: &Self) -> bool {
let f = |entry: &&Entry| entry.is_of::<T>();
self.0.iter().filter(f).count() == other.0.iter().filter(f).count()
@@ -242,10 +242,10 @@ impl<'a> StyleChain<'a> {
/// Barriers interact with [scoped](StyleMap::scoped) styles: A scoped style
/// can still be read through a single barrier (the one of the node it
/// _should_ apply to), but a second barrier will make it invisible.
- pub fn barred<'b>(&'b self, node: TypeId) -> StyleChain<'b> {
- if self.needs_barrier(node) {
+ pub fn barred<'b>(&'b self, class: TypeId) -> StyleChain<'b> {
+ if self.needs_barrier(class) {
StyleChain {
- first: Link::Barrier(node),
+ first: Link::Barrier(class),
outer: Some(self),
}
} else {
@@ -297,18 +297,18 @@ impl<'a> StyleChain<'a> {
.and_then(|entry| entry.downcast::<P>()),
depth,
),
- Link::Barrier(node) => (None, depth + (P::node_id() == node) as usize),
+ Link::Barrier(class) => (None, depth + (P::class_id() == class) as usize),
}
}
- fn needs_barrier(self, node: TypeId) -> bool {
+ fn needs_barrier(self, class: TypeId) -> bool {
if let Link::Map(map) = self.first {
- if map.0.iter().any(|entry| entry.is_of_same(node)) {
+ if map.0.iter().any(|entry| entry.is_of_same(class)) {
return true;
}
}
- self.outer.map_or(false, |outer| outer.needs_barrier(node))
+ self.outer.map_or(false, |outer| outer.needs_barrier(class))
}
}
@@ -352,11 +352,11 @@ impl Entry {
}
fn is_of<T: 'static>(&self) -> bool {
- self.p.node_id() == TypeId::of::<T>()
+ self.p.class_id() == TypeId::of::<T>()
}
- fn is_of_same(&self, node: TypeId) -> bool {
- self.p.node_id() == node
+ fn is_of_same(&self, class: TypeId) -> bool {
+ self.p.class_id() == class
}
fn downcast<P: Property>(&self) -> Option<&P::Value> {
@@ -402,8 +402,8 @@ pub trait Property: Copy + Sync + Send + 'static {
/// Whether the property needs folding.
const FOLDABLE: bool = false;
- /// The type id of the node this property belongs to.
- fn node_id() -> TypeId;
+ /// The type id of the class this property belongs to.
+ fn class_id() -> TypeId;
/// The default value of the property.
fn default() -> Self::Value;
@@ -437,7 +437,7 @@ trait Bounds: Sync + Send + 'static {
fn dyn_fmt(&self, f: &mut Formatter) -> fmt::Result;
fn dyn_eq(&self, other: &Entry) -> bool;
fn hash64(&self) -> u64;
- fn node_id(&self) -> TypeId;
+ fn class_id(&self) -> TypeId;
fn style_id(&self) -> TypeId;
fn fold(&self, outer: &Entry) -> Entry;
}
@@ -467,8 +467,8 @@ impl<P: Property> Bounds for (P, P::Value) {
state.finish()
}
- fn node_id(&self) -> TypeId {
- P::node_id()
+ fn class_id(&self) -> TypeId {
+ P::class_id()
}
fn style_id(&self) -> TypeId {
diff --git a/src/eval/node.rs b/src/eval/template.rs
index e6985faa..8a49f84d 100644
--- a/src/eval/node.rs
+++ b/src/eval/template.rs
@@ -14,17 +14,17 @@ use crate::library::{
};
use crate::util::EcoString;
-/// A partial representation of a layout node.
+/// Composable representation of styled content.
///
-/// A node is a composable intermediate representation that can be converted
-/// into a proper layout node by lifting it to a [block-level](PackedNode) or
-/// [root node](RootNode).
+/// This results from:
+/// - anything written between square brackets in Typst
+/// - any class constructor
///
/// When you write `[Hi] + [you]` in Typst, this type's [`Add`] implementation
-/// is invoked. There, multiple nodes are combined into a single
-/// [`Sequence`](Self::Sequence) node.
+/// is invoked. There, multiple templates are combined into a single
+/// [`Sequence`](Self::Sequence) template.
#[derive(Debug, PartialEq, Clone, Hash)]
-pub enum Node {
+pub enum Template {
/// A word space.
Space,
/// A line break.
@@ -62,13 +62,13 @@ pub enum Node {
Sequence(Vec<Styled<Self>>),
}
-impl Node {
- /// Create an empty node.
+impl Template {
+ /// Create an empty template.
pub fn new() -> Self {
Self::Sequence(vec![])
}
- /// Create an inline-level node.
+ /// Create a template from an inline-level node.
pub fn inline<T>(node: T) -> Self
where
T: Layout + Debug + Hash + Sync + Send + 'static,
@@ -76,7 +76,7 @@ impl Node {
Self::Inline(node.pack())
}
- /// Create a block-level node.
+ /// Create a template from a block-level node.
pub fn block<T>(node: T) -> Self
where
T: Layout + Debug + Hash + Sync + Send + 'static,
@@ -84,7 +84,7 @@ impl Node {
Self::Block(node.pack())
}
- /// Style this node with a single property.
+ /// Style this template with a single property.
pub fn styled<P: Property>(mut self, key: P, value: P::Value) -> Self {
if let Self::Sequence(vec) = &mut self {
if let [styled] = vec.as_mut_slice() {
@@ -96,7 +96,7 @@ impl Node {
self.styled_with_map(StyleMap::with(key, value))
}
- /// Style this node with a full style map.
+ /// Style this template with a full style map.
pub fn styled_with_map(mut self, styles: StyleMap) -> Self {
if styles.is_empty() {
return self;
@@ -112,14 +112,14 @@ impl Node {
Self::Sequence(vec![Styled::new(self, styles)])
}
- /// Style this node in monospace.
+ /// Style this template in monospace.
pub fn monospaced(self) -> Self {
self.styled(TextNode::MONOSPACE, true)
}
- /// Lift to a type-erased block-level node.
+ /// Lift to a type-erased block-level template.
pub fn into_block(self) -> PackedNode {
- if let Node::Block(packed) = self {
+ if let Template::Block(packed) = self {
packed
} else {
let mut packer = Packer::new(false);
@@ -135,23 +135,22 @@ impl Node {
packer.into_root()
}
- /// Repeat this node `n` times.
+ /// Repeat this template `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))?;
- // TODO(style): Make more efficient.
Ok(Self::Sequence(vec![Styled::bare(self.clone()); count]))
}
}
-impl Default for Node {
+impl Default for Template {
fn default() -> Self {
Self::new()
}
}
-impl Add for Node {
+impl Add for Template {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
@@ -175,19 +174,19 @@ impl Add for Node {
}
}
-impl AddAssign for Node {
+impl AddAssign for Template {
fn add_assign(&mut self, rhs: Self) {
*self = mem::take(self) + rhs;
}
}
-impl Sum for Node {
+impl Sum for Template {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
Self::Sequence(iter.map(|n| Styled::bare(n)).collect())
}
}
-/// Packs a [`Node`] into a flow or root node.
+/// Packs a [`Template`] into a flow or root node.
struct Packer {
/// Whether this packer produces a root node.
top: bool,
@@ -200,7 +199,7 @@ struct Packer {
}
impl Packer {
- /// Start a new node-packing session.
+ /// Start a new template-packing session.
fn new(top: bool) -> Self {
Self {
top,
@@ -222,28 +221,28 @@ impl Packer {
RootNode(self.pages)
}
- /// Consider a node with the given styles.
- fn walk(&mut self, node: Node, styles: StyleMap) {
- match node {
- Node::Space => {
+ /// Consider a template with the given styles.
+ fn walk(&mut self, template: Template, styles: StyleMap) {
+ match template {
+ Template::Space => {
// A text space is "soft", meaning that it can be eaten up by
// adjacent line breaks or explicit spacings.
self.par.last.soft(Styled::new(ParChild::text(' '), styles), false);
}
- Node::Linebreak => {
+ Template::Linebreak => {
// A line break eats up surrounding text spaces.
self.par.last.hard();
self.push_inline(Styled::new(ParChild::text('\n'), styles));
self.par.last.hard();
}
- Node::Parbreak => {
+ Template::Parbreak => {
// An explicit paragraph break is styled according to the active
// styles (`Some(_)`) whereas paragraph breaks forced by
// incompatibility take their styles from the preceding
// paragraph.
self.parbreak(Some(styles), true);
}
- Node::Colbreak => {
+ Template::Colbreak => {
// Explicit column breaks end the current paragraph and then
// discards the paragraph break.
self.parbreak(None, false);
@@ -251,24 +250,24 @@ impl Packer {
self.flow.children.push(Styled::new(FlowChild::Skip, styles));
self.flow.last.hard();
}
- Node::Pagebreak => {
+ Template::Pagebreak => {
// We must set the flow styles after the page break such that an
// empty page created by two page breaks in a row has styles at
// all.
self.pagebreak();
self.flow.styles = styles;
}
- Node::Text(text) => {
+ Template::Text(text) => {
self.push_inline(Styled::new(ParChild::text(text), styles));
}
- Node::Spacing(SpecAxis::Horizontal, kind) => {
+ Template::Spacing(SpecAxis::Horizontal, kind) => {
// Just like a line break, explicit horizontal spacing eats up
// surrounding text spaces.
self.par.last.hard();
self.push_inline(Styled::new(ParChild::Spacing(kind), styles));
self.par.last.hard();
}
- Node::Spacing(SpecAxis::Vertical, kind) => {
+ Template::Spacing(SpecAxis::Vertical, kind) => {
// Explicit vertical spacing ends the current paragraph and then
// discards the paragraph break.
self.parbreak(None, false);
@@ -276,13 +275,13 @@ impl Packer {
self.flow.children.push(Styled::new(FlowChild::Spacing(kind), styles));
self.flow.last.hard();
}
- Node::Inline(inline) => {
+ Template::Inline(inline) => {
self.push_inline(Styled::new(ParChild::Node(inline), styles));
}
- Node::Block(block) => {
+ Template::Block(block) => {
self.push_block(Styled::new(block, styles));
}
- Node::Page(page) => {
+ Template::Page(page) => {
if self.top {
self.pagebreak();
self.pages.push(Styled::new(page, styles));
@@ -290,12 +289,12 @@ impl Packer {
self.push_block(Styled::new(page.0, styles));
}
}
- Node::Sequence(list) => {
- // For a list of nodes, we apply the list's styles to each node
- // individually.
- for mut node in list {
- node.map.apply(&styles);
- self.walk(node.item, node.map);
+ Template::Sequence(list) => {
+ // For a list of templates, we apply the list's styles to each
+ // templates individually.
+ for Styled { item, mut map } in list {
+ map.apply(&styles);
+ self.walk(item, map);
}
}
}
@@ -303,7 +302,7 @@ impl Packer {
/// Insert an inline-level element into the current paragraph.
fn push_inline(&mut self, child: Styled<ParChild>) {
- // The node must be both compatible with the current page and the
+ // The child's map must be both compatible with the current page and the
// current paragraph.
self.make_flow_compatible(&child.map);
self.make_par_compatible(&child.map);
@@ -458,28 +457,28 @@ impl<T> Default for Builder<T> {
}
}
-/// The kind of node that was last added to a flow or paragraph. A small finite
+/// The kind of child that was last added to a flow or paragraph. A small finite
/// state machine used to coalesce spaces.
///
-/// Soft nodes can only exist when surrounded by `Any` nodes. Not at the
-/// start, end or next to hard nodes. This way, spaces at start and end of
+/// Soft children can only exist when surrounded by `Any` children. Not at the
+/// start, end or next to hard children. This way, spaces at start and end of
/// paragraphs and next to `#h(..)` goes away.
enum Last<N> {
/// Start state, nothing there.
None,
/// Text or a block node or something.
Any,
- /// Hard nodes: Linebreaks and explicit spacing.
+ /// Hard children: Linebreaks and explicit spacing.
Hard,
- /// Soft nodes: Word spaces and paragraph breaks. These are saved here
- /// temporarily and then applied once an `Any` node appears. The boolean
- /// says whether this soft node is "important" and preferrable to other soft
+ /// Soft children: Word spaces and paragraph breaks. These are saved here
+ /// temporarily and then applied once an `Any` child appears. The boolean
+ /// says whether this soft child is "important" and preferrable to other soft
/// nodes (that is the case for explicit paragraph breaks).
Soft(N, bool),
}
impl<N> Last<N> {
- /// Transition into the `Any` state and return a soft node to really add
+ /// Transition into the `Any` state and return a soft child to really add
/// now if currently in `Soft` state.
fn any(&mut self) -> Option<N> {
match mem::replace(self, Self::Any) {
@@ -489,7 +488,7 @@ impl<N> Last<N> {
}
/// Transition into the `Soft` state, but only if in `Any`. Otherwise, the
- /// soft node is discarded.
+ /// soft child is discarded.
fn soft(&mut self, soft: N, important: bool) {
if matches!(
(&self, important),
@@ -500,7 +499,7 @@ impl<N> Last<N> {
}
/// Transition into the `Hard` state, discarding a possibly existing soft
- /// node and preventing further soft nodes from being added.
+ /// child and preventing further soft nodes from being added.
fn hard(&mut self) {
*self = Self::Hard;
}
diff --git a/src/eval/value.rs b/src/eval/value.rs
index c19794df..d0b822f1 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;
use std::sync::Arc;
-use super::{ops, Args, Array, Class, Dict, Func, Node};
+use super::{ops, Args, Array, Class, Dict, Func, Template};
use crate::diag::StrResult;
use crate::geom::{Angle, Color, Fractional, Length, Linear, Relative, RgbaColor};
use crate::layout::Layout;
@@ -42,8 +42,8 @@ pub enum Value {
Array(Array),
/// A dictionary value: `(color: #f79143, pattern: dashed)`.
Dict(Dict),
- /// A node value: `[*Hi* there]`.
- Node(Node),
+ /// A template value: `[*Hi* there]`.
+ Template(Template),
/// An executable function.
Func(Func),
/// Captured arguments to a function.
@@ -55,20 +55,20 @@ pub enum Value {
}
impl Value {
- /// Create an inline-level node value.
+ /// Create a template value from an inline-level node.
pub fn inline<T>(node: T) -> Self
where
T: Layout + Debug + Hash + Sync + Send + 'static,
{
- Self::Node(Node::inline(node))
+ Self::Template(Template::inline(node))
}
- /// Create a block-level node value.
+ /// Create a template value from a block-level node.
pub fn block<T>(node: T) -> Self
where
T: Layout + Debug + Hash + Sync + Send + 'static,
{
- Self::Node(Node::block(node))
+ Self::Template(Template::block(node))
}
/// The name of the stored value's type.
@@ -88,7 +88,7 @@ impl Value {
Self::Str(_) => EcoString::TYPE_NAME,
Self::Array(_) => Array::TYPE_NAME,
Self::Dict(_) => Dict::TYPE_NAME,
- Self::Node(_) => Node::TYPE_NAME,
+ Self::Template(_) => Template::TYPE_NAME,
Self::Func(_) => Func::TYPE_NAME,
Self::Args(_) => Args::TYPE_NAME,
Self::Class(_) => Class::TYPE_NAME,
@@ -115,16 +115,16 @@ impl Value {
}
/// Return the display representation of the value.
- pub fn show(self) -> Node {
+ pub fn show(self) -> Template {
match self {
- Value::None => Node::new(),
- Value::Int(v) => Node::Text(format_eco!("{}", v)),
- Value::Float(v) => Node::Text(format_eco!("{}", v)),
- Value::Str(v) => Node::Text(v),
- Value::Node(v) => v,
+ 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,
// For values which can't be shown "naturally", we print the
// representation in monospace.
- v => Node::Text(v.repr()).monospaced(),
+ v => Template::Text(v.repr()).monospaced(),
}
}
}
@@ -152,7 +152,7 @@ impl Debug for Value {
Self::Str(v) => Debug::fmt(v, f),
Self::Array(v) => Debug::fmt(v, f),
Self::Dict(v) => Debug::fmt(v, f),
- Self::Node(_) => f.pad("<template>"),
+ 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),
@@ -400,7 +400,7 @@ primitive! { Color: "color", Color }
primitive! { EcoString: "string", Str }
primitive! { Array: "array", Array }
primitive! { Dict: "dictionary", Dict }
-primitive! { Node: "template", Node }
+primitive! { Template: "template", Template }
primitive! { Func: "function", Func, Class(v) => v.constructor() }
primitive! { Args: "arguments", Args }
primitive! { Class: "class", Class }