diff options
Diffstat (limited to 'src/exec')
| -rw-r--r-- | src/exec/context.rs | 90 | ||||
| -rw-r--r-- | src/exec/mod.rs | 22 |
2 files changed, 57 insertions, 55 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(), diff --git a/src/exec/mod.rs b/src/exec/mod.rs index 63e47995..16fd75f8 100644 --- a/src/exec/mod.rs +++ b/src/exec/mod.rs @@ -51,8 +51,8 @@ impl ExecWithMap for SyntaxTree { impl ExecWithMap for SyntaxNode { fn exec_with_map(&self, ctx: &mut ExecContext, map: &ExprMap) { match self { - Self::Space => ctx.push_word_space(), - Self::Text(text) => ctx.push_text(text), + Self::Space => ctx.space(), + Self::Text(text) => ctx.text(text), Self::Linebreak(_) => ctx.linebreak(), Self::Parbreak(_) => ctx.parbreak(), Self::Strong(_) => ctx.state.font_mut().strong ^= true, @@ -72,7 +72,7 @@ impl Exec for RawNode { ctx.parbreak(); } - ctx.push_monospace_text(&self.text); + ctx.text_mono(&self.text); if self.block { ctx.parbreak(); @@ -112,9 +112,9 @@ impl ExecWithMap for EnumItem { } fn exec_item(ctx: &mut ExecContext, label: EcoString, body: &SyntaxTree, map: &ExprMap) { - let label = ctx.exec_stack(|ctx| ctx.push_text(label)); - let body = ctx.exec_tree_stack(body, map); - ctx.push_into_stack(StackNode { + let label = ctx.exec_to_stack(|ctx| ctx.text(label)); + let body = ctx.exec_tree(body, map); + ctx.block(StackNode { dirs: Gen::new(ctx.state.dirs.main, ctx.state.dirs.cross), aspect: None, children: vec