Show More
@@ -78,8 +78,12 b' IPython.mathjaxutils = (function (IPython) {' | |||||
78 | }; |
|
78 | }; | |
79 | }; |
|
79 | }; | |
80 |
|
80 | |||
81 |
// Some magic for deferring mathematical expressions to MathJa |
|
81 | // Some magic for deferring mathematical expressions to MathJax | |
82 | // Some of the code here is adapted with permission from Stack Exchange Inc. |
|
82 | // by hiding them from the Markdown parser. | |
|
83 | // Some of the code here is adapted with permission from Davide Cervone | |||
|
84 | // under the terms of the Apache2 license governing the MathJax project. | |||
|
85 | // Other minor modifications are also due to StackExchange and are used with | |||
|
86 | // permission. | |||
83 |
|
87 | |||
84 | var inline = "$"; // the inline math delimiter |
|
88 | var inline = "$"; // the inline math delimiter | |
85 | var blocks, start, end, last, braces; // used in searching for math |
|
89 | var blocks, start, end, last, braces; // used in searching for math | |
@@ -97,7 +101,7 b' IPython.mathjaxutils = (function (IPython) {' | |||||
97 | // Clear the current math positions and store the index of the |
|
101 | // Clear the current math positions and store the index of the | |
98 | // math, then push the math string onto the storage array. |
|
102 | // math, then push the math string onto the storage array. | |
99 | // The preProcess function is called on all blocks if it has been passed in |
|
103 | // The preProcess function is called on all blocks if it has been passed in | |
100 |
function |
|
104 | var process_math = function (i, j, pre_process) { | |
101 | var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&") // use HTML entity for & |
|
105 | var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&") // use HTML entity for & | |
102 | .replace(/</g, "<") // use HTML entity for < |
|
106 | .replace(/</g, "<") // use HTML entity for < | |
103 | .replace(/>/g, ">") // use HTML entity for > |
|
107 | .replace(/>/g, ">") // use HTML entity for > | |
@@ -110,8 +114,8 b' IPython.mathjaxutils = (function (IPython) {' | |||||
110 | j--; |
|
114 | j--; | |
111 | } |
|
115 | } | |
112 | blocks[i] = "@@" + math.length + "@@"; // replace the current block text with a unique tag to find later |
|
116 | blocks[i] = "@@" + math.length + "@@"; // replace the current block text with a unique tag to find later | |
113 |
if (pre |
|
117 | if (pre_process) | |
114 |
block = pre |
|
118 | block = pre_process(block); | |
115 | math.push(block); |
|
119 | math.push(block); | |
116 | start = end = last = null; |
|
120 | start = end = last = null; | |
117 | } |
|
121 | } | |
@@ -122,7 +126,7 b' IPython.mathjaxutils = (function (IPython) {' | |||||
122 | // Don't allow math to pass through a double linebreak |
|
126 | // Don't allow math to pass through a double linebreak | |
123 | // (which will be a paragraph). |
|
127 | // (which will be a paragraph). | |
124 | // |
|
128 | // | |
125 |
function |
|
129 | var remove_math = function (text) { | |
126 | start = end = last = null; // for tracking math delimiters |
|
130 | start = end = last = null; // for tracking math delimiters | |
127 | math = []; // stores math strings for later |
|
131 | math = []; // stores math strings for later | |
128 |
|
132 | |||
@@ -133,14 +137,14 b' IPython.mathjaxutils = (function (IPython) {' | |||||
133 | // `$foo` and `$bar` are varibales. --> <code>$foo ` and `$bar</code> are variables. |
|
137 | // `$foo` and `$bar` are varibales. --> <code>$foo ` and `$bar</code> are variables. | |
134 |
|
138 | |||
135 | var hasCodeSpans = /`/.test(text), |
|
139 | var hasCodeSpans = /`/.test(text), | |
136 |
de |
|
140 | de_tilde; | |
137 | if (hasCodeSpans) { |
|
141 | if (hasCodeSpans) { | |
138 | text = text.replace(/~/g, "~T").replace(/(^|[^\\])(`+)([^\n]*?[^`\n])\2(?!`)/gm, function (wholematch) { |
|
142 | text = text.replace(/~/g, "~T").replace(/(^|[^\\])(`+)([^\n]*?[^`\n])\2(?!`)/gm, function (wholematch) { | |
139 | return wholematch.replace(/\$/g, "~D"); |
|
143 | return wholematch.replace(/\$/g, "~D"); | |
140 | }); |
|
144 | }); | |
141 |
de |
|
145 | de_tilde = function (text) { return text.replace(/~([TD])/g, function (wholematch, character) { return { T: "~", D: "$" }[character]; }) }; | |
142 | } else { |
|
146 | } else { | |
143 |
de |
|
147 | de_tilde = function (text) { return text; }; | |
144 | } |
|
148 | } | |
145 |
|
149 | |||
146 | blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT); |
|
150 | blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT); | |
@@ -166,13 +170,13 b' IPython.mathjaxutils = (function (IPython) {' | |||||
166 | last = i |
|
170 | last = i | |
167 | } |
|
171 | } | |
168 | else { |
|
172 | else { | |
169 |
process |
|
173 | process_math(start, i, de_tilde) | |
170 | } |
|
174 | } | |
171 | } |
|
175 | } | |
172 | else if (block.match(/\n.*\n/)) { |
|
176 | else if (block.match(/\n.*\n/)) { | |
173 | if (last) { |
|
177 | if (last) { | |
174 | i = last; |
|
178 | i = last; | |
175 |
process |
|
179 | process_math(start, i, de_tilde) | |
176 | } |
|
180 | } | |
177 | start = end = last = null; |
|
181 | start = end = last = null; | |
178 | braces = 0; |
|
182 | braces = 0; | |
@@ -202,16 +206,16 b' IPython.mathjaxutils = (function (IPython) {' | |||||
202 | } |
|
206 | } | |
203 | } |
|
207 | } | |
204 | if (last) { |
|
208 | if (last) { | |
205 |
process |
|
209 | process_math(start, last, de_tilde) | |
206 | } |
|
210 | } | |
207 |
return de |
|
211 | return de_tilde(blocks.join("")); | |
208 | } |
|
212 | } | |
209 |
|
213 | |||
210 | // |
|
214 | // | |
211 | // Put back the math strings that were saved, |
|
215 | // Put back the math strings that were saved, | |
212 | // and clear the math array (no need to keep it around). |
|
216 | // and clear the math array (no need to keep it around). | |
213 | // |
|
217 | // | |
214 | function replaceMath(text) { |
|
218 | var replace_math = function (text) { | |
215 | text = text.replace(/@@(\d+)@@/g, function (match, n) { |
|
219 | text = text.replace(/@@(\d+)@@/g, function (match, n) { | |
216 | return math[n] |
|
220 | return math[n] | |
217 | }); |
|
221 | }); | |
@@ -219,7 +223,7 b' IPython.mathjaxutils = (function (IPython) {' | |||||
219 | return text; |
|
223 | return text; | |
220 | } |
|
224 | } | |
221 |
|
225 | |||
222 | function queueRender() { |
|
226 | var queue_render = function () { | |
223 | // see https://groups.google.com/forum/?fromgroups=#!topic/mathjax-users/cpwy5eCH1ZQ |
|
227 | // see https://groups.google.com/forum/?fromgroups=#!topic/mathjax-users/cpwy5eCH1ZQ | |
224 | MathJax.Hub.Queue( |
|
228 | MathJax.Hub.Queue( | |
225 | ["resetEquationNumbers",MathJax.InputJax.TeX], |
|
229 | ["resetEquationNumbers",MathJax.InputJax.TeX], | |
@@ -230,10 +234,10 b' IPython.mathjaxutils = (function (IPython) {' | |||||
230 |
|
234 | |||
231 | return { |
|
235 | return { | |
232 | init : init, |
|
236 | init : init, | |
233 |
process |
|
237 | process_math : process_math, | |
234 |
remove |
|
238 | remove_math : remove_math, | |
235 |
replace |
|
239 | replace_math : replace_math, | |
236 |
queue |
|
240 | queue_render : queue_render | |
237 | }; |
|
241 | }; | |
238 |
|
242 | |||
239 | }(IPython)); No newline at end of file |
|
243 | }(IPython)); |
@@ -222,9 +222,9 b' var IPython = (function (IPython) {' | |||||
222 | var text = this.get_text(); |
|
222 | var text = this.get_text(); | |
223 | if (text === "") { text = this.placeholder; } |
|
223 | if (text === "") { text = this.placeholder; } | |
224 |
|
224 | |||
225 |
text = IPython.mathjaxutils.remove |
|
225 | text = IPython.mathjaxutils.remove_math(text) | |
226 | var html = IPython.markdown_converter.makeHtml(text); |
|
226 | var html = IPython.markdown_converter.makeHtml(text); | |
227 |
html = IPython.mathjaxutils.replace |
|
227 | html = IPython.mathjaxutils.replace_math(html) | |
228 |
|
228 | |||
229 | try { |
|
229 | try { | |
230 | this.set_rendered(html); |
|
230 | this.set_rendered(html); | |
@@ -251,7 +251,7 b' var IPython = (function (IPython) {' | |||||
251 | return '<code class="prettyprint">' + code + '</code>'; |
|
251 | return '<code class="prettyprint">' + code + '</code>'; | |
252 | }); |
|
252 | }); | |
253 |
|
253 | |||
254 |
IPython.mathjaxutils.queue |
|
254 | IPython.mathjaxutils.queue_render() | |
255 | this.rendered = true; |
|
255 | this.rendered = true; | |
256 | } |
|
256 | } | |
257 | }; |
|
257 | }; |
General Comments 0
You need to be logged in to leave comments.
Login now