Show More
@@ -1,423 +1,436 | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008-2012 The IPython Development Team |
|
2 | // Copyright (C) 2008-2012 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // Utilities |
|
9 | // Utilities | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 | IPython.namespace('IPython.utils'); |
|
11 | IPython.namespace('IPython.utils'); | |
12 |
|
12 | |||
13 | IPython.utils = (function (IPython) { |
|
13 | IPython.utils = (function (IPython) { | |
14 | "use strict"; |
|
14 | "use strict"; | |
15 |
|
15 | |||
16 | //============================================================================ |
|
16 | //============================================================================ | |
17 | // Cross-browser RegEx Split |
|
17 | // Cross-browser RegEx Split | |
18 | //============================================================================ |
|
18 | //============================================================================ | |
19 |
|
19 | |||
20 | // This code has been MODIFIED from the code licensed below to not replace the |
|
20 | // This code has been MODIFIED from the code licensed below to not replace the | |
21 | // default browser split. The license is reproduced here. |
|
21 | // default browser split. The license is reproduced here. | |
22 |
|
22 | |||
23 | // see http://blog.stevenlevithan.com/archives/cross-browser-split for more info: |
|
23 | // see http://blog.stevenlevithan.com/archives/cross-browser-split for more info: | |
24 | /*! |
|
24 | /*! | |
25 | * Cross-Browser Split 1.1.1 |
|
25 | * Cross-Browser Split 1.1.1 | |
26 | * Copyright 2007-2012 Steven Levithan <stevenlevithan.com> |
|
26 | * Copyright 2007-2012 Steven Levithan <stevenlevithan.com> | |
27 | * Available under the MIT License |
|
27 | * Available under the MIT License | |
28 | * ECMAScript compliant, uniform cross-browser split method |
|
28 | * ECMAScript compliant, uniform cross-browser split method | |
29 | */ |
|
29 | */ | |
30 |
|
30 | |||
31 | /** |
|
31 | /** | |
32 | * Splits a string into an array of strings using a regex or string |
|
32 | * Splits a string into an array of strings using a regex or string | |
33 | * separator. Matches of the separator are not included in the result array. |
|
33 | * separator. Matches of the separator are not included in the result array. | |
34 | * However, if `separator` is a regex that contains capturing groups, |
|
34 | * However, if `separator` is a regex that contains capturing groups, | |
35 | * backreferences are spliced into the result each time `separator` is |
|
35 | * backreferences are spliced into the result each time `separator` is | |
36 | * matched. Fixes browser bugs compared to the native |
|
36 | * matched. Fixes browser bugs compared to the native | |
37 | * `String.prototype.split` and can be used reliably cross-browser. |
|
37 | * `String.prototype.split` and can be used reliably cross-browser. | |
38 | * @param {String} str String to split. |
|
38 | * @param {String} str String to split. | |
39 | * @param {RegExp|String} separator Regex or string to use for separating |
|
39 | * @param {RegExp|String} separator Regex or string to use for separating | |
40 | * the string. |
|
40 | * the string. | |
41 | * @param {Number} [limit] Maximum number of items to include in the result |
|
41 | * @param {Number} [limit] Maximum number of items to include in the result | |
42 | * array. |
|
42 | * array. | |
43 | * @returns {Array} Array of substrings. |
|
43 | * @returns {Array} Array of substrings. | |
44 | * @example |
|
44 | * @example | |
45 | * |
|
45 | * | |
46 | * // Basic use |
|
46 | * // Basic use | |
47 | * regex_split('a b c d', ' '); |
|
47 | * regex_split('a b c d', ' '); | |
48 | * // -> ['a', 'b', 'c', 'd'] |
|
48 | * // -> ['a', 'b', 'c', 'd'] | |
49 | * |
|
49 | * | |
50 | * // With limit |
|
50 | * // With limit | |
51 | * regex_split('a b c d', ' ', 2); |
|
51 | * regex_split('a b c d', ' ', 2); | |
52 | * // -> ['a', 'b'] |
|
52 | * // -> ['a', 'b'] | |
53 | * |
|
53 | * | |
54 | * // Backreferences in result array |
|
54 | * // Backreferences in result array | |
55 | * regex_split('..word1 word2..', /([a-z]+)(\d+)/i); |
|
55 | * regex_split('..word1 word2..', /([a-z]+)(\d+)/i); | |
56 | * // -> ['..', 'word', '1', ' ', 'word', '2', '..'] |
|
56 | * // -> ['..', 'word', '1', ' ', 'word', '2', '..'] | |
57 | */ |
|
57 | */ | |
58 | var regex_split = function (str, separator, limit) { |
|
58 | var regex_split = function (str, separator, limit) { | |
59 | // If `separator` is not a regex, use `split` |
|
59 | // If `separator` is not a regex, use `split` | |
60 | if (Object.prototype.toString.call(separator) !== "[object RegExp]") { |
|
60 | if (Object.prototype.toString.call(separator) !== "[object RegExp]") { | |
61 | return split.call(str, separator, limit); |
|
61 | return split.call(str, separator, limit); | |
62 | } |
|
62 | } | |
63 | var output = [], |
|
63 | var output = [], | |
64 | flags = (separator.ignoreCase ? "i" : "") + |
|
64 | flags = (separator.ignoreCase ? "i" : "") + | |
65 | (separator.multiline ? "m" : "") + |
|
65 | (separator.multiline ? "m" : "") + | |
66 | (separator.extended ? "x" : "") + // Proposed for ES6 |
|
66 | (separator.extended ? "x" : "") + // Proposed for ES6 | |
67 | (separator.sticky ? "y" : ""), // Firefox 3+ |
|
67 | (separator.sticky ? "y" : ""), // Firefox 3+ | |
68 | lastLastIndex = 0, |
|
68 | lastLastIndex = 0, | |
69 | // Make `global` and avoid `lastIndex` issues by working with a copy |
|
69 | // Make `global` and avoid `lastIndex` issues by working with a copy | |
70 | separator = new RegExp(separator.source, flags + "g"), |
|
70 | separator = new RegExp(separator.source, flags + "g"), | |
71 | separator2, match, lastIndex, lastLength; |
|
71 | separator2, match, lastIndex, lastLength; | |
72 | str += ""; // Type-convert |
|
72 | str += ""; // Type-convert | |
73 |
|
73 | |||
74 | var compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined"; |
|
74 | var compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined"; | |
75 | if (!compliantExecNpcg) { |
|
75 | if (!compliantExecNpcg) { | |
76 | // Doesn't need flags gy, but they don't hurt |
|
76 | // Doesn't need flags gy, but they don't hurt | |
77 | separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); |
|
77 | separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); | |
78 | } |
|
78 | } | |
79 | /* Values for `limit`, per the spec: |
|
79 | /* Values for `limit`, per the spec: | |
80 | * If undefined: 4294967295 // Math.pow(2, 32) - 1 |
|
80 | * If undefined: 4294967295 // Math.pow(2, 32) - 1 | |
81 | * If 0, Infinity, or NaN: 0 |
|
81 | * If 0, Infinity, or NaN: 0 | |
82 | * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; |
|
82 | * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; | |
83 | * If negative number: 4294967296 - Math.floor(Math.abs(limit)) |
|
83 | * If negative number: 4294967296 - Math.floor(Math.abs(limit)) | |
84 | * If other: Type-convert, then use the above rules |
|
84 | * If other: Type-convert, then use the above rules | |
85 | */ |
|
85 | */ | |
86 | limit = typeof(limit) === "undefined" ? |
|
86 | limit = typeof(limit) === "undefined" ? | |
87 | -1 >>> 0 : // Math.pow(2, 32) - 1 |
|
87 | -1 >>> 0 : // Math.pow(2, 32) - 1 | |
88 | limit >>> 0; // ToUint32(limit) |
|
88 | limit >>> 0; // ToUint32(limit) | |
89 | while (match = separator.exec(str)) { |
|
89 | while (match = separator.exec(str)) { | |
90 | // `separator.lastIndex` is not reliable cross-browser |
|
90 | // `separator.lastIndex` is not reliable cross-browser | |
91 | lastIndex = match.index + match[0].length; |
|
91 | lastIndex = match.index + match[0].length; | |
92 | if (lastIndex > lastLastIndex) { |
|
92 | if (lastIndex > lastLastIndex) { | |
93 | output.push(str.slice(lastLastIndex, match.index)); |
|
93 | output.push(str.slice(lastLastIndex, match.index)); | |
94 | // Fix browsers whose `exec` methods don't consistently return `undefined` for |
|
94 | // Fix browsers whose `exec` methods don't consistently return `undefined` for | |
95 | // nonparticipating capturing groups |
|
95 | // nonparticipating capturing groups | |
96 | if (!compliantExecNpcg && match.length > 1) { |
|
96 | if (!compliantExecNpcg && match.length > 1) { | |
97 | match[0].replace(separator2, function () { |
|
97 | match[0].replace(separator2, function () { | |
98 | for (var i = 1; i < arguments.length - 2; i++) { |
|
98 | for (var i = 1; i < arguments.length - 2; i++) { | |
99 | if (typeof(arguments[i]) === "undefined") { |
|
99 | if (typeof(arguments[i]) === "undefined") { | |
100 | match[i] = undefined; |
|
100 | match[i] = undefined; | |
101 | } |
|
101 | } | |
102 | } |
|
102 | } | |
103 | }); |
|
103 | }); | |
104 | } |
|
104 | } | |
105 | if (match.length > 1 && match.index < str.length) { |
|
105 | if (match.length > 1 && match.index < str.length) { | |
106 | Array.prototype.push.apply(output, match.slice(1)); |
|
106 | Array.prototype.push.apply(output, match.slice(1)); | |
107 | } |
|
107 | } | |
108 | lastLength = match[0].length; |
|
108 | lastLength = match[0].length; | |
109 | lastLastIndex = lastIndex; |
|
109 | lastLastIndex = lastIndex; | |
110 | if (output.length >= limit) { |
|
110 | if (output.length >= limit) { | |
111 | break; |
|
111 | break; | |
112 | } |
|
112 | } | |
113 | } |
|
113 | } | |
114 | if (separator.lastIndex === match.index) { |
|
114 | if (separator.lastIndex === match.index) { | |
115 | separator.lastIndex++; // Avoid an infinite loop |
|
115 | separator.lastIndex++; // Avoid an infinite loop | |
116 | } |
|
116 | } | |
117 | } |
|
117 | } | |
118 | if (lastLastIndex === str.length) { |
|
118 | if (lastLastIndex === str.length) { | |
119 | if (lastLength || !separator.test("")) { |
|
119 | if (lastLength || !separator.test("")) { | |
120 | output.push(""); |
|
120 | output.push(""); | |
121 | } |
|
121 | } | |
122 | } else { |
|
122 | } else { | |
123 | output.push(str.slice(lastLastIndex)); |
|
123 | output.push(str.slice(lastLastIndex)); | |
124 | } |
|
124 | } | |
125 | return output.length > limit ? output.slice(0, limit) : output; |
|
125 | return output.length > limit ? output.slice(0, limit) : output; | |
126 | }; |
|
126 | }; | |
127 |
|
127 | |||
128 | //============================================================================ |
|
128 | //============================================================================ | |
129 | // End contributed Cross-browser RegEx Split |
|
129 | // End contributed Cross-browser RegEx Split | |
130 | //============================================================================ |
|
130 | //============================================================================ | |
131 |
|
131 | |||
132 |
|
132 | |||
133 | var uuid = function () { |
|
133 | var uuid = function () { | |
134 | // http://www.ietf.org/rfc/rfc4122.txt |
|
134 | // http://www.ietf.org/rfc/rfc4122.txt | |
135 | var s = []; |
|
135 | var s = []; | |
136 | var hexDigits = "0123456789ABCDEF"; |
|
136 | var hexDigits = "0123456789ABCDEF"; | |
137 | for (var i = 0; i < 32; i++) { |
|
137 | for (var i = 0; i < 32; i++) { | |
138 | s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); |
|
138 | s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); | |
139 | } |
|
139 | } | |
140 | s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 |
|
140 | s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 | |
141 | s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 |
|
141 | s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 | |
142 |
|
142 | |||
143 | var uuid = s.join(""); |
|
143 | var uuid = s.join(""); | |
144 | return uuid; |
|
144 | return uuid; | |
145 | }; |
|
145 | }; | |
146 |
|
146 | |||
147 |
|
147 | |||
148 | //Fix raw text to parse correctly in crazy XML |
|
148 | //Fix raw text to parse correctly in crazy XML | |
149 | function xmlencode(string) { |
|
149 | function xmlencode(string) { | |
150 | return string.replace(/\&/g,'&'+'amp;') |
|
150 | return string.replace(/\&/g,'&'+'amp;') | |
151 | .replace(/</g,'&'+'lt;') |
|
151 | .replace(/</g,'&'+'lt;') | |
152 | .replace(/>/g,'&'+'gt;') |
|
152 | .replace(/>/g,'&'+'gt;') | |
153 | .replace(/\'/g,'&'+'apos;') |
|
153 | .replace(/\'/g,'&'+'apos;') | |
154 | .replace(/\"/g,'&'+'quot;') |
|
154 | .replace(/\"/g,'&'+'quot;') | |
155 | .replace(/`/g,'&'+'#96;'); |
|
155 | .replace(/`/g,'&'+'#96;'); | |
156 | } |
|
156 | } | |
157 |
|
157 | |||
158 |
|
158 | |||
159 | //Map from terminal commands to CSS classes |
|
159 | //Map from terminal commands to CSS classes | |
160 | var ansi_colormap = { |
|
160 | var ansi_colormap = { | |
161 | "01":"ansibold", |
|
161 | "01":"ansibold", | |
162 |
|
162 | |||
163 | "30":"ansiblack", |
|
163 | "30":"ansiblack", | |
164 | "31":"ansired", |
|
164 | "31":"ansired", | |
165 | "32":"ansigreen", |
|
165 | "32":"ansigreen", | |
166 | "33":"ansiyellow", |
|
166 | "33":"ansiyellow", | |
167 | "34":"ansiblue", |
|
167 | "34":"ansiblue", | |
168 | "35":"ansipurple", |
|
168 | "35":"ansipurple", | |
169 | "36":"ansicyan", |
|
169 | "36":"ansicyan", | |
170 | "37":"ansigray", |
|
170 | "37":"ansigray", | |
171 |
|
171 | |||
172 | "40":"ansibgblack", |
|
172 | "40":"ansibgblack", | |
173 | "41":"ansibgred", |
|
173 | "41":"ansibgred", | |
174 | "42":"ansibggreen", |
|
174 | "42":"ansibggreen", | |
175 | "43":"ansibgyellow", |
|
175 | "43":"ansibgyellow", | |
176 | "44":"ansibgblue", |
|
176 | "44":"ansibgblue", | |
177 | "45":"ansibgpurple", |
|
177 | "45":"ansibgpurple", | |
178 | "46":"ansibgcyan", |
|
178 | "46":"ansibgcyan", | |
179 | "47":"ansibggray" |
|
179 | "47":"ansibggray" | |
180 | }; |
|
180 | }; | |
181 |
|
181 | |||
182 | function _process_numbers(attrs, numbers) { |
|
182 | function _process_numbers(attrs, numbers) { | |
183 | // process ansi escapes |
|
183 | // process ansi escapes | |
184 | var n = numbers.shift(); |
|
184 | var n = numbers.shift(); | |
185 | if (ansi_colormap[n]) { |
|
185 | if (ansi_colormap[n]) { | |
186 | if ( ! attrs["class"] ) { |
|
186 | if ( ! attrs["class"] ) { | |
187 | attrs["class"] = ansi_colormap[n]; |
|
187 | attrs["class"] = ansi_colormap[n]; | |
188 | } else { |
|
188 | } else { | |
189 | attrs["class"] += " " + ansi_colormap[n]; |
|
189 | attrs["class"] += " " + ansi_colormap[n]; | |
190 | } |
|
190 | } | |
191 | } else if (n == "38" || n == "48") { |
|
191 | } else if (n == "38" || n == "48") { | |
192 | // VT100 256 color or 24 bit RGB |
|
192 | // VT100 256 color or 24 bit RGB | |
193 | if (numbers.length < 2) { |
|
193 | if (numbers.length < 2) { | |
194 | console.log("Not enough fields for VT100 color", numbers); |
|
194 | console.log("Not enough fields for VT100 color", numbers); | |
195 | return; |
|
195 | return; | |
196 | } |
|
196 | } | |
197 |
|
197 | |||
198 | var index_or_rgb = numbers.shift(); |
|
198 | var index_or_rgb = numbers.shift(); | |
199 | var r,g,b; |
|
199 | var r,g,b; | |
200 | if (index_or_rgb == "5") { |
|
200 | if (index_or_rgb == "5") { | |
201 | // 256 color |
|
201 | // 256 color | |
202 | var idx = parseInt(numbers.shift()); |
|
202 | var idx = parseInt(numbers.shift()); | |
203 | if (idx < 16) { |
|
203 | if (idx < 16) { | |
204 | // indexed ANSI |
|
204 | // indexed ANSI | |
205 | // ignore bright / non-bright distinction |
|
205 | // ignore bright / non-bright distinction | |
206 | idx = idx % 8; |
|
206 | idx = idx % 8; | |
207 | var ansiclass = ansi_colormap[n[0] + (idx % 8).toString()]; |
|
207 | var ansiclass = ansi_colormap[n[0] + (idx % 8).toString()]; | |
208 | if ( ! attrs["class"] ) { |
|
208 | if ( ! attrs["class"] ) { | |
209 | attrs["class"] = ansiclass; |
|
209 | attrs["class"] = ansiclass; | |
210 | } else { |
|
210 | } else { | |
211 | attrs["class"] += " " + ansiclass; |
|
211 | attrs["class"] += " " + ansiclass; | |
212 | } |
|
212 | } | |
213 | return; |
|
213 | return; | |
214 | } else if (idx < 232) { |
|
214 | } else if (idx < 232) { | |
215 | // 216 color 6x6x6 RGB |
|
215 | // 216 color 6x6x6 RGB | |
216 | idx = idx - 16; |
|
216 | idx = idx - 16; | |
217 | b = idx % 6; |
|
217 | b = idx % 6; | |
218 | g = Math.floor(idx / 6) % 6; |
|
218 | g = Math.floor(idx / 6) % 6; | |
219 | r = Math.floor(idx / 36) % 6; |
|
219 | r = Math.floor(idx / 36) % 6; | |
220 | // convert to rgb |
|
220 | // convert to rgb | |
221 | r = (r * 51); |
|
221 | r = (r * 51); | |
222 | g = (g * 51); |
|
222 | g = (g * 51); | |
223 | b = (b * 51); |
|
223 | b = (b * 51); | |
224 | } else { |
|
224 | } else { | |
225 | // grayscale |
|
225 | // grayscale | |
226 | idx = idx - 231; |
|
226 | idx = idx - 231; | |
227 | // it's 1-24 and should *not* include black or white, |
|
227 | // it's 1-24 and should *not* include black or white, | |
228 | // so a 26 point scale |
|
228 | // so a 26 point scale | |
229 | r = g = b = Math.floor(idx * 256 / 26); |
|
229 | r = g = b = Math.floor(idx * 256 / 26); | |
230 | } |
|
230 | } | |
231 | } else if (index_or_rgb == "2") { |
|
231 | } else if (index_or_rgb == "2") { | |
232 | // Simple 24 bit RGB |
|
232 | // Simple 24 bit RGB | |
233 | if (numbers.length > 3) { |
|
233 | if (numbers.length > 3) { | |
234 | console.log("Not enough fields for RGB", numbers); |
|
234 | console.log("Not enough fields for RGB", numbers); | |
235 | return; |
|
235 | return; | |
236 | } |
|
236 | } | |
237 | r = numbers.shift(); |
|
237 | r = numbers.shift(); | |
238 | g = numbers.shift(); |
|
238 | g = numbers.shift(); | |
239 | b = numbers.shift(); |
|
239 | b = numbers.shift(); | |
240 | } else { |
|
240 | } else { | |
241 | console.log("unrecognized control", numbers); |
|
241 | console.log("unrecognized control", numbers); | |
242 | return; |
|
242 | return; | |
243 | } |
|
243 | } | |
244 | if (r !== undefined) { |
|
244 | if (r !== undefined) { | |
245 | // apply the rgb color |
|
245 | // apply the rgb color | |
246 | var line; |
|
246 | var line; | |
247 | if (n == "38") { |
|
247 | if (n == "38") { | |
248 | line = "color: "; |
|
248 | line = "color: "; | |
249 | } else { |
|
249 | } else { | |
250 | line = "background-color: "; |
|
250 | line = "background-color: "; | |
251 | } |
|
251 | } | |
252 | line = line + "rgb(" + r + "," + g + "," + b + ");" |
|
252 | line = line + "rgb(" + r + "," + g + "," + b + ");" | |
253 | if ( !attrs["style"] ) { |
|
253 | if ( !attrs["style"] ) { | |
254 | attrs["style"] = line; |
|
254 | attrs["style"] = line; | |
255 | } else { |
|
255 | } else { | |
256 | attrs["style"] += " " + line; |
|
256 | attrs["style"] += " " + line; | |
257 | } |
|
257 | } | |
258 | } |
|
258 | } | |
259 | } |
|
259 | } | |
260 | } |
|
260 | } | |
261 |
|
261 | |||
262 | function ansispan(str) { |
|
262 | function ansispan(str) { | |
263 | // ansispan function adapted from github.com/mmalecki/ansispan (MIT License) |
|
263 | // ansispan function adapted from github.com/mmalecki/ansispan (MIT License) | |
264 | // regular ansi escapes (using the table above) |
|
264 | // regular ansi escapes (using the table above) | |
265 | return str.replace(/\033\[(0?[01]|22|39)?([;\d]+)?m/g, function(match, prefix, pattern) { |
|
265 | return str.replace(/\033\[(0?[01]|22|39)?([;\d]+)?m/g, function(match, prefix, pattern) { | |
266 | if (!pattern) { |
|
266 | if (!pattern) { | |
267 | // [(01|22|39|)m close spans |
|
267 | // [(01|22|39|)m close spans | |
268 | return "</span>"; |
|
268 | return "</span>"; | |
269 | } |
|
269 | } | |
270 | // consume sequence of color escapes |
|
270 | // consume sequence of color escapes | |
271 | var numbers = pattern.match(/\d+/g); |
|
271 | var numbers = pattern.match(/\d+/g); | |
272 | var attrs = {}; |
|
272 | var attrs = {}; | |
273 | while (numbers.length > 0) { |
|
273 | while (numbers.length > 0) { | |
274 | _process_numbers(attrs, numbers); |
|
274 | _process_numbers(attrs, numbers); | |
275 | } |
|
275 | } | |
276 |
|
276 | |||
277 | var span = "<span "; |
|
277 | var span = "<span "; | |
278 | for (var attr in attrs) { |
|
278 | for (var attr in attrs) { | |
279 | var value = attrs[attr]; |
|
279 | var value = attrs[attr]; | |
280 | span = span + " " + attr + '="' + attrs[attr] + '"'; |
|
280 | span = span + " " + attr + '="' + attrs[attr] + '"'; | |
281 | } |
|
281 | } | |
282 | return span + ">"; |
|
282 | return span + ">"; | |
283 | }); |
|
283 | }); | |
284 | }; |
|
284 | }; | |
285 |
|
285 | |||
286 | // Transform ANSI color escape codes into HTML <span> tags with css |
|
286 | // Transform ANSI color escape codes into HTML <span> tags with css | |
287 | // classes listed in the above ansi_colormap object. The actual color used |
|
287 | // classes listed in the above ansi_colormap object. The actual color used | |
288 | // are set in the css file. |
|
288 | // are set in the css file. | |
289 | function fixConsole(txt) { |
|
289 | function fixConsole(txt) { | |
290 | txt = xmlencode(txt); |
|
290 | txt = xmlencode(txt); | |
291 | var re = /\033\[([\dA-Fa-f;]*?)m/; |
|
291 | var re = /\033\[([\dA-Fa-f;]*?)m/; | |
292 | var opened = false; |
|
292 | var opened = false; | |
293 | var cmds = []; |
|
293 | var cmds = []; | |
294 | var opener = ""; |
|
294 | var opener = ""; | |
295 | var closer = ""; |
|
295 | var closer = ""; | |
296 |
|
296 | |||
297 | // Strip all ANSI codes that are not color related. Matches |
|
297 | // Strip all ANSI codes that are not color related. Matches | |
298 | // all ANSI codes that do not end with "m". |
|
298 | // all ANSI codes that do not end with "m". | |
299 | var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g; |
|
299 | var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g; | |
300 | txt = txt.replace(ignored_re, ""); |
|
300 | txt = txt.replace(ignored_re, ""); | |
301 |
|
301 | |||
302 | // color ansi codes |
|
302 | // color ansi codes | |
303 | txt = ansispan(txt); |
|
303 | txt = ansispan(txt); | |
304 | return txt; |
|
304 | return txt; | |
305 | } |
|
305 | } | |
306 |
|
306 | |||
307 | // Remove chunks that should be overridden by the effect of |
|
307 | // Remove chunks that should be overridden by the effect of | |
308 | // carriage return characters |
|
308 | // carriage return characters | |
309 | function fixCarriageReturn(txt) { |
|
309 | function fixCarriageReturn(txt) { | |
310 | var tmp = txt; |
|
310 | var tmp = txt; | |
311 | do { |
|
311 | do { | |
312 | txt = tmp; |
|
312 | txt = tmp; | |
313 | tmp = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline |
|
313 | tmp = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline | |
314 | tmp = tmp.replace(/^.*\r+/gm, ''); // Other \r --> clear line |
|
314 | tmp = tmp.replace(/^.*\r+/gm, ''); // Other \r --> clear line | |
315 | } while (tmp.length < txt.length); |
|
315 | } while (tmp.length < txt.length); | |
316 | return txt; |
|
316 | return txt; | |
317 | } |
|
317 | } | |
318 |
|
318 | |||
319 | // Locate any URLs and convert them to a anchor tag |
|
319 | // Locate any URLs and convert them to a anchor tag | |
320 | function autoLinkUrls(txt) { |
|
320 | function autoLinkUrls(txt) { | |
321 | return txt.replace(/(^|\s)(https?|ftp)(:[^'">\s]+)/gi, |
|
321 | return txt.replace(/(^|\s)(https?|ftp)(:[^'">\s]+)/gi, | |
322 | "$1<a target=\"_blank\" href=\"$2$3\">$2$3</a>"); |
|
322 | "$1<a target=\"_blank\" href=\"$2$3\">$2$3</a>"); | |
323 | } |
|
323 | } | |
324 |
|
324 | |||
325 | // some keycodes that seem to be platform/browser independent |
|
325 | // some keycodes that seem to be platform/browser independent | |
326 | var keycodes = { |
|
326 | var keycodes = { | |
327 | BACKSPACE: 8, |
|
327 | BACKSPACE: 8, | |
328 | TAB : 9, |
|
328 | TAB : 9, | |
329 | ENTER : 13, |
|
329 | ENTER : 13, | |
330 | SHIFT : 16, |
|
330 | SHIFT : 16, | |
331 | CTRL : 17, |
|
331 | CTRL : 17, | |
332 | CONTROL : 17, |
|
332 | CONTROL : 17, | |
333 | ALT : 18, |
|
333 | ALT : 18, | |
334 | CAPS_LOCK: 20, |
|
334 | CAPS_LOCK: 20, | |
335 | ESC : 27, |
|
335 | ESC : 27, | |
336 | SPACE : 32, |
|
336 | SPACE : 32, | |
337 | PGUP : 33, |
|
337 | PGUP : 33, | |
338 | PGDOWN : 34, |
|
338 | PGDOWN : 34, | |
339 | END : 35, |
|
339 | END : 35, | |
340 | HOME : 36, |
|
340 | HOME : 36, | |
341 | LEFT_ARROW: 37, |
|
341 | LEFT_ARROW: 37, | |
342 | LEFTARROW: 37, |
|
342 | LEFTARROW: 37, | |
343 | LEFT : 37, |
|
343 | LEFT : 37, | |
344 | UP_ARROW : 38, |
|
344 | UP_ARROW : 38, | |
345 | UPARROW : 38, |
|
345 | UPARROW : 38, | |
346 | UP : 38, |
|
346 | UP : 38, | |
347 | RIGHT_ARROW:39, |
|
347 | RIGHT_ARROW:39, | |
348 | RIGHTARROW:39, |
|
348 | RIGHTARROW:39, | |
349 | RIGHT : 39, |
|
349 | RIGHT : 39, | |
350 | DOWN_ARROW: 40, |
|
350 | DOWN_ARROW: 40, | |
351 | DOWNARROW: 40, |
|
351 | DOWNARROW: 40, | |
352 | DOWN : 40, |
|
352 | DOWN : 40, | |
353 | // all three of these keys may be COMMAND on OS X: |
|
353 | // all three of these keys may be COMMAND on OS X: | |
354 | LEFT_SUPER : 91, |
|
354 | LEFT_SUPER : 91, | |
355 | RIGHT_SUPER : 92, |
|
355 | RIGHT_SUPER : 92, | |
356 | COMMAND : 93, |
|
356 | COMMAND : 93, | |
357 | }; |
|
357 | }; | |
358 |
|
358 | |||
359 |
|
359 | |||
360 | var points_to_pixels = function (points) { |
|
360 | var points_to_pixels = function (points) { | |
361 | // A reasonably good way of converting between points and pixels. |
|
361 | // A reasonably good way of converting between points and pixels. | |
362 | var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>'); |
|
362 | var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>'); | |
363 | $(body).append(test); |
|
363 | $(body).append(test); | |
364 | var pixel_per_point = test.width()/10000; |
|
364 | var pixel_per_point = test.width()/10000; | |
365 | test.remove(); |
|
365 | test.remove(); | |
366 | return Math.floor(points*pixel_per_point); |
|
366 | return Math.floor(points*pixel_per_point); | |
367 | }; |
|
367 | }; | |
368 |
|
368 | |||
|
369 | var always_new = function (constructor) { | |||
|
370 | // wrapper around contructor to avoid requiring `var a = new constructor()` | |||
|
371 | // useful for passing constructors as callbacks, | |||
|
372 | // not for programmer laziness. | |||
|
373 | // from http://programmers.stackexchange.com/questions/118798 | |||
|
374 | return function () { | |||
|
375 | var obj = Object.create(constructor.prototype); | |||
|
376 | constructor.apply(obj, arguments); | |||
|
377 | return obj; | |||
|
378 | }; | |||
|
379 | }; | |||
|
380 | ||||
369 |
|
381 | |||
370 | var url_path_join = function () { |
|
382 | var url_path_join = function () { | |
371 | // join a sequence of url components with '/' |
|
383 | // join a sequence of url components with '/' | |
372 | var url = ''; |
|
384 | var url = ''; | |
373 | for (var i = 0; i < arguments.length; i++) { |
|
385 | for (var i = 0; i < arguments.length; i++) { | |
374 | if (arguments[i] === '') { |
|
386 | if (arguments[i] === '') { | |
375 | continue; |
|
387 | continue; | |
376 | } |
|
388 | } | |
377 | if (url.length > 0 && url[url.length-1] != '/') { |
|
389 | if (url.length > 0 && url[url.length-1] != '/') { | |
378 | url = url + '/' + arguments[i]; |
|
390 | url = url + '/' + arguments[i]; | |
379 | } else { |
|
391 | } else { | |
380 | url = url + arguments[i]; |
|
392 | url = url + arguments[i]; | |
381 | } |
|
393 | } | |
382 | } |
|
394 | } | |
383 | return url; |
|
395 | return url; | |
384 | }; |
|
396 | }; | |
385 |
|
397 | |||
386 |
|
398 | |||
387 | var splitext = function (filename) { |
|
399 | var splitext = function (filename) { | |
388 | // mimic Python os.path.splitext |
|
400 | // mimic Python os.path.splitext | |
389 | // Returns ['base', '.ext'] |
|
401 | // Returns ['base', '.ext'] | |
390 | var idx = filename.lastIndexOf('.'); |
|
402 | var idx = filename.lastIndexOf('.'); | |
391 | if (idx > 0) { |
|
403 | if (idx > 0) { | |
392 | return [filename.slice(0, idx), filename.slice(idx)]; |
|
404 | return [filename.slice(0, idx), filename.slice(idx)]; | |
393 | } else { |
|
405 | } else { | |
394 | return [filename, '']; |
|
406 | return [filename, '']; | |
395 | } |
|
407 | } | |
396 | } |
|
408 | } | |
397 |
|
409 | |||
398 |
|
410 | |||
399 | // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript |
|
411 | // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript | |
400 | var browser = (function() { |
|
412 | var browser = (function() { | |
401 | var N= navigator.appName, ua= navigator.userAgent, tem; |
|
413 | var N= navigator.appName, ua= navigator.userAgent, tem; | |
402 | var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); |
|
414 | var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); | |
403 | if (M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1]; |
|
415 | if (M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1]; | |
404 | M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?']; |
|
416 | M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?']; | |
405 | return M; |
|
417 | return M; | |
406 | })(); |
|
418 | })(); | |
407 |
|
419 | |||
408 |
|
420 | |||
409 | return { |
|
421 | return { | |
410 | regex_split : regex_split, |
|
422 | regex_split : regex_split, | |
411 | uuid : uuid, |
|
423 | uuid : uuid, | |
412 | fixConsole : fixConsole, |
|
424 | fixConsole : fixConsole, | |
413 | keycodes : keycodes, |
|
425 | keycodes : keycodes, | |
414 | fixCarriageReturn : fixCarriageReturn, |
|
426 | fixCarriageReturn : fixCarriageReturn, | |
415 | autoLinkUrls : autoLinkUrls, |
|
427 | autoLinkUrls : autoLinkUrls, | |
416 | points_to_pixels : points_to_pixels, |
|
428 | points_to_pixels : points_to_pixels, | |
417 | url_path_join : url_path_join, |
|
429 | url_path_join : url_path_join, | |
418 | splitext : splitext, |
|
430 | splitext : splitext, | |
|
431 | always_new : always_new, | |||
419 | browser : browser |
|
432 | browser : browser | |
420 | }; |
|
433 | }; | |
421 |
|
434 | |||
422 | }(IPython)); |
|
435 | }(IPython)); | |
423 |
|
436 |
General Comments 0
You need to be logged in to leave comments.
Login now