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