Show More
@@ -9,6 +9,8 b'' | |||
|
9 | 9 | // MathJax utility functions |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | "using strict"; | |
|
13 | ||
|
12 | 14 | IPython.namespace('IPython.mathjaxutils'); |
|
13 | 15 | |
|
14 | 16 | IPython.mathjaxutils = (function (IPython) { |
@@ -90,8 +92,6 b' IPython.mathjaxutils = (function (IPython) {' | |||
|
90 | 92 | // permission. |
|
91 | 93 | |
|
92 | 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 | 96 | // MATHSPLIT contains the pattern for math delimiters and special symbols |
|
97 | 97 | // needed for searching for math in the text input. |
@@ -104,7 +104,7 b' IPython.mathjaxutils = (function (IPython) {' | |||
|
104 | 104 | // Clear the current math positions and store the index of the |
|
105 | 105 | // math, then push the math string onto the storage array. |
|
106 | 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 | 108 | var hub = MathJax.Hub; |
|
109 | 109 | var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&") // use HTML entity for & |
|
110 | 110 | .replace(/</g, "<") // use HTML entity for < |
@@ -121,7 +121,10 b' IPython.mathjaxutils = (function (IPython) {' | |||
|
121 | 121 | if (pre_process) |
|
122 | 122 | block = pre_process(block); |
|
123 | 123 | math.push(block); |
|
124 |
start |
|
|
124 | start = null; | |
|
125 | end = null; | |
|
126 | last = null; | |
|
127 | return [blocks, start, end, last] | |
|
125 | 128 | } |
|
126 | 129 | |
|
127 | 130 | // Break up the text into its component parts and search |
@@ -135,8 +138,10 b' IPython.mathjaxutils = (function (IPython) {' | |||
|
135 | 138 | return text; |
|
136 | 139 | } |
|
137 | 140 | |
|
138 | start = end = last = null; // for tracking math delimiters | |
|
139 | math = []; // stores math strings for later | |
|
141 | var math = []; // stores math strings for later | |
|
142 | var start; | |
|
143 | var end; | |
|
144 | var last; | |
|
140 | 145 | |
|
141 | 146 | // Except for extreme edge cases, this should catch precisely those pieces of the markdown |
|
142 | 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 | 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 | 165 | for (var i = 1, m = blocks.length; i < m; i += 2) { |
|
161 | 166 | var block = blocks[i]; |
@@ -178,15 +183,25 b' IPython.mathjaxutils = (function (IPython) {' | |||
|
178 | 183 | last = i |
|
179 | 184 | } |
|
180 | 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 | 193 | else if (block.match(/\n.*\n/)) { |
|
185 | 194 | if (last) { |
|
186 | 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 |
|
|
202 | start = null; | |
|
203 | end = null; | |
|
204 | last = null; | |
|
190 | 205 | braces = 0; |
|
191 | 206 | } |
|
192 | 207 | else if (block === "{") { |
@@ -214,32 +229,33 b' IPython.mathjaxutils = (function (IPython) {' | |||
|
214 | 229 | } |
|
215 | 230 | } |
|
216 | 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 | 242 | // Put back the math strings that were saved, |
|
224 | 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 | 246 | if (!window.MathJax) { |
|
228 | 247 | return text; |
|
229 | 248 | } |
|
230 | ||
|
231 | 249 | text = text.replace(/@@(\d+)@@/g, function (match, n) { |
|
232 | 250 | return math[n] |
|
233 | 251 | }); |
|
234 | math = null; | |
|
235 | 252 | return text; |
|
236 | 253 | } |
|
237 | 254 | |
|
238 | 255 | return { |
|
239 | 256 | init : init, |
|
240 | process_math : process_math, | |
|
241 | 257 | remove_math : remove_math, |
|
242 | 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 | 315 | if (this.rendered === false) { |
|
316 | 316 | var text = this.get_text(); |
|
317 | 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 | 321 | var html = marked.parser(marked.lexer(text)); |
|
320 | html = $(IPython.mathjaxutils.replace_math(html)); | |
|
322 | html = $(IPython.mathjaxutils.replace_math(html, math)); | |
|
321 | 323 | // links in markdown cells should open in new tabs |
|
322 | 324 | html.find("a[href]").attr("target", "_blank"); |
|
323 | 325 | try { |
General Comments 0
You need to be logged in to leave comments.
Login now