-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex-parser.js
81 lines (70 loc) · 1.79 KB
/
latex-parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*\
title: $:/plugins/joerenes/TW5-TexZilla/latex-parser.js
type: application/javascript
module-type: wikirule
Wiki text rule for LaTeX, both inline and display. For example:
```
$$latex-goes-here$$ inline
\[more-latex\] display
```
This wikiparser can be modified using the rules eg:
```
\rules except latex-parser
\rules only latex-parser
```
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
//var displayopen = '\\\[',
// displayclose = '\\\]',
// inlinestring = '\$\$';
exports.name = "latex-parser";
exports.types = {inline: true}; // this is confusing; but this inline is setting the form of this wikitext parser.
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /\\\[|\$\$/mg; // just escape regexp... new RegExp(displayopen + '|' + inlinestring,'mg');
};
exports.parse = function() {
// figure out which delimiter we're dealing with. the result of the first regex from init is stored in this.match
var openmatch = this.match[0],
displaystyle,
reEnd;
if(openmatch == '\$\$') {
displaystyle = "inline";
reEnd = /\$\$/mg;
} else {
displaystyle = "block";
reEnd = /\\\]/mg;
}
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
// Look for the end marker
reEnd.lastIndex = this.parser.pos;
var match = reEnd.exec(this.parser.source),
text;
// Process the text
if(match) {
text = this.parser.source.substring(this.parser.pos,match.index);
this.parser.pos = match.index + match[0].length;
} else {
text = this.parser.source.substr(this.parser.pos);
this.parser.pos = this.parser.sourceLength;
}
return [{
type: "latex",
attributes: {
text: {
type: "text",
value: text
},
style: {
type: "text",
value: displaystyle
}
}
}];
};
})();