mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Allow set & show only directly in markup
This commit is contained in:
parent
7c33c1e691
commit
bdb1c008f2
@ -97,8 +97,6 @@ pub struct EvalContext<'a> {
|
|||||||
pub modules: HashMap<SourceId, Module>,
|
pub modules: HashMap<SourceId, Module>,
|
||||||
/// The active scopes.
|
/// The active scopes.
|
||||||
pub scopes: Scopes<'a>,
|
pub scopes: Scopes<'a>,
|
||||||
/// The active styles.
|
|
||||||
pub styles: StyleMap,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> EvalContext<'a> {
|
impl<'a> EvalContext<'a> {
|
||||||
@ -111,7 +109,6 @@ impl<'a> EvalContext<'a> {
|
|||||||
route: vec![source],
|
route: vec![source],
|
||||||
modules: HashMap::new(),
|
modules: HashMap::new(),
|
||||||
scopes: Scopes::new(Some(&ctx.std)),
|
scopes: Scopes::new(Some(&ctx.std)),
|
||||||
styles: StyleMap::new(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +140,6 @@ impl<'a> EvalContext<'a> {
|
|||||||
// Prepare the new context.
|
// Prepare the new context.
|
||||||
let new_scopes = Scopes::new(self.scopes.base);
|
let new_scopes = Scopes::new(self.scopes.base);
|
||||||
let prev_scopes = mem::replace(&mut self.scopes, new_scopes);
|
let prev_scopes = mem::replace(&mut self.scopes, new_scopes);
|
||||||
let prev_styles = mem::take(&mut self.styles);
|
|
||||||
self.route.push(id);
|
self.route.push(id);
|
||||||
|
|
||||||
// Evaluate the module.
|
// Evaluate the module.
|
||||||
@ -151,7 +147,6 @@ impl<'a> EvalContext<'a> {
|
|||||||
|
|
||||||
// Restore the old context.
|
// Restore the old context.
|
||||||
let new_scopes = mem::replace(&mut self.scopes, prev_scopes);
|
let new_scopes = mem::replace(&mut self.scopes, prev_scopes);
|
||||||
self.styles = prev_styles;
|
|
||||||
self.route.pop().unwrap();
|
self.route.pop().unwrap();
|
||||||
|
|
||||||
// Save the evaluated module.
|
// Save the evaluated module.
|
||||||
@ -178,30 +173,42 @@ impl Eval for Markup {
|
|||||||
type Output = Node;
|
type Output = Node;
|
||||||
|
|
||||||
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
|
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
|
||||||
process_nodes(ctx, &mut self.nodes())
|
eval_markup(ctx, &mut self.nodes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Evaluate a stream of nodes.
|
/// Evaluate a stream of markup nodes.
|
||||||
fn process_nodes(
|
fn eval_markup(
|
||||||
ctx: &mut EvalContext,
|
ctx: &mut EvalContext,
|
||||||
nodes: &mut impl Iterator<Item = MarkupNode>,
|
nodes: &mut impl Iterator<Item = MarkupNode>,
|
||||||
) -> TypResult<Node> {
|
) -> TypResult<Node> {
|
||||||
let prev_styles = mem::take(&mut ctx.styles);
|
|
||||||
let mut seq = Vec::with_capacity(nodes.size_hint().1.unwrap_or_default());
|
let mut seq = Vec::with_capacity(nodes.size_hint().1.unwrap_or_default());
|
||||||
while let Some(piece) = nodes.next() {
|
let mut styles = StyleMap::new();
|
||||||
// Need to deal with wrap here.
|
|
||||||
let node = if let MarkupNode::Expr(Expr::Wrap(wrap)) = piece {
|
while let Some(node) = nodes.next() {
|
||||||
let tail = process_nodes(ctx, nodes)?;
|
let result = match node {
|
||||||
ctx.scopes.def_mut(wrap.binding().take(), tail);
|
MarkupNode::Expr(Expr::Set(set)) => {
|
||||||
wrap.body().eval(ctx)?.show()
|
let class = set.class();
|
||||||
} else {
|
let class = class.eval(ctx)?.cast::<Class>().at(class.span())?;
|
||||||
piece.eval(ctx)?
|
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))
|
Ok(Node::Sequence(seq))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -738,13 +745,8 @@ impl Eval for LetExpr {
|
|||||||
impl Eval for SetExpr {
|
impl Eval for SetExpr {
|
||||||
type Output = Value;
|
type Output = Value;
|
||||||
|
|
||||||
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
|
fn eval(&self, _: &mut EvalContext) -> TypResult<Self::Output> {
|
||||||
let class = self.class();
|
Err("set is only allowed directly in markup").at(self.span())
|
||||||
let class = class.eval(ctx)?.cast::<Class>().at(class.span())?;
|
|
||||||
let mut args = self.args().eval(ctx)?;
|
|
||||||
class.set(&mut args, &mut ctx.styles)?;
|
|
||||||
args.finish()?;
|
|
||||||
Ok(Value::None)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -752,7 +754,7 @@ impl Eval for ShowExpr {
|
|||||||
type Output = Value;
|
type Output = Value;
|
||||||
|
|
||||||
fn eval(&self, _: &mut EvalContext) -> TypResult<Self::Output> {
|
fn eval(&self, _: &mut EvalContext) -> TypResult<Self::Output> {
|
||||||
Err("show rules are not yet implemented").at(self.span())
|
Err("show is only allowed directly in markup").at(self.span())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 1.1 KiB |
@ -1,10 +0,0 @@
|
|||||||
// Test set in code blocks.
|
|
||||||
|
|
||||||
---
|
|
||||||
// Test that template in block is not affected by set
|
|
||||||
// rule in block ...
|
|
||||||
A{set text(fill: eastern); [B]}C
|
|
||||||
|
|
||||||
---
|
|
||||||
// ... no matter the order.
|
|
||||||
A{[B]; set text(fill: eastern)}C
|
|
5
tests/typ/style/set.typ
Normal file
5
tests/typ/style/set.typ
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// General tests for set.
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 2-10 set is only allowed directly in markup
|
||||||
|
{set f(x)}
|
@ -1,6 +1,9 @@
|
|||||||
// Test show rules.
|
// Test show rules.
|
||||||
// Ref: false
|
|
||||||
|
|
||||||
---
|
---
|
||||||
// Error: 1-34 show rules are not yet implemented
|
// Error: 1-34 show rules are not yet implemented
|
||||||
#show heading(body) as [*{body}*]
|
#show heading(body) as [*{body}*]
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 2-15 show is only allowed directly in markup
|
||||||
|
{show (a) as b}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user