This repository was archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrapper.js
170 lines (140 loc) · 5.67 KB
/
wrapper.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*\
title: $:/plugins/padawanphysicist/tw5-mathdown/wrapper.js
type: application/javascript
module-type: parser
Wraps up markdown parser for use in TiddlyWiki5
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
// Load main libraries
var Markdown = require("$:/plugins/padawanphysicist/tw5-mathdown/markdown-it.js");
var TeXZilla = require("$:/plugins/padawanphysicist/tw5-mathdown/TeXZilla.js");
var md; // Store markdown parser instance
/** Function count the occurrences of substring in a string;
* @param {String} string Required. The string;
* @param {String} subString Required. The string to search for;
* @param {Boolean} allowOverlapping Optional. Default: false;
*/
function occurrences(string, subString, allowOverlapping){
string+=""; subString+="";
if(subString.length<=0) return string.length+1;
var n=0, pos=0;
var step=(allowOverlapping)?(1):(subString.length);
while(true){
pos=string.indexOf(subString,pos);
if(pos>=0){ n++; pos+=step; } else break;
}
return(n);
}
// Regular expressions contain special (meta) characters, and as such it is dangerous to blindly pass an argument in the find function above without pre-processing it to escape those characters. This is covered in the Mozilla Developer Network's JavaScript Guide on Regular Expressions, where they present the following utility function:
function escapeRegExp(string) {
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
function replaceAll(string, find, replace) {
return string.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
/*
This function parse the equations, turning them into html tags
*/
//function preParse(text, newMacros) {
function preParse(text) {
var txt = text;
//var ncmds = newMacros.length-1;
if(!!txt) {
// Parse Internal tiddler links
var re = /(?:\[(.*?)\]\((.*?)\))/gm;
txt = txt.replace(re,function(match,text) {
var linkName = match.match(/(?:\((.*)\))/g);
linkName = linkName[0].substr(1,(linkName[0].length-2));
//console.log("parsing link... " + linkName + ", with text " + text);
return "<a href=\"" + linkName + "\">" + text + "</a>";
});
// Parse new TeX commands
//if (ncmds > 0) {
// for(var i=0;i<ncmds;i++){
// var cmdArray = newMacros[i].split("\t");
// if(cmdArray[1] == 0) { // Check for no-argument command
// txt = replaceAll(txt,cmdArray[0],cmdArray[2]);
// }
// else if(cmdArray[1] == 1){
// //console.log("found one argument command " + cmdArray[0])
// var cmdStr = cmdArray[0].replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
// var re = new RegExp(cmdStr + '{(.*?)}', 'g');
// txt = txt.replace(re,function(match,text) {
// return cmdArray[2].replace('#1',text);
// });
// }
// }
//}
// Displayed equation using \begin{equation} LaTeX environment \end{equation}
var re = /([^\\]\\begin\{equation\}(?:\\.|[\s\S])*?\\end\{equation\})/gm;
txt = txt.replace(re,function(match,text) {
var tmp = text.replace("\\begin{equation}","").replace("\\end{equation}","").replace(/\r?\n|\r/g,"");
// Parse label
var labelRegexp = /(?:\\label\{(.*?)\})/;
var label = ""
if(labelRegexp.test(tmp)) {
var match = labelRegexp.exec(tmp);
label = match[1];
tmp = tmp.replace("\\label\{" + label + "\}",""); // Now that we have the label, we can delete from string
console.log(label);
}
var ml = TeXZilla.toMathMLString(tmp,true);
if(label!=="") { // Additional markup for labeling
var innerMathRegex = /(?:\<math.*?\>(.*)\<\/math\>)/;
if(innerMathRegex.test(ml)) {
var match = innerMathRegex.exec(ml);
ml = match[1];
}
ml = "<div id=\'" + label + "\'><math xmlns=\'http:\/\/www.w3.org/1998/Math/MathML\' display=\'block\'><mtable side=\'right\'><mlabeledtr><mtd>"+ ml + "</mtd></mlabeledtr></mtable></math></div>";
}
else {
ml = "<math xmlns=\'http:\/\/www.w3.org/1998/Math/MathML\' display=\'block\'><mtable side=\'right\'><mlabeledtr><mtd>"+ ml + "</mtd></mlabeledtr></mtable></math>";
}
return ml;
});
// Displayed (unumbered) equation using \begin{equation*} LaTeX environment \end{equation*}
var re = /([^\\]\\begin\{equation\*\}(?:\\.|[\s\S])*?\\end\{equation\*\})/gm;
txt = txt.replace(re,function(match,text) {
var tmp = text.replace("\\begin{equation*}","").replace("\\end{equation*}","").replace(/\r?\n|\r/g,"");
var ml = TeXZilla.toMathMLString(tmp,true);
return ml;
});
// Displayed equation using \[ square brackets \]
var re = /([^\\]\\\[(?:\\.|[\s\S])*?\\\])/gm;
txt = txt.replace(re,function(match,text) {
var tmp = text.replace("\\[","").replace("\\]","").replace(/\r?\n|\r/g,"");
var ml = TeXZilla.toMathMLString(tmp,true);
return ml;
});
// Inline equation using $single dollars$
var re = /(?:\$)([^\$\s]{0}[^\$]*?[^\$\\\s])(?:\$[^\$]{0})/gm;
txt = txt.replace(re,function(match,text) {
var ml = TeXZilla.toMathMLString(text,false);
return ml;
});
}
return txt;
}
var MarkdownParser = function(type,text,options) {
var preParsedText,htmlText;
//var newMacrosTiddlerStr = options.wiki.getTiddlerText(CONFIG_MACRO_TIDDLER,DEFAULT_CMD);
// To count number of commands
//var ncmds = occurrences(newMacrosTiddlerStr, "\n");
//var newMacros = newMacrosTiddlerStr.split("\n");
md = new Markdown();
// Set Markdown options here
md.set({html: true});
// Parse additional rules before markdown (I hope this is just a
// workaround while learning how to insert rules properly on
// remarkable parser)
//preParsedText = preParse(text, newMacros);
preParsedText = preParse(text);
// Parse Markdown
htmlText = md.render(preParsedText);
this.tree = [{type: "raw", html: htmlText}];
};
exports["text/x-mathdown"] = MarkdownParser;
})();