mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
New display-math syntax 🧮
Changed to `$[x]$` instead of `$$x$$` because then `$$` simply is an empty formula that does not poison the whole document.
This commit is contained in:
parent
89eb8bae49
commit
fe7ea53800
@ -249,32 +249,38 @@ impl<'s> Tokens<'s> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn math(&mut self) -> Token<'s> {
|
fn math(&mut self) -> Token<'s> {
|
||||||
let mut dollars = 1;
|
let mut inline = true;
|
||||||
if self.s.eat_if('$') {
|
if self.s.eat_if('[') {
|
||||||
dollars = 2;
|
inline = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let start = self.s.index();
|
let start = self.s.index();
|
||||||
|
|
||||||
let mut found = 0;
|
|
||||||
let mut escaped = false;
|
let mut escaped = false;
|
||||||
while found < dollars {
|
let mut dollar = inline;
|
||||||
|
|
||||||
|
let terminated = loop {
|
||||||
match self.s.eat() {
|
match self.s.eat() {
|
||||||
Some('$') if !escaped => found += 1,
|
Some('$') if !escaped && dollar => break true,
|
||||||
|
Some(']') if !escaped => dollar = true,
|
||||||
Some(c) => {
|
Some(c) => {
|
||||||
found = 0;
|
dollar = inline;
|
||||||
escaped = c == '\\' && !escaped;
|
escaped = c == '\\' && !escaped;
|
||||||
}
|
}
|
||||||
None => break,
|
None => break false,
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
let terminated = found == dollars;
|
let end = self.s.index()
|
||||||
let end = self.s.index() - if terminated { found } else { 0 };
|
- match (terminated, inline) {
|
||||||
|
(false, _) => 0,
|
||||||
|
(true, true) => 1,
|
||||||
|
(true, false) => 2,
|
||||||
|
};
|
||||||
|
|
||||||
Token::Math(TokenMath {
|
Token::Math(TokenMath {
|
||||||
formula: self.s.get(start .. end),
|
formula: self.s.get(start .. end),
|
||||||
inline: dollars == 1,
|
inline,
|
||||||
terminated,
|
terminated,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -737,16 +743,21 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_tokenize_math_formulas() {
|
fn test_tokenize_math_formulas() {
|
||||||
// Test basic formula.
|
// Test basic formula.
|
||||||
t!(Markup: "$x$" => Math("x", true, true));
|
t!(Markup: "$$" => Math("", true, true));
|
||||||
t!(Markup: "$$x + y$$" => Math("x + y", false, true));
|
t!(Markup: "$x$" => Math("x", true, true));
|
||||||
|
t!(Markup: r"$\\$" => Math(r"\\", true, true));
|
||||||
|
t!(Markup: "$[x + y]$" => Math("x + y", false, true));
|
||||||
|
t!(Markup: r"$[\\]$" => Math(r"\\", false, true));
|
||||||
|
|
||||||
// Test unterminated.
|
// Test unterminated.
|
||||||
t!(Markup[""]: "$$x" => Math("x", false, false));
|
t!(Markup[""]: "$x" => Math("x", true, false));
|
||||||
t!(Markup[""]: "$$x$\n$" => Math("x$\n$", false, false));
|
t!(Markup[""]: "$[x" => Math("x", false, false));
|
||||||
|
t!(Markup[""]: "$[x]\n$" => Math("x]\n$", false, false));
|
||||||
|
|
||||||
// Test escape sequences.
|
// Test escape sequences.
|
||||||
t!(Markup: r"$$\\\$$$" => Math(r"\\\$", false, true));
|
t!(Markup: r"$\$x$" => Math(r"\$x", true, true));
|
||||||
t!(Markup[""]: r"$$ $\\$" => Math(r" $\\$", false, false));
|
t!(Markup: r"$[\\\]$]$" => Math(r"\\\]$", false, true));
|
||||||
|
t!(Markup[""]: r"$[ ]\\$" => Math(r" ]\\$", false, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user