##// END OF EJS Templates
statelessify matjaxutils...
Matthias BUSSONNIER -
Show More
@@ -9,6 +9,8 b''
9 // MathJax utility functions
9 // MathJax utility functions
10 //============================================================================
10 //============================================================================
11
11
12 "using strict";
13
12 IPython.namespace('IPython.mathjaxutils');
14 IPython.namespace('IPython.mathjaxutils');
13
15
14 IPython.mathjaxutils = (function (IPython) {
16 IPython.mathjaxutils = (function (IPython) {
@@ -90,8 +92,6 b' IPython.mathjaxutils = (function (IPython) {'
90 // permission.
92 // permission.
91
93
92 var inline = "$"; // the inline math delimiter
94 var inline = "$"; // the inline math delimiter
93 var blocks, start, end, last, braces; // used in searching for math
94 var math; // stores math until pagedown (Markdown parser) is done
95
95
96 // MATHSPLIT contains the pattern for math delimiters and special symbols
96 // MATHSPLIT contains the pattern for math delimiters and special symbols
97 // needed for searching for math in the text input.
97 // needed for searching for math in the text input.
@@ -104,7 +104,7 b' IPython.mathjaxutils = (function (IPython) {'
104 // Clear the current math positions and store the index of the
104 // Clear the current math positions and store the index of the
105 // math, then push the math string onto the storage array.
105 // math, then push the math string onto the storage array.
106 // The preProcess function is called on all blocks if it has been passed in
106 // The preProcess function is called on all blocks if it has been passed in
107 var process_math = function (i, j, pre_process) {
107 var process_math = function (i, j, pre_process, math, blocks, start, end, last) {
108 var hub = MathJax.Hub;
108 var hub = MathJax.Hub;
109 var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&") // use HTML entity for &
109 var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&") // use HTML entity for &
110 .replace(/</g, "&lt;") // use HTML entity for <
110 .replace(/</g, "&lt;") // use HTML entity for <
@@ -121,7 +121,10 b' IPython.mathjaxutils = (function (IPython) {'
121 if (pre_process)
121 if (pre_process)
122 block = pre_process(block);
122 block = pre_process(block);
123 math.push(block);
123 math.push(block);
124 start = end = last = null;
124 start = null;
125 end = null;
126 last = null;
127 return [blocks, start, end, last]
125 }
128 }
126
129
127 // Break up the text into its component parts and search
130 // Break up the text into its component parts and search
@@ -135,8 +138,10 b' IPython.mathjaxutils = (function (IPython) {'
135 return text;
138 return text;
136 }
139 }
137
140
138 start = end = last = null; // for tracking math delimiters
141 var math = []; // stores math strings for later
139 math = []; // stores math strings for later
142 var start;
143 var end;
144 var last;
140
145
141 // Except for extreme edge cases, this should catch precisely those pieces of the markdown
146 // Except for extreme edge cases, this should catch precisely those pieces of the markdown
142 // source that will later be turned into code spans. While MathJax will not TeXify code spans,
147 // source that will later be turned into code spans. While MathJax will not TeXify code spans,
@@ -155,7 +160,7 b' IPython.mathjaxutils = (function (IPython) {'
155 de_tilde = function (text) { return text; };
160 de_tilde = function (text) { return text; };
156 }
161 }
157
162
158 blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT);
163 var blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT);
159
164
160 for (var i = 1, m = blocks.length; i < m; i += 2) {
165 for (var i = 1, m = blocks.length; i < m; i += 2) {
161 var block = blocks[i];
166 var block = blocks[i];
@@ -178,15 +183,25 b' IPython.mathjaxutils = (function (IPython) {'
178 last = i
183 last = i
179 }
184 }
180 else {
185 else {
181 process_math(start, i, de_tilde)
186 var res = process_math(start, i, de_tilde, math, blocks, start, end, last);
187 blocks = res[0];
188 start = res[1];
189 end = res[2];
190 last = res[3];
182 }
191 }
183 }
192 }
184 else if (block.match(/\n.*\n/)) {
193 else if (block.match(/\n.*\n/)) {
185 if (last) {
194 if (last) {
186 i = last;
195 i = last;
187 process_math(start, i, de_tilde)
196 var res = process_math(start, i, de_tilde, math, blocks, start, end, last);
197 blocks = res[0];
198 start = res[1];
199 end = res[2];
200 last = res[3];
188 }
201 }
189 start = end = last = null;
202 start = null;
203 end = null;
204 last = null;
190 braces = 0;
205 braces = 0;
191 }
206 }
192 else if (block === "{") {
207 else if (block === "{") {
@@ -214,32 +229,33 b' IPython.mathjaxutils = (function (IPython) {'
214 }
229 }
215 }
230 }
216 if (last) {
231 if (last) {
217 process_math(start, last, de_tilde)
232 var res = process_math(start, last, de_tilde, math, blocks, start, end, last);
233 blocks = res[0];
234 start = res[1]
235 end = res[2];
236 last = res[3];
218 }
237 }
219 return de_tilde(blocks.join(""));
238 return [de_tilde(blocks.join("")), math];
220 }
239 }
221
240
222 //
241 //
223 // Put back the math strings that were saved,
242 // Put back the math strings that were saved,
224 // and clear the math array (no need to keep it around).
243 // and clear the math array (no need to keep it around).
225 //
244 //
226 var replace_math = function (text) {
245 var replace_math = function (text, math) {
227 if (!window.MathJax) {
246 if (!window.MathJax) {
228 return text;
247 return text;
229 }
248 }
230
231 text = text.replace(/@@(\d+)@@/g, function (match, n) {
249 text = text.replace(/@@(\d+)@@/g, function (match, n) {
232 return math[n]
250 return math[n]
233 });
251 });
234 math = null;
235 return text;
252 return text;
236 }
253 }
237
254
238 return {
255 return {
239 init : init,
256 init : init,
240 process_math : process_math,
241 remove_math : remove_math,
257 remove_math : remove_math,
242 replace_math : replace_math
258 replace_math : replace_math
243 };
259 };
244
260
245 }(IPython)); No newline at end of file
261 }(IPython));
@@ -315,9 +315,11 b' var IPython = (function (IPython) {'
315 if (this.rendered === false) {
315 if (this.rendered === false) {
316 var text = this.get_text();
316 var text = this.get_text();
317 if (text === "") { text = this.placeholder; }
317 if (text === "") { text = this.placeholder; }
318 text = IPython.mathjaxutils.remove_math(text);
318 text_math = IPython.mathjaxutils.remove_math(text);
319 text = text_math[0]
320 math = text_math[1]
319 var html = marked.parser(marked.lexer(text));
321 var html = marked.parser(marked.lexer(text));
320 html = $(IPython.mathjaxutils.replace_math(html));
322 html = $(IPython.mathjaxutils.replace_math(html, math));
321 // links in markdown cells should open in new tabs
323 // links in markdown cells should open in new tabs
322 html.find("a[href]").attr("target", "_blank");
324 html.find("a[href]").attr("target", "_blank");
323 try {
325 try {
General Comments 0
You need to be logged in to leave comments. Login now