summaryrefslogtreecommitdiff
path: root/src/parse
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-01-30 12:36:41 +0100
committerLaurenz <laurmaedje@gmail.com>2021-01-30 12:36:41 +0100
commitfe7ea53800e3b646e3c51c1f10a1e95a06334c7f (patch)
treeb1c518845b0993853bcff8e970ef2a7da8a66152 /src/parse
parent89eb8bae49edb71d9a9279a2210bb1ccaf4dd707 (diff)
New display-math syntax 🧮
Changed to `$[x]$` instead of `$$x$$` because then `$$` simply is an empty formula that does not poison the whole document.
Diffstat (limited to 'src/parse')
-rw-r--r--src/parse/tokens.rs47
1 files changed, 29 insertions, 18 deletions
diff --git a/src/parse/tokens.rs b/src/parse/tokens.rs
index 4f5d8ab4..71e71e3e 100644
--- a/src/parse/tokens.rs
+++ b/src/parse/tokens.rs
@@ -249,32 +249,38 @@ impl<'s> Tokens<'s> {
}
fn math(&mut self) -> Token<'s> {
- let mut dollars = 1;
- if self.s.eat_if('$') {
- dollars = 2;
+ let mut inline = true;
+ if self.s.eat_if('[') {
+ inline = false;
}
let start = self.s.index();
- let mut found = 0;
let mut escaped = false;
- while found < dollars {
+ let mut dollar = inline;
+
+ let terminated = loop {
match self.s.eat() {
- Some('$') if !escaped => found += 1,
+ Some('$') if !escaped && dollar => break true,
+ Some(']') if !escaped => dollar = true,
Some(c) => {
- found = 0;
+ dollar = inline;
escaped = c == '\\' && !escaped;
}
- None => break,
+ None => break false,
}
- }
+ };
- let terminated = found == dollars;
- let end = self.s.index() - if terminated { found } else { 0 };
+ let end = self.s.index()
+ - match (terminated, inline) {
+ (false, _) => 0,
+ (true, true) => 1,
+ (true, false) => 2,
+ };
Token::Math(TokenMath {
formula: self.s.get(start .. end),
- inline: dollars == 1,
+ inline,
terminated,
})
}
@@ -737,16 +743,21 @@ mod tests {
#[test]
fn test_tokenize_math_formulas() {
// Test basic formula.
- t!(Markup: "$x$" => Math("x", true, true));
- t!(Markup: "$$x + y$$" => Math("x + y", false, true));
+ t!(Markup: "$$" => Math("", true, 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.
- t!(Markup[""]: "$$x" => Math("x", false, false));
- t!(Markup[""]: "$$x$\n$" => Math("x$\n$", false, false));
+ t!(Markup[""]: "$x" => Math("x", true, false));
+ t!(Markup[""]: "$[x" => Math("x", false, false));
+ t!(Markup[""]: "$[x]\n$" => Math("x]\n$", false, false));
// Test escape sequences.
- t!(Markup: r"$$\\\$$$" => Math(r"\\\$", false, true));
- t!(Markup[""]: r"$$ $\\$" => Math(r" $\\$", false, false));
+ t!(Markup: r"$\$x$" => Math(r"\$x", true, true));
+ t!(Markup: r"$[\\\]$]$" => Math(r"\\\]$", false, true));
+ t!(Markup[""]: r"$[ ]\\$" => Math(r" ]\\$", false, false));
}
#[test]