utils.js
288 lines
| 11.0 KiB
| application/javascript
|
JavascriptLexer
Brian E. Granger
|
r4609 | //---------------------------------------------------------------------------- | ||
Aron Ahmadia
|
r8565 | // Copyright (C) 2008-2012 The IPython Development Team | ||
Brian E. Granger
|
r4609 | // | ||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r4349 | |||
//============================================================================ | ||||
// Utilities | ||||
//============================================================================ | ||||
Stefan van der Walt
|
r5479 | IPython.namespace('IPython.utils'); | ||
Brian E. Granger
|
r4349 | |||
Brian E. Granger
|
r4352 | IPython.utils = (function (IPython) { | ||
Aron Ahmadia
|
r8565 | //============================================================================ | ||
// Cross-browser RegEx Split | ||||
//============================================================================ | ||||
// This code has been MODIFIED from the code licensed below to not replace the | ||||
// default browser split. The license is reproduced here. | ||||
// see http://blog.stevenlevithan.com/archives/cross-browser-split for more info: | ||||
/*! | ||||
* Cross-Browser Split 1.1.1 | ||||
* Copyright 2007-2012 Steven Levithan <stevenlevithan.com> | ||||
* Available under the MIT License | ||||
* ECMAScript compliant, uniform cross-browser split method | ||||
*/ | ||||
/** | ||||
* Splits a string into an array of strings using a regex or string | ||||
* separator. Matches of the separator are not included in the result array. | ||||
* However, if `separator` is a regex that contains capturing groups, | ||||
* backreferences are spliced into the result each time `separator` is | ||||
* matched. Fixes browser bugs compared to the native | ||||
* `String.prototype.split` and can be used reliably cross-browser. | ||||
* @param {String} str String to split. | ||||
* @param {RegExp|String} separator Regex or string to use for separating | ||||
* the string. | ||||
* @param {Number} [limit] Maximum number of items to include in the result | ||||
* array. | ||||
* @returns {Array} Array of substrings. | ||||
* @example | ||||
* | ||||
* // Basic use | ||||
* regex_split('a b c d', ' '); | ||||
* // -> ['a', 'b', 'c', 'd'] | ||||
* | ||||
* // With limit | ||||
* regex_split('a b c d', ' ', 2); | ||||
* // -> ['a', 'b'] | ||||
* | ||||
* // Backreferences in result array | ||||
* regex_split('..word1 word2..', /([a-z]+)(\d+)/i); | ||||
* // -> ['..', 'word', '1', ' ', 'word', '2', '..'] | ||||
*/ | ||||
var regex_split = function (str, separator, limit) { | ||||
// If `separator` is not a regex, use `split` | ||||
if (Object.prototype.toString.call(separator) !== "[object RegExp]") { | ||||
return split.call(str, separator, limit); | ||||
} | ||||
var output = [], | ||||
flags = (separator.ignoreCase ? "i" : "") + | ||||
(separator.multiline ? "m" : "") + | ||||
(separator.extended ? "x" : "") + // Proposed for ES6 | ||||
(separator.sticky ? "y" : ""), // Firefox 3+ | ||||
lastLastIndex = 0, | ||||
// Make `global` and avoid `lastIndex` issues by working with a copy | ||||
separator = new RegExp(separator.source, flags + "g"), | ||||
separator2, match, lastIndex, lastLength; | ||||
str += ""; // Type-convert | ||||
compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined" | ||||
if (!compliantExecNpcg) { | ||||
// Doesn't need flags gy, but they don't hurt | ||||
separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); | ||||
} | ||||
/* Values for `limit`, per the spec: | ||||
* If undefined: 4294967295 // Math.pow(2, 32) - 1 | ||||
* If 0, Infinity, or NaN: 0 | ||||
* If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; | ||||
* If negative number: 4294967296 - Math.floor(Math.abs(limit)) | ||||
* If other: Type-convert, then use the above rules | ||||
*/ | ||||
limit = typeof(limit) === "undefined" ? | ||||
-1 >>> 0 : // Math.pow(2, 32) - 1 | ||||
limit >>> 0; // ToUint32(limit) | ||||
while (match = separator.exec(str)) { | ||||
// `separator.lastIndex` is not reliable cross-browser | ||||
lastIndex = match.index + match[0].length; | ||||
if (lastIndex > lastLastIndex) { | ||||
output.push(str.slice(lastLastIndex, match.index)); | ||||
// Fix browsers whose `exec` methods don't consistently return `undefined` for | ||||
// nonparticipating capturing groups | ||||
if (!compliantExecNpcg && match.length > 1) { | ||||
match[0].replace(separator2, function () { | ||||
for (var i = 1; i < arguments.length - 2; i++) { | ||||
if (typeof(arguments[i]) === "undefined") { | ||||
match[i] = undefined; | ||||
} | ||||
} | ||||
}); | ||||
} | ||||
if (match.length > 1 && match.index < str.length) { | ||||
Array.prototype.push.apply(output, match.slice(1)); | ||||
} | ||||
lastLength = match[0].length; | ||||
lastLastIndex = lastIndex; | ||||
if (output.length >= limit) { | ||||
break; | ||||
} | ||||
} | ||||
if (separator.lastIndex === match.index) { | ||||
separator.lastIndex++; // Avoid an infinite loop | ||||
} | ||||
} | ||||
if (lastLastIndex === str.length) { | ||||
if (lastLength || !separator.test("")) { | ||||
output.push(""); | ||||
} | ||||
} else { | ||||
output.push(str.slice(lastLastIndex)); | ||||
} | ||||
return output.length > limit ? output.slice(0, limit) : output; | ||||
}; | ||||
//============================================================================ | ||||
// End contributed Cross-browser RegEx Split | ||||
//============================================================================ | ||||
Brian E. Granger
|
r4352 | var uuid = function () { | ||
// http://www.ietf.org/rfc/rfc4122.txt | ||||
var s = []; | ||||
var hexDigits = "0123456789ABCDEF"; | ||||
for (var i = 0; i < 32; i++) { | ||||
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); | ||||
} | ||||
s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 | ||||
s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 | ||||
var uuid = s.join(""); | ||||
return uuid; | ||||
}; | ||||
//Fix raw text to parse correctly in crazy XML | ||||
function xmlencode(string) { | ||||
return string.replace(/\&/g,'&'+'amp;') | ||||
.replace(/</g,'&'+'lt;') | ||||
.replace(/>/g,'&'+'gt;') | ||||
.replace(/\'/g,'&'+'apos;') | ||||
.replace(/\"/g,'&'+'quot;') | ||||
Stefan van der Walt
|
r5479 | .replace(/`/g,'&'+'#96;'); | ||
Brian E. Granger
|
r4349 | } | ||
Brian E. Granger
|
r4352 | |||
Brian E. Granger
|
r4361 | |||
Brian E. Granger
|
r4352 | //Map from terminal commands to CSS classes | ||
Brian Granger
|
r4383 | ansi_colormap = { | ||
"30":"ansiblack", "31":"ansired", | ||||
"32":"ansigreen", "33":"ansiyellow", | ||||
Michael Droettboom
|
r7338 | "34":"ansiblue", "35":"ansipurple","36":"ansicyan", | ||
Brian Granger
|
r4383 | "37":"ansigrey", "01":"ansibold" | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4352 | |||
Michael Droettboom
|
r7339 | // Transform ANSI color escape codes into HTML <span> tags with css | ||
Brian Granger
|
r4383 | // classes listed in the above ansi_colormap object. The actual color used | ||
// are set in the css file. | ||||
Brian E. Granger
|
r4352 | function fixConsole(txt) { | ||
Stefan van der Walt
|
r5479 | txt = xmlencode(txt); | ||
MinRK
|
r6423 | var re = /\033\[([\dA-Fa-f;]*?)m/; | ||
Stefan van der Walt
|
r5479 | var opened = false; | ||
var cmds = []; | ||||
var opener = ""; | ||||
var closer = ""; | ||||
Brian E. Granger
|
r4352 | while (re.test(txt)) { | ||
Stefan van der Walt
|
r5479 | var cmds = txt.match(re)[1].split(";"); | ||
closer = opened?"</span>":""; | ||||
opened = cmds.length > 1 || cmds[0] != 0; | ||||
var rep = []; | ||||
Brian E. Granger
|
r4352 | for (var i in cmds) | ||
Brian Granger
|
r4383 | if (typeof(ansi_colormap[cmds[i]]) != "undefined") | ||
Stefan van der Walt
|
r5479 | rep.push(ansi_colormap[cmds[i]]); | ||
opener = rep.length > 0?"<span class=\""+rep.join(" ")+"\">":""; | ||||
txt = txt.replace(re, closer + opener); | ||||
Brian E. Granger
|
r4352 | } | ||
Stefan van der Walt
|
r5479 | if (opened) txt += "</span>"; | ||
return txt; | ||||
Brian E. Granger
|
r4352 | } | ||
Michael Droettboom
|
r7338 | // Remove chunks that should be overridden by the effect of | ||
// carriage return characters | ||||
function fixCarriageReturn(txt) { | ||||
tmp = txt; | ||||
do { | ||||
txt = tmp; | ||||
Thomas Kluyver
|
r8702 | tmp = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline | ||
tmp = tmp.replace(/^.*\r+/gm, ''); // Other \r --> clear line | ||||
Michael Droettboom
|
r7338 | } while (tmp.length < txt.length); | ||
return txt; | ||||
} | ||||
Brian E. Granger
|
r4361 | |||
Erik M. Bray
|
r8528 | // Locate URLs in plain text and wrap them in spaces so that they can be | ||
// better picked out by autoLinkUrls even after the text has been | ||||
// converted to HTML | ||||
function wrapUrls(txt) { | ||||
// Note this regexp is a modified version of one from | ||||
// Markdown.Converter For now it only supports http(s) and ftp URLs, | ||||
// but could easily support others (though file:// should maybe be | ||||
// avoided) | ||||
var url_re = /(^|\W)(https?|ftp)(:\/\/[-A-Z0-9+&@#\/%?=~_|\[\]\(\)!:,\.;]*[-A-Z0-9+&@#\/%=~_|\[\]])($|\W)/gi; | ||||
return txt.replace(url_re, "$1 $2$3 $4"); | ||||
} | ||||
// Locate a URL with spaces around it and convert that to a anchor tag | ||||
function autoLinkUrls(txt) { | ||||
return txt.replace(/ ((https?|ftp):[^'">\s]+) /gi, | ||||
"<a target=\"_blank\" href=\"$1\">$1</a>"); | ||||
} | ||||
Brian E. Granger
|
r4361 | grow = function(element) { | ||
// Grow the cell by hand. This is used upon reloading from JSON, when the | ||||
// autogrow handler is not called. | ||||
var dom = element.get(0); | ||||
var lines_count = 0; | ||||
// modified split rule from | ||||
// http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 | ||||
var lines = dom.value.split(/\r|\r\n|\n/); | ||||
lines_count = lines.length; | ||||
if (lines_count >= 1) { | ||||
dom.rows = lines_count; | ||||
} else { | ||||
dom.rows = 1; | ||||
} | ||||
}; | ||||
Matthias BUSSONNIER
|
r7136 | // some keycodes that seem to be platform/browser independant | ||
var keycodes ={ | ||||
Matthias BUSSONNIER
|
r7193 | BACKSPACE: 8, | ||
TAB : 9, | ||||
ENTER : 13, | ||||
SHIFT : 16, | ||||
CTRL : 17, | ||||
CONTROL : 17, | ||||
Brian Granger
|
r7199 | ALT : 18, | ||
Matthias BUSSONNIER
|
r7193 | ESC : 27, | ||
SPACE : 32, | ||||
PGUP : 33, | ||||
PGDOWN : 34, | ||||
LEFT_ARROW: 37, | ||||
LEFTARROW: 37, | ||||
LEFT : 37, | ||||
UP_ARROW : 38, | ||||
UPARROW : 38, | ||||
UP : 38, | ||||
RIGHT_ARROW:39, | ||||
RIGHTARROW:39, | ||||
RIGHT : 39, | ||||
DOWN_ARROW: 40, | ||||
DOWNARROW: 40, | ||||
DOWN : 40, | ||||
Matthias BUSSONNIER
|
r7136 | }; | ||
Brian E. Granger
|
r4361 | |||
Brian Granger
|
r7353 | |||
points_to_pixels = function (points) { | ||||
// A reasonably good way of converting between points and pixels. | ||||
var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>'); | ||||
$(body).append(test); | ||||
var pixel_per_point = test.width()/10000; | ||||
test.remove(); | ||||
return Math.floor(points*pixel_per_point); | ||||
} | ||||
Brian E. Granger
|
r4352 | return { | ||
Aron Ahmadia
|
r8565 | regex_split : regex_split, | ||
Brian E. Granger
|
r4352 | uuid : uuid, | ||
Brian E. Granger
|
r4361 | fixConsole : fixConsole, | ||
Matthias BUSSONNIER
|
r7136 | keycodes : keycodes, | ||
grow : grow, | ||||
Min RK
|
r7357 | fixCarriageReturn : fixCarriageReturn, | ||
Erik M. Bray
|
r8528 | wrapUrls : wrapUrls, | ||
autoLinkUrls : autoLinkUrls, | ||||
Brian Granger
|
r7353 | points_to_pixels : points_to_pixels | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4352 | |||
}(IPython)); | ||||