Show More
@@ -9,11 +9,11 | |||
|
9 | 9 | // MathJax utility functions |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | "using strict"; | |
|
13 | 12 | |
|
14 | 13 | IPython.namespace('IPython.mathjaxutils'); |
|
15 | 14 | |
|
16 | 15 | IPython.mathjaxutils = (function (IPython) { |
|
16 | "use strict"; | |
|
17 | 17 | |
|
18 | 18 | var init = function () { |
|
19 | 19 | if (window.MathJax) { |
@@ -31,7 +31,7 IPython.mathjaxutils = (function (IPython) { | |||
|
31 | 31 | } |
|
32 | 32 | }); |
|
33 | 33 | MathJax.Hub.Configured(); |
|
34 | } else if (window.mathjax_url != "") { | |
|
34 | } else if (window.mathjax_url !== "") { | |
|
35 | 35 | // Don't have MathJax, but should. Show dialog. |
|
36 | 36 | var message = $('<div/>') |
|
37 | 37 | .append( |
@@ -71,7 +71,7 IPython.mathjaxutils = (function (IPython) { | |||
|
71 | 71 | $("<p></p>").addClass('dialog').html( |
|
72 | 72 | "which will prevent this dialog from appearing." |
|
73 | 73 | ) |
|
74 | ) | |
|
74 | ); | |
|
75 | 75 | IPython.dialog.modal({ |
|
76 | 76 | title : "Failed to retrieve MathJax from '" + window.mathjax_url + "'", |
|
77 | 77 | body : message, |
@@ -79,9 +79,7 IPython.mathjaxutils = (function (IPython) { | |||
|
79 | 79 | OK : {class: "btn-danger"} |
|
80 | 80 | } |
|
81 | 81 | }); |
|
82 |
} |
|
|
83 | // No MathJax, but none expected. No dialog. | |
|
84 | }; | |
|
82 | } | |
|
85 | 83 | }; |
|
86 | 84 | |
|
87 | 85 | // Some magic for deferring mathematical expressions to MathJax |
@@ -94,38 +92,36 IPython.mathjaxutils = (function (IPython) { | |||
|
94 | 92 | var inline = "$"; // the inline math delimiter |
|
95 | 93 | |
|
96 | 94 | // MATHSPLIT contains the pattern for math delimiters and special symbols |
|
97 |
// needed for searching for math in the text input. |
|
|
95 | // needed for searching for math in the text input. | |
|
98 | 96 | var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@)/i; |
|
99 | 97 | |
|
100 |
// The math is in blocks i through j, so |
|
|
98 | // The math is in blocks i through j, so | |
|
101 | 99 | // collect it into one block and clear the others. |
|
102 | 100 | // Replace &, <, and > by named entities. |
|
103 | 101 | // For IE, put <br> at the ends of comments since IE removes \n. |
|
104 | 102 | // Clear the current math positions and store the index of the |
|
105 | 103 | // math, then push the math string onto the storage array. |
|
106 | 104 | // The preProcess function is called on all blocks if it has been passed in |
|
107 |
var process_math = function (i, j, pre_process, math, blocks |
|
|
105 | var process_math = function (i, j, pre_process, math, blocks) { | |
|
108 | 106 | var hub = MathJax.Hub; |
|
109 | 107 | var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&") // use HTML entity for & |
|
110 | 108 | .replace(/</g, "<") // use HTML entity for < |
|
111 | 109 | .replace(/>/g, ">") // use HTML entity for > |
|
112 | 110 | ; |
|
113 | 111 | if (hub.Browser.isMSIE) { |
|
114 | block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n") | |
|
112 | block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n"); | |
|
115 | 113 | } |
|
116 | 114 | while (j > i) { |
|
117 | 115 | blocks[j] = ""; |
|
118 | 116 | j--; |
|
119 | 117 | } |
|
120 | 118 | blocks[i] = "@@" + math.length + "@@"; // replace the current block text with a unique tag to find later |
|
121 | if (pre_process) | |
|
119 | if (pre_process){ | |
|
122 | 120 | block = pre_process(block); |
|
121 | } | |
|
123 | 122 | math.push(block); |
|
124 | start = null; | |
|
125 | end = null; | |
|
126 | last = null; | |
|
127 | return [blocks, start, end, last] | |
|
128 | } | |
|
123 | return blocks; | |
|
124 | }; | |
|
129 | 125 | |
|
130 | 126 | // Break up the text into its component parts and search |
|
131 | 127 | // through them for math delimiters, braces, linebreaks, etc. |
@@ -142,7 +138,8 IPython.mathjaxutils = (function (IPython) { | |||
|
142 | 138 | var start; |
|
143 | 139 | var end; |
|
144 | 140 | var last; |
|
145 | ||
|
141 | var braces; | |
|
142 | ||
|
146 | 143 | // Except for extreme edge cases, this should catch precisely those pieces of the markdown |
|
147 | 144 | // source that will later be turned into code spans. While MathJax will not TeXify code spans, |
|
148 | 145 | // we still have to consider them at this point; the following issue has happened several times: |
@@ -155,11 +152,15 IPython.mathjaxutils = (function (IPython) { | |||
|
155 | 152 | text = text.replace(/~/g, "~T").replace(/(^|[^\\])(`+)([^\n]*?[^`\n])\2(?!`)/gm, function (wholematch) { |
|
156 | 153 | return wholematch.replace(/\$/g, "~D"); |
|
157 | 154 | }); |
|
158 | de_tilde = function (text) { return text.replace(/~([TD])/g, function (wholematch, character) { return { T: "~", D: "$" }[character]; }) }; | |
|
155 | de_tilde = function (text) { | |
|
156 | return text.replace(/~([TD])/g, function (wholematch, character) { | |
|
157 | return { T: "~", D: "$" }[character]; | |
|
158 | }); | |
|
159 | }; | |
|
159 | 160 | } else { |
|
160 | 161 | de_tilde = function (text) { return text; }; |
|
161 | 162 | } |
|
162 | ||
|
163 | ||
|
163 | 164 | var blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT); |
|
164 | 165 | |
|
165 | 166 | for (var i = 1, m = blocks.length; i < m; i += 2) { |
@@ -180,24 +181,19 IPython.mathjaxutils = (function (IPython) { | |||
|
180 | 181 | // |
|
181 | 182 | if (block === end) { |
|
182 | 183 | if (braces) { |
|
183 | last = i | |
|
184 | last = i; | |
|
184 | 185 | } |
|
185 | 186 | else { |
|
186 |
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
|
190 | last = res[3]; | |
|
187 | blocks = process_math(start, i, de_tilde, math, blocks); | |
|
188 | start = null; | |
|
189 | end = null; | |
|
190 | last = null; | |
|
191 | 191 | } |
|
192 | 192 | } |
|
193 | 193 | else if (block.match(/\n.*\n/)) { |
|
194 | 194 | if (last) { |
|
195 | 195 | i = last; |
|
196 |
|
|
|
197 | blocks = res[0]; | |
|
198 | start = res[1]; | |
|
199 | end = res[2]; | |
|
200 | last = res[3]; | |
|
196 | blocks = process_math(start, i, de_tilde, math, blocks); | |
|
201 | 197 | } |
|
202 | 198 | start = null; |
|
203 | 199 | end = null; |
@@ -205,10 +201,10 IPython.mathjaxutils = (function (IPython) { | |||
|
205 | 201 | braces = 0; |
|
206 | 202 | } |
|
207 | 203 | else if (block === "{") { |
|
208 | braces++ | |
|
204 | braces++; | |
|
209 | 205 | } |
|
210 | 206 | else if (block === "}" && braces) { |
|
211 | braces-- | |
|
207 | braces--; | |
|
212 | 208 | } |
|
213 | 209 | } |
|
214 | 210 | else { |
@@ -229,28 +225,27 IPython.mathjaxutils = (function (IPython) { | |||
|
229 | 225 | } |
|
230 | 226 | } |
|
231 | 227 | if (last) { |
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 | last = res[3]; | |
|
228 | blocks = process_math(start, last, de_tilde, math, blocks); | |
|
229 | start = null; | |
|
230 | end = null; | |
|
231 | last = null; | |
|
237 | 232 | } |
|
238 | 233 | return [de_tilde(blocks.join("")), math]; |
|
239 | } | |
|
234 | }; | |
|
240 | 235 | |
|
241 | 236 | // |
|
242 | 237 | // Put back the math strings that were saved, |
|
243 | 238 | // and clear the math array (no need to keep it around). |
|
244 |
// |
|
|
239 | // | |
|
245 | 240 | var replace_math = function (text, math) { |
|
246 | 241 | if (!window.MathJax) { |
|
247 | 242 | return text; |
|
248 | 243 | } |
|
249 | 244 | text = text.replace(/@@(\d+)@@/g, function (match, n) { |
|
250 | return math[n] | |
|
245 | return math[n]; | |
|
251 | 246 | }); |
|
252 | 247 | return text; |
|
253 | } | |
|
248 | }; | |
|
254 | 249 | |
|
255 | 250 | return { |
|
256 | 251 | init : init, |
General Comments 0
You need to be logged in to leave comments.
Login now