Fix that templates don't create a scope

This commit is contained in:
Laurenz 2022-01-29 22:32:30 +01:00
parent 44ebefbec2
commit aeb036f4dc
7 changed files with 28 additions and 12 deletions

14
Cargo.lock generated
View File

@ -399,9 +399,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.113" version = "0.2.116"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eef78b64d87775463c549fbd80e19249ef436ea3bf1de2a1eb7e717ec7fab1e9" checksum = "565dbd88872dbe4cc8a46e527f26483c1d1f7afa6b884a3bd6cd893d4f98da74"
[[package]] [[package]]
name = "line-wrap" name = "line-wrap"
@ -537,7 +537,7 @@ checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468"
[[package]] [[package]]
name = "pixglyph" name = "pixglyph"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/typst/pixglyph#b63319b30eb34bcd7f3f3bb4c253a0731bae4234" source = "git+https://github.com/typst/pixglyph#da648abb60d0e0f4353cd7602652c832132e6a39"
dependencies = [ dependencies = [
"ttf-parser", "ttf-parser",
] ]
@ -769,18 +769,18 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.135" version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cf9235533494ea2ddcdb794665461814781c53f19d87b76e571a1c35acbad2b" checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.135" version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dcde03d87d4c973c04be249e7d8f0b35db1c848c487bd43032808e59dd8328d" checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -187,7 +187,7 @@ fn process_nodes(
ctx: &mut EvalContext, ctx: &mut EvalContext,
nodes: &mut impl Iterator<Item = MarkupNode>, nodes: &mut impl Iterator<Item = MarkupNode>,
) -> TypResult<Node> { ) -> TypResult<Node> {
let prev = mem::take(&mut ctx.styles); 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() { while let Some(piece) = nodes.next() {
// Need to deal with wrap here. // Need to deal with wrap here.
@ -201,7 +201,7 @@ fn process_nodes(
seq.push(Styled::new(node, ctx.styles.clone())); seq.push(Styled::new(node, ctx.styles.clone()));
} }
ctx.styles = prev; ctx.styles = prev_styles;
Ok(Node::Sequence(seq)) Ok(Node::Sequence(seq))
} }
@ -461,7 +461,10 @@ impl Eval for TemplateExpr {
type Output = Node; type Output = Node;
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
self.body().eval(ctx) ctx.scopes.enter();
let node = self.body().eval(ctx)?;
ctx.scopes.exit();
Ok(node)
} }
} }

View File

@ -17,7 +17,7 @@ pub mod list;
pub mod pad; pub mod pad;
pub mod page; pub mod page;
pub mod par; pub mod par;
pub mod placed; pub mod place;
pub mod shape; pub mod shape;
pub mod spacing; pub mod spacing;
pub mod stack; pub mod stack;
@ -40,7 +40,7 @@ pub use list::*;
pub use pad::*; pub use pad::*;
pub use page::*; pub use page::*;
pub use par::*; pub use par::*;
pub use placed::*; pub use place::*;
pub use shape::*; pub use shape::*;
pub use spacing::*; pub use spacing::*;
pub use stack::*; pub use stack::*;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -102,6 +102,13 @@
test(a, "a1") test(a, "a1")
} }
---
// Template also creates a scope.
[#let x = 1]
// Error: 2-3 unknown variable
{x}
--- ---
// Multiple unseparated expressions in one line. // Multiple unseparated expressions in one line.

View File

@ -18,6 +18,12 @@ in booklovers and the great fulfiller of human need.
// Test wrap in template. // Test wrap in template.
A [_B #wrap c in [*#c*]; C] D A [_B #wrap c in [*#c*]; C] D
---
// Test wrap style precedence.
#set text(fill: eastern, size: 150%)
#wrap body in text(fill: forest, body)
Forest
--- ---
// Error: 6-17 wrap is only allowed directly in markup // Error: 6-17 wrap is only allowed directly in markup
{1 + wrap x in y} {1 + wrap x in y}