mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-07 20:28:38 +00:00
14
src/Lexer.js
14
src/Lexer.js
@@ -19,6 +19,7 @@ import {LexerInterface, Token} from "./Token";
|
||||
|
||||
/* The following tokenRegex
|
||||
* - matches typical whitespace (but not NBSP etc.) using its first group
|
||||
* - matches comments (must have trailing newlines)
|
||||
* - does not match any control character \x00-\x1f except whitespace
|
||||
* - does not match a bare backslash
|
||||
* - matches any ASCII character except those just mentioned
|
||||
@@ -32,9 +33,11 @@ import {LexerInterface, Token} from "./Token";
|
||||
* If there is no matching function or symbol definition, the Parser will
|
||||
* still reject the input.
|
||||
*/
|
||||
const commentRegexString = "%[^\n]*[\n]";
|
||||
const tokenRegex = new RegExp(
|
||||
"([ \r\n\t]+)|" + // whitespace
|
||||
"([!-\\[\\]-\u2027\u202A-\uD7FF\uF900-\uFFFF]" + // single codepoint
|
||||
`(${commentRegexString}|` + // comments
|
||||
"[!-\\[\\]-\u2027\u202A-\uD7FF\uF900-\uFFFF]" + // single codepoint
|
||||
"|[\uD800-\uDBFF][\uDC00-\uDFFF]" + // surrogate pair
|
||||
"|\\\\verb\\*([^]).*?\\3" + // \verb*
|
||||
"|\\\\verb([^*a-zA-Z]).*?\\4" + // \verb unstarred
|
||||
@@ -42,6 +45,8 @@ const tokenRegex = new RegExp(
|
||||
")"
|
||||
);
|
||||
|
||||
const commentRegex = new RegExp(commentRegexString);
|
||||
|
||||
/** Main Lexer class */
|
||||
export default class Lexer implements LexerInterface {
|
||||
input: string;
|
||||
@@ -71,6 +76,11 @@ export default class Lexer implements LexerInterface {
|
||||
const start = this.pos;
|
||||
this.pos += match[0].length;
|
||||
const end = this.pos;
|
||||
return new Token(text, new SourceLocation(this, start, end));
|
||||
|
||||
if (commentRegex.test(text)) {
|
||||
return this.lex();
|
||||
} else {
|
||||
return new Token(text, new SourceLocation(this, start, end));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,9 +8,11 @@
|
||||
<link href="main.css" rel="stylesheet" type="text/css">
|
||||
</head>
|
||||
<body>
|
||||
<input type="text"
|
||||
value="(\left( x \right) \left( x^2 \right) \left( \frac{a}{b} \right) \left( \frac{a^2}{b} \right) \left( \dfrac{a}{b} \right) \left( \dfrac{a^2}{b} \right)"
|
||||
id="input" />
|
||||
<textarea id="input" rows="5">
|
||||
\left( x \right) \left( x^2 \right) % comment
|
||||
\left( \frac{a}{b} \right) \left( \frac{a^2}{b} \right)
|
||||
\left( \dfrac{a}{b} \right) \left( \dfrac{a^2}{b} \right)
|
||||
</textarea>
|
||||
<div id="math"></div>
|
||||
<input id="permalink" type="button" value="permalink">
|
||||
<script src="main.js" type="text/javascript"></script>
|
||||
|
@@ -1,6 +1,10 @@
|
||||
body {
|
||||
margin: 0px;
|
||||
padding: 0px;
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
#math {
|
||||
font-size: 72px;
|
||||
}
|
||||
|
||||
|
@@ -1482,6 +1482,35 @@ describe("A font parser", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("A comment parser", function() {
|
||||
it("should parse comments at the end of a line", () => {
|
||||
expect("a^2 + b^2 = c^2 % Pythagoras' Theorem\n").toParse();
|
||||
});
|
||||
|
||||
it("should parse comments at the start of a line", () => {
|
||||
expect("% comment\n").toParse();
|
||||
});
|
||||
|
||||
it("should parse multiple lines of comments in a row", () => {
|
||||
expect("% comment 1\n% comment 2\n").toParse();
|
||||
});
|
||||
|
||||
it("should not parse a comment that isn't followed by a newline", () => {
|
||||
expect("x%y").toNotParse();
|
||||
});
|
||||
|
||||
it("should not produce or consume space", () => {
|
||||
expect("\text{hello% comment 1\nworld}")
|
||||
.toParseLike("\text{helloworld}");
|
||||
expect("\text{hello% comment\n\nworld}")
|
||||
.toParseLike("\text{hello world}");
|
||||
});
|
||||
|
||||
it("should not include comments in the output", () => {
|
||||
expect("5 % comment\n").toParseLike("5");
|
||||
});
|
||||
});
|
||||
|
||||
describe("An HTML font tree-builder", function() {
|
||||
it("should render \\mathbb{R} with the correct font", function() {
|
||||
const markup = katex.renderToString("\\mathbb{R}");
|
||||
|
Reference in New Issue
Block a user