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