Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
'base/js/namespace',
6
'jquery',
7
'base/js/utils',
8
'base/js/dialog',
9
], function(IPython, $, utils, dialog) {
10
"use strict";
11
12
var init = function () {
13
if (window.MathJax) {
14
// MathJax loaded
15
MathJax.Hub.Config({
16
tex2jax: {
17
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
18
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
19
processEscapes: true,
20
processEnvironments: true
21
},
22
// Center justify equations in code and markdown cells. Elsewhere
23
// we use CSS to left justify single line equations in code cells.
24
displayAlign: 'center',
25
"HTML-CSS": {
26
styles: {'.MathJax_Display': {"margin": 0}},
27
linebreaks: { automatic: true }
28
}
29
});
30
MathJax.Hub.Configured();
31
} else if (window.mathjax_url !== "") {
32
// Don't have MathJax, but should. Show dialog.
33
var message = $('<div/>')
34
.append(
35
$("<p/></p>").addClass('dialog').text(
36
"Math/LaTeX rendering will be disabled."
37
)
38
).append(
39
$("<p></p>").addClass('dialog').text(
40
"If you have administrative access to the notebook server and" +
41
" a working internet connection, you can install a local copy" +
42
" of MathJax for offline use with the following command on the server" +
43
" at a Python or IPython prompt:"
44
)
45
).append(
46
$("<pre></pre>").addClass('dialog').text(
47
">>> from IPython.external import mathjax; mathjax.install_mathjax()"
48
)
49
).append(
50
$("<p></p>").addClass('dialog').text(
51
"This will try to install MathJax into the IPython source directory."
52
)
53
).append(
54
$("<p></p>").addClass('dialog').text(
55
"If IPython is installed to a location that requires" +
56
" administrative privileges to write, you will need to make this call as" +
57
" an administrator, via 'sudo'."
58
)
59
).append(
60
$("<p></p>").addClass('dialog').text(
61
"When you start the notebook server, you can instruct it to disable MathJax support altogether:"
62
)
63
).append(
64
$("<pre></pre>").addClass('dialog').text(
65
"$ ipython notebook --no-mathjax"
66
)
67
).append(
68
$("<p></p>").addClass('dialog').text(
69
"which will prevent this dialog from appearing."
70
)
71
);
72
dialog.modal({
73
title : "Failed to retrieve MathJax from '" + window.mathjax_url + "'",
74
body : message,
75
buttons : {
76
OK : {class: "btn-danger"}
77
}
78
});
79
}
80
};
81
82
// Some magic for deferring mathematical expressions to MathJax
83
// by hiding them from the Markdown parser.
84
// Some of the code here is adapted with permission from Davide Cervone
85
// under the terms of the Apache2 license governing the MathJax project.
86
// Other minor modifications are also due to StackExchange and are used with
87
// permission.
88
89
var inline = "$"; // the inline math delimiter
90
91
// MATHSPLIT contains the pattern for math delimiters and special symbols
92
// needed for searching for math in the text input.
93
var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@)/i;
94
95
// The math is in blocks i through j, so
96
// collect it into one block and clear the others.
97
// Replace &, <, and > by named entities.
98
// For IE, put <br> at the ends of comments since IE removes \n.
99
// Clear the current math positions and store the index of the
100
// math, then push the math string onto the storage array.
101
// The preProcess function is called on all blocks if it has been passed in
102
var process_math = function (i, j, pre_process, math, blocks) {
103
var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&amp;") // use HTML entity for &
104
.replace(/</g, "&lt;") // use HTML entity for <
105
.replace(/>/g, "&gt;") // use HTML entity for >
106
;
107
if (utils.browser === 'msie') {
108
block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n");
109
}
110
while (j > i) {
111
blocks[j] = "";
112
j--;
113
}
114
blocks[i] = "@@" + math.length + "@@"; // replace the current block text with a unique tag to find later
115
if (pre_process){
116
block = pre_process(block);
117
}
118
math.push(block);
119
return blocks;
120
};
121
122
// Break up the text into its component parts and search
123
// through them for math delimiters, braces, linebreaks, etc.
124
// Math delimiters must match and braces must balance.
125
// Don't allow math to pass through a double linebreak
126
// (which will be a paragraph).
127
//
128
var remove_math = function (text) {
129
var math = []; // stores math strings for later
130
var start;
131
var end;
132
var last;
133
var braces;
134
135
// Except for extreme edge cases, this should catch precisely those pieces of the markdown
136
// source that will later be turned into code spans. While MathJax will not TeXify code spans,
137
// we still have to consider them at this point; the following issue has happened several times:
138
//
139
// `$foo` and `$bar` are varibales. --> <code>$foo ` and `$bar</code> are variables.
140
141
var hasCodeSpans = /`/.test(text),
142
de_tilde;
143
if (hasCodeSpans) {
144
text = text.replace(/~/g, "~T").replace(/(^|[^\\])(`+)([^\n]*?[^`\n])\2(?!`)/gm, function (wholematch) {
145
return wholematch.replace(/\$/g, "~D");
146
});
147
de_tilde = function (text) {
148
return text.replace(/~([TD])/g, function (wholematch, character) {
149
return { T: "~", D: "$" }[character];
150
});
151
};
152
} else {
153
de_tilde = function (text) { return text; };
154
}
155
156
var blocks = utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT);
157
158
for (var i = 1, m = blocks.length; i < m; i += 2) {
159
var block = blocks[i];
160
if (block.charAt(0) === "@") {
161
//
162
// Things that look like our math markers will get
163
// stored and then retrieved along with the math.
164
//
165
blocks[i] = "@@" + math.length + "@@";
166
math.push(block);
167
}
168
else if (start) {
169
//
170
// If we are in math, look for the end delimiter,
171
// but don't go past double line breaks, and
172
// and balance braces within the math.
173
//
174
if (block === end) {
175
if (braces) {
176
last = i;
177
}
178
else {
179
blocks = process_math(start, i, de_tilde, math, blocks);
180
start = null;
181
end = null;
182
last = null;
183
}
184
}
185
else if (block.match(/\n.*\n/)) {
186
if (last) {
187
i = last;
188
blocks = process_math(start, i, de_tilde, math, blocks);
189
}
190
start = null;
191
end = null;
192
last = null;
193
braces = 0;
194
}
195
else if (block === "{") {
196
braces++;
197
}
198
else if (block === "}" && braces) {
199
braces--;
200
}
201
}
202
else {
203
//
204
// Look for math start delimiters and when
205
// found, set up the end delimiter.
206
//
207
if (block === inline || block === "$$") {
208
start = i;
209
end = block;
210
braces = 0;
211
}
212
else if (block.substr(1, 5) === "begin") {
213
start = i;
214
end = "\\end" + block.substr(6);
215
braces = 0;
216
}
217
}
218
}
219
if (last) {
220
blocks = process_math(start, last, de_tilde, math, blocks);
221
start = null;
222
end = null;
223
last = null;
224
}
225
return [de_tilde(blocks.join("")), math];
226
};
227
228
//
229
// Put back the math strings that were saved,
230
// and clear the math array (no need to keep it around).
231
//
232
var replace_math = function (text, math) {
233
text = text.replace(/@@(\d+)@@/g, function (match, n) {
234
return math[n];
235
});
236
return text;
237
};
238
239
var mathjaxutils = {
240
init : init,
241
remove_math : remove_math,
242
replace_math : replace_math
243
};
244
245
IPython.mathjaxutils = mathjaxutils;
246
247
return mathjaxutils;
248
});
249
250