Show with set

This commit is contained in:
Laurenz 2022-11-21 17:12:16 +01:00
parent c28d2130dd
commit dd9c323941
10 changed files with 25 additions and 19 deletions

View File

@ -851,7 +851,11 @@ impl Eval for ast::ShowRule {
let transform = self.transform();
let span = transform.span();
let transform = transform.eval(vm)?.cast::<Transform>().at(span)?;
let transform = match transform {
ast::Expr::Set(set) => Transform::Style(set.eval(vm)?),
expr => expr.eval(vm)?.cast::<Transform>().at(span)?,
};
Ok(Recipe { span, selector, transform })
}

View File

@ -491,6 +491,8 @@ pub enum Transform {
Content(Content),
/// A function to apply to the match.
Func(Func),
/// Apply styles to the content.
Style(StyleMap),
}
impl Transform {
@ -512,6 +514,7 @@ impl Transform {
}
Ok(result?.display())
}
Transform::Style(styles) => Ok(content.styled_with_map(styles.clone())),
}
}
}

View File

@ -458,7 +458,7 @@ mod tests {
test(r#"a ```typst hello``` b"#, 16 .. 17, "", 0 .. 18);
test(r#"a ```typst hello```"#, 16 .. 17, "", 0 .. 18);
test("#for", 4 .. 4, "//", 0 .. 6);
test("#show a: f as b..", 16..16, "c", 0..18);
test("#show f: a => b..", 16..16, "c", 0..18);
test("a\n#let \nb", 7 .. 7, "i", 2 .. 9);
test("a\n#for i \nb", 9 .. 9, "in", 2 .. 12);
test("a~https://fun/html", 13..14, "n", 0..18);

View File

@ -10,7 +10,7 @@ Next paragraph.
---
// Test that attached list isn't affected by block spacing.
#show list: it => { set block(above: 100pt); it }
#show list: set block(above: 100pt)
Hello
- A
World

View File

@ -27,5 +27,5 @@ A #rect(fill: yellow, inset: 5pt, rect()) B
---
// The constructor property should still work
// when there are recursive show rules.
#show list: text.with(blue)
#show list: set text(blue)
#list(label: "(a)", [A], list[B])

View File

@ -2,7 +2,7 @@
---
// Test labelled headings.
#show heading: text.with(10pt)
#show heading: set text(10pt)
#show heading.where(label: "intro"): underline
= Introduction <intro>
@ -13,7 +13,7 @@ The end.
---
// Test label after expression.
#show strong.where(label: "v"): text.with(red)
#show strong.where(label: "v"): set text(red)
#let a = [*A*]
#let b = [*B*]
@ -30,8 +30,8 @@ This is a thing [that <last>] happened.
---
// Test abusing labels for styling.
#show strong.where(label: "red"): text.with(red)
#show strong.where(label: "blue"): text.with(blue)
#show strong.where(label: "red"): set text(red)
#show strong.where(label: "blue"): set text(blue)
*A* *B* <red> *C* <blue> *D*

View File

@ -13,7 +13,7 @@
---
// Test full reset.
#show heading: [B]
#show heading: text.with(size: 10pt, weight: 400)
#show heading: set text(size: 10pt, weight: 400)
A [= Heading] C
---

View File

@ -28,9 +28,9 @@ code!("it");
```
---
#show heading.where(level: 1): text.with(red)
#show heading.where(level: 2): text.with(blue)
#show heading: text.with(green)
#show heading.where(level: 1): set text(red)
#show heading.where(level: 2): set text(blue)
#show heading: set text(green)
= Red
== Blue
=== Green

View File

@ -3,7 +3,7 @@
---
#set par(indent: 12pt, leading: 5pt)
#set block(spacing: 5pt)
#show heading: text.with(size: 10pt)
#show heading: set text(size: 10pt)
The first paragraph has no indent.

View File

@ -16,18 +16,17 @@ It is the east, and Juliet is the sun.
---
// Test that paragraph spacing loses against block spacing.
// TODO
// #set block(spacing: 100pt)
// #show table: set block(spacing: 5pt)
#set block(spacing: 5pt)
#set block(spacing: 100pt)
#show table: set block(above: 5pt, below: 5pt)
Hello
#table(columns: 4, fill: (x, y) => if odd(x + y) { silver })[A][B][C][D]
---
// While we're at it, test the larger block spacing wins.
#set block(spacing: 0pt)
#show raw: it => { set block(spacing: 15pt); it }
#show math: it => { set block(spacing: 7.5pt); it }
#show list: it => { set block(spacing: 2.5pt); it }
#show raw: set block(spacing: 15pt)
#show math: set block(spacing: 7.5pt)
#show list: set block(spacing: 2.5pt)
```rust
fn main() {}