summaryrefslogtreecommitdiff
path: root/src/exec/context.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-08-17 12:49:48 +0200
committerLaurenz <laurmaedje@gmail.com>2021-08-17 12:49:48 +0200
commitc53d98a22f367a9eecfb45d1b22f1be5c6cf908d (patch)
treeac319fa1b53d659bc021f5ebfd85b868f982a922 /src/exec/context.rs
parent9a798ce6f6e734a02764473891632c071fed41ee (diff)
More logical ordering and naming
Diffstat (limited to 'src/exec/context.rs')
-rw-r--r--src/exec/context.rs90
1 files changed, 46 insertions, 44 deletions
diff --git a/src/exec/context.rs b/src/exec/context.rs
index 2b0ef9fa..d8ce6528 100644
--- a/src/exec/context.rs
+++ b/src/exec/context.rs
@@ -35,57 +35,55 @@ impl ExecContext {
}
}
- /// Execute a template and return the result as a stack node.
- pub fn exec_template_stack(&mut self, template: &Template) -> StackNode {
- self.exec_stack(|ctx| template.exec(ctx))
+ /// Push a word space into the active paragraph.
+ pub fn space(&mut self) {
+ self.stack.par.push_soft(self.make_text_node(' '));
}
- /// Execute a syntax tree with a map and return the result as a stack node.
- pub fn exec_tree_stack(&mut self, tree: &SyntaxTree, map: &ExprMap) -> StackNode {
- self.exec_stack(|ctx| tree.exec_with_map(ctx, map))
+ /// Apply a forced line break.
+ pub fn linebreak(&mut self) {
+ self.stack.par.push_hard(self.make_text_node('\n'));
}
- /// Execute something and return the result as a stack node.
- pub fn exec_stack(&mut self, f: impl FnOnce(&mut Self)) -> StackNode {
- let snapshot = self.state.clone();
- let page = self.page.take();
- let stack = mem::replace(&mut self.stack, StackBuilder::new(&self.state));
-
- f(self);
+ /// Apply a forced paragraph break.
+ pub fn parbreak(&mut self) {
+ let amount = self.state.par_spacing();
+ self.stack.finish_par(&self.state);
+ self.stack.push_soft(StackChild::Spacing(amount.into()));
+ }
- self.state = snapshot;
- self.page = page;
- mem::replace(&mut self.stack, stack).build()
+ /// Apply a forced page break.
+ pub fn pagebreak(&mut self, keep: bool, hard: bool) {
+ if let Some(builder) = &mut self.page {
+ let page = mem::replace(builder, PageBuilder::new(&self.state, hard));
+ let stack = mem::replace(&mut self.stack, StackBuilder::new(&self.state));
+ self.tree.runs.extend(page.build(stack.build(), keep));
+ }
}
/// Push text into the active paragraph.
///
/// The text is split into lines at newlines.
- pub fn push_text(&mut self, text: impl Into<EcoString>) {
+ pub fn text(&mut self, text: impl Into<EcoString>) {
self.stack.par.push(self.make_text_node(text));
}
/// Push text, but in monospace.
- pub fn push_monospace_text(&mut self, text: impl Into<EcoString>) {
+ pub fn text_mono(&mut self, text: impl Into<EcoString>) {
let prev = Rc::clone(&self.state.font);
self.state.font_mut().monospace = true;
- self.push_text(text);
+ self.text(text);
self.state.font = prev;
}
- /// Push a word space into the active paragraph.
- pub fn push_word_space(&mut self) {
- self.stack.par.push_soft(self.make_text_node(' '));
- }
-
- /// Push any node into the active paragraph.
- pub fn push_into_par(&mut self, node: impl Into<LayoutNode>) {
+ /// Push an inline node into the active paragraph.
+ pub fn inline(&mut self, node: impl Into<LayoutNode>) {
let align = self.state.aligns.cross;
self.stack.par.push(ParChild::Any(node.into(), align));
}
- /// Push any node into the active stack.
- pub fn push_into_stack(&mut self, node: impl Into<LayoutNode>) {
+ /// Push a block node into the active stack, finishing the active paragraph.
+ pub fn block(&mut self, node: impl Into<LayoutNode>) {
self.parbreak();
let aligns = self.state.aligns;
self.stack.push(StackChild::Any(node.into(), aligns));
@@ -93,7 +91,7 @@ impl ExecContext {
}
/// Push spacing into the active paragraph or stack depending on the `axis`.
- pub fn push_spacing(&mut self, axis: GenAxis, amount: Linear) {
+ pub fn spacing(&mut self, axis: GenAxis, amount: Linear) {
match axis {
GenAxis::Main => {
self.stack.finish_par(&self.state);
@@ -105,25 +103,27 @@ impl ExecContext {
}
}
- /// Apply a forced line break.
- pub fn linebreak(&mut self) {
- self.stack.par.push_hard(self.make_text_node('\n'));
+ /// Execute a template and return the result as a stack node.
+ pub fn exec_template(&mut self, template: &Template) -> StackNode {
+ self.exec_to_stack(|ctx| template.exec(ctx))
}
- /// Apply a forced paragraph break.
- pub fn parbreak(&mut self) {
- let amount = self.state.par_spacing();
- self.stack.finish_par(&self.state);
- self.stack.push_soft(StackChild::Spacing(amount.into()));
+ /// Execute a syntax tree with a map and return the result as a stack node.
+ pub fn exec_tree(&mut self, tree: &SyntaxTree, map: &ExprMap) -> StackNode {
+ self.exec_to_stack(|ctx| tree.exec_with_map(ctx, map))
}
- /// Apply a forced page break.
- pub fn pagebreak(&mut self, keep: bool, hard: bool) {
- if let Some(builder) = &mut self.page {
- let page = mem::replace(builder, PageBuilder::new(&self.state, hard));
- let stack = mem::replace(&mut self.stack, StackBuilder::new(&self.state));
- self.tree.runs.extend(page.build(stack.build(), keep));
- }
+ /// Execute something and return the result as a stack node.
+ pub fn exec_to_stack(&mut self, f: impl FnOnce(&mut Self)) -> StackNode {
+ let snapshot = self.state.clone();
+ let page = self.page.take();
+ let stack = mem::replace(&mut self.stack, StackBuilder::new(&self.state));
+
+ f(self);
+
+ self.state = snapshot;
+ self.page = page;
+ mem::replace(&mut self.stack, stack).build()
}
/// Finish execution and return the created layout tree.
@@ -133,6 +133,8 @@ impl ExecContext {
self.tree
}
+ /// Construct a text node with the given text and settings from the active
+ /// state.
fn make_text_node(&self, text: impl Into<EcoString>) -> ParChild {
ParChild::Text(
text.into(),