From bdb1c008f2cee7c3929b671d9407d2b578182199 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 2 Feb 2022 11:28:31 +0100 Subject: [PATCH] Allow set & show only directly in markup --- src/eval/mod.rs | 56 ++++++++++++++++++---------------- tests/ref/style/set-block.png | Bin 1151 -> 0 bytes tests/typ/style/set-block.typ | 10 ------ tests/typ/style/set.typ | 5 +++ tests/typ/style/show.typ | 5 ++- 5 files changed, 38 insertions(+), 38 deletions(-) delete mode 100644 tests/ref/style/set-block.png delete mode 100644 tests/typ/style/set-block.typ create mode 100644 tests/typ/style/set.typ diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 5129a41ff..ebbeec3d4 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -97,8 +97,6 @@ pub struct EvalContext<'a> { pub modules: HashMap, /// The active scopes. pub scopes: Scopes<'a>, - /// The active styles. - pub styles: StyleMap, } impl<'a> EvalContext<'a> { @@ -111,7 +109,6 @@ impl<'a> EvalContext<'a> { route: vec![source], modules: HashMap::new(), scopes: Scopes::new(Some(&ctx.std)), - styles: StyleMap::new(), } } @@ -143,7 +140,6 @@ impl<'a> EvalContext<'a> { // Prepare the new context. let new_scopes = Scopes::new(self.scopes.base); let prev_scopes = mem::replace(&mut self.scopes, new_scopes); - let prev_styles = mem::take(&mut self.styles); self.route.push(id); // Evaluate the module. @@ -151,7 +147,6 @@ impl<'a> EvalContext<'a> { // Restore the old context. let new_scopes = mem::replace(&mut self.scopes, prev_scopes); - self.styles = prev_styles; self.route.pop().unwrap(); // Save the evaluated module. @@ -178,30 +173,42 @@ impl Eval for Markup { type Output = Node; fn eval(&self, ctx: &mut EvalContext) -> TypResult { - process_nodes(ctx, &mut self.nodes()) + eval_markup(ctx, &mut self.nodes()) } } -/// Evaluate a stream of nodes. -fn process_nodes( +/// Evaluate a stream of markup nodes. +fn eval_markup( ctx: &mut EvalContext, nodes: &mut impl Iterator, ) -> TypResult { - let prev_styles = mem::take(&mut ctx.styles); let mut seq = Vec::with_capacity(nodes.size_hint().1.unwrap_or_default()); - while let Some(piece) = nodes.next() { - // Need to deal with wrap here. - let node = if let MarkupNode::Expr(Expr::Wrap(wrap)) = piece { - let tail = process_nodes(ctx, nodes)?; - ctx.scopes.def_mut(wrap.binding().take(), tail); - wrap.body().eval(ctx)?.show() - } else { - piece.eval(ctx)? + let mut styles = StyleMap::new(); + + while let Some(node) = nodes.next() { + let result = match node { + MarkupNode::Expr(Expr::Set(set)) => { + let class = set.class(); + let class = class.eval(ctx)?.cast::().at(class.span())?; + let mut args = set.args().eval(ctx)?; + class.set(&mut args, &mut styles)?; + args.finish()?; + continue; + } + MarkupNode::Expr(Expr::Show(show)) => { + return Err("show rules are not yet implemented").at(show.span()); + } + MarkupNode::Expr(Expr::Wrap(wrap)) => { + let tail = eval_markup(ctx, nodes)?; + ctx.scopes.def_mut(wrap.binding().take(), tail); + wrap.body().eval(ctx)?.show() + } + _ => node.eval(ctx)?, }; - seq.push(Styled::new(node, ctx.styles.clone())); + seq.push(Styled::new(result, styles.clone())); } - ctx.styles = prev_styles; + Ok(Node::Sequence(seq)) } @@ -738,13 +745,8 @@ impl Eval for LetExpr { impl Eval for SetExpr { type Output = Value; - fn eval(&self, ctx: &mut EvalContext) -> TypResult { - let class = self.class(); - let class = class.eval(ctx)?.cast::().at(class.span())?; - let mut args = self.args().eval(ctx)?; - class.set(&mut args, &mut ctx.styles)?; - args.finish()?; - Ok(Value::None) + fn eval(&self, _: &mut EvalContext) -> TypResult { + Err("set is only allowed directly in markup").at(self.span()) } } @@ -752,7 +754,7 @@ impl Eval for ShowExpr { type Output = Value; fn eval(&self, _: &mut EvalContext) -> TypResult { - Err("show rules are not yet implemented").at(self.span()) + Err("show is only allowed directly in markup").at(self.span()) } } diff --git a/tests/ref/style/set-block.png b/tests/ref/style/set-block.png deleted file mode 100644 index 898c9c6597b0fc241516d7b0fbe9829e908b9002..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1151 zcmeAS@N?(olHy`uVBq!ia0y~yU}OQZyEvGEWVQ4W6`;Vy0G|-o|NsC01EPQb{{H*_ z=ik3SfByXZ`}gT>2+3ttW zb^+0Yr#tRH-FE-U=DUwCUc9*R&ZD)rAFjRiVD+sBD{kCBb?Vg8>vtDlySwPxoufyO z9yxMk&gEOvF5K9^fB%#V*C(I9KIz=GiRZ5Mow?e3=E}~UJG)L_?mT^Y+qP|OCoi>} zxVUA@mZswu8jfA4KX!iO#*I}+&Q=^gyJpRr)vH(M??0Kl@5HiY%d+>LSh{p+=APpj zyN@OAI1<0(aMad=vu4eT*m5v*^MR1f`-3;__gTNkbKP$DwYyx_>~vnev$wa`Vbu=% zmD}xBZns&n&0^VBv!z>1mu}Htv{7%-#>U1*-Gv);7H-g5uwE00=C9Y7zfOJLx|*6A zwR!7Q=dD$lyHVVW7l$aYOOG#56 zQ~kU{ujN*WRcx(R40k}?*H6DJyI(Jo^>o;Ad!y#P`OX~TOjBZZFo~GQ$gpp)o)h+h z<3PJQv)#ToeFqu~Omw=m7$l+sLtb!fFuHf@ul(cP$})T7czz#J{QK4Hg)WoLAEOyd zKW?;P5;>o{meJ4TYku=R1)g7DI4b^IwOWIwZ?b( z<=1?7@3EPvvNBf4*ymml3_}oeB=ubeXBO+4O;uAxi`GZP@7^hPnLOu_MEr} z9Lf9b-`~1*-%-mdCNwm(SY-|KjJq`}v;KYhRZzT8zP)*__3mxebGc`TvN#H`Jl{5j zDWGhRC6DwpkzaE!T;`c$v1-52k6>3=EFj_o#QG(5P`wc;I{ru=Jf8-QFI1H-Kc;hh z6Mmz9!+*p41p9+^O@CPs9Htxo48J*dZk&JN@(uqN@0GDcF)-R|yB