utils.js
532 lines
| 19.1 KiB
| application/javascript
|
JavascriptLexer
|
r4609 | //---------------------------------------------------------------------------- | ||
|
r8565 | // Copyright (C) 2008-2012 The IPython Development Team | ||
|
r4609 | // | ||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
|
r4349 | |||
//============================================================================ | ||||
// Utilities | ||||
//============================================================================ | ||||
|
r5479 | IPython.namespace('IPython.utils'); | ||
|
r4349 | |||
|
r4352 | IPython.utils = (function (IPython) { | ||
|
r12103 | "use strict"; | ||
|
r15228 | |||
IPython.load_extensions = function () { | ||||
// load one or more IPython notebook extensions with requirejs | ||||
var extensions = []; | ||||
var extension_names = arguments; | ||||
for (var i = 0; i < extension_names.length; i++) { | ||||
extensions.push("nbextensions/" + arguments[i]); | ||||
} | ||||
require(extensions, | ||||
function () { | ||||
for (var i = 0; i < arguments.length; i++) { | ||||
var ext = arguments[i]; | ||||
var ext_name = extension_names[i]; | ||||
// success callback | ||||
console.log("Loaded extension: " + ext_name); | ||||
if (ext && ext.load_ipython_extension !== undefined) { | ||||
ext.load_ipython_extension(); | ||||
} | ||||
} | ||||
}, | ||||
function (err) { | ||||
// failure callback | ||||
console.log("Failed to load extension(s):", err.requireModules, err); | ||||
} | ||||
); | ||||
}; | ||||
|
r4352 | |||
|
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 | ||||
|
r11307 | var compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined"; | ||
|
r8565 | 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 | ||||
//============================================================================ | ||||
|
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;') | ||||
|
r5479 | .replace(/`/g,'&'+'#96;'); | ||
|
r4349 | } | ||
|
r4352 | |||
|
r4361 | |||
|
r4352 | //Map from terminal commands to CSS classes | ||
|
r8839 | var ansi_colormap = { | ||
|
r10250 | "01":"ansibold", | ||
|
r11313 | |||
|
r10250 | "30":"ansiblack", | ||
"31":"ansired", | ||||
"32":"ansigreen", | ||||
"33":"ansiyellow", | ||||
"34":"ansiblue", | ||||
"35":"ansipurple", | ||||
"36":"ansicyan", | ||||
|
r11312 | "37":"ansigray", | ||
|
r11313 | |||
|
r11312 | "40":"ansibgblack", | ||
"41":"ansibgred", | ||||
"42":"ansibggreen", | ||||
|
r12095 | "43":"ansibgyellow", | ||
|
r11312 | "44":"ansibgblue", | ||
"45":"ansibgpurple", | ||||
"46":"ansibgcyan", | ||||
"47":"ansibggray" | ||||
|
r5479 | }; | ||
|
r11307 | |||
function _process_numbers(attrs, numbers) { | ||||
// process ansi escapes | ||||
var n = numbers.shift(); | ||||
if (ansi_colormap[n]) { | ||||
if ( ! attrs["class"] ) { | ||||
attrs["class"] = ansi_colormap[n]; | ||||
} else { | ||||
attrs["class"] += " " + ansi_colormap[n]; | ||||
} | ||||
} else if (n == "38" || n == "48") { | ||||
|
r11314 | // VT100 256 color or 24 bit RGB | ||
|
r11307 | if (numbers.length < 2) { | ||
|
r11314 | console.log("Not enough fields for VT100 color", numbers); | ||
|
r11307 | return; | ||
} | ||||
var index_or_rgb = numbers.shift(); | ||||
var r,g,b; | ||||
if (index_or_rgb == "5") { | ||||
|
r11314 | // 256 color | ||
|
r11307 | var idx = parseInt(numbers.shift()); | ||
if (idx < 16) { | ||||
|
r11313 | // indexed ANSI | ||
// ignore bright / non-bright distinction | ||||
idx = idx % 8; | ||||
var ansiclass = ansi_colormap[n[0] + (idx % 8).toString()]; | ||||
if ( ! attrs["class"] ) { | ||||
attrs["class"] = ansiclass; | ||||
} else { | ||||
attrs["class"] += " " + ansiclass; | ||||
} | ||||
return; | ||||
|
r11307 | } else if (idx < 232) { | ||
|
r11313 | // 216 color 6x6x6 RGB | ||
|
r11307 | idx = idx - 16; | ||
b = idx % 6; | ||||
g = Math.floor(idx / 6) % 6; | ||||
r = Math.floor(idx / 36) % 6; | ||||
// convert to rgb | ||||
r = (r * 51); | ||||
g = (g * 51); | ||||
b = (b * 51); | ||||
} else { | ||||
// grayscale | ||||
idx = idx - 231; | ||||
// it's 1-24 and should *not* include black or white, | ||||
// so a 26 point scale | ||||
|
r11313 | r = g = b = Math.floor(idx * 256 / 26); | ||
|
r11307 | } | ||
} else if (index_or_rgb == "2") { | ||||
|
r11313 | // Simple 24 bit RGB | ||
|
r11307 | if (numbers.length > 3) { | ||
console.log("Not enough fields for RGB", numbers); | ||||
return; | ||||
} | ||||
r = numbers.shift(); | ||||
g = numbers.shift(); | ||||
b = numbers.shift(); | ||||
} else { | ||||
console.log("unrecognized control", numbers); | ||||
return; | ||||
} | ||||
if (r !== undefined) { | ||||
// apply the rgb color | ||||
var line; | ||||
if (n == "38") { | ||||
line = "color: "; | ||||
} else { | ||||
line = "background-color: "; | ||||
} | ||||
line = line + "rgb(" + r + "," + g + "," + b + ");" | ||||
if ( !attrs["style"] ) { | ||||
attrs["style"] = line; | ||||
} else { | ||||
attrs["style"] += " " + line; | ||||
} | ||||
} | ||||
} | ||||
} | ||||
|
r4352 | |||
|
r10250 | function ansispan(str) { | ||
// ansispan function adapted from github.com/mmalecki/ansispan (MIT License) | ||||
|
r11307 | // regular ansi escapes (using the table above) | ||
return str.replace(/\033\[(0?[01]|22|39)?([;\d]+)?m/g, function(match, prefix, pattern) { | ||||
if (!pattern) { | ||||
// [(01|22|39|)m close spans | ||||
return "</span>"; | ||||
} | ||||
// consume sequence of color escapes | ||||
var numbers = pattern.match(/\d+/g); | ||||
var attrs = {}; | ||||
while (numbers.length > 0) { | ||||
_process_numbers(attrs, numbers); | ||||
} | ||||
var span = "<span "; | ||||
for (var attr in attrs) { | ||||
var value = attrs[attr]; | ||||
span = span + " " + attr + '="' + attrs[attr] + '"'; | ||||
} | ||||
return span + ">"; | ||||
|
r10250 | }); | ||
|
r11307 | }; | ||
|
r10250 | |||
|
r7339 | // Transform ANSI color escape codes into HTML <span> tags with css | ||
|
r4383 | // classes listed in the above ansi_colormap object. The actual color used | ||
// are set in the css file. | ||||
|
r4352 | function fixConsole(txt) { | ||
|
r5479 | txt = xmlencode(txt); | ||
|
r6423 | var re = /\033\[([\dA-Fa-f;]*?)m/; | ||
|
r5479 | var opened = false; | ||
var cmds = []; | ||||
var opener = ""; | ||||
var closer = ""; | ||||
|
r10140 | |||
// Strip all ANSI codes that are not color related. Matches | ||||
// all ANSI codes that do not end with "m". | ||||
var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g; | ||||
txt = txt.replace(ignored_re, ""); | ||||
|
r10250 | |||
// color ansi codes | ||||
txt = ansispan(txt); | ||||
|
r5479 | return txt; | ||
|
r4352 | } | ||
|
r7338 | // Remove chunks that should be overridden by the effect of | ||
// carriage return characters | ||||
function fixCarriageReturn(txt) { | ||||
|
r8839 | var tmp = txt; | ||
|
r7338 | do { | ||
txt = tmp; | ||||
|
r8702 | tmp = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline | ||
tmp = tmp.replace(/^.*\r+/gm, ''); // Other \r --> clear line | ||||
|
r7338 | } while (tmp.length < txt.length); | ||
return txt; | ||||
} | ||||
|
r4361 | |||
|
r10045 | // Locate any URLs and convert them to a anchor tag | ||
|
r8528 | function autoLinkUrls(txt) { | ||
|
r10045 | return txt.replace(/(^|\s)(https?|ftp)(:[^'">\s]+)/gi, | ||
"$1<a target=\"_blank\" href=\"$2$3\">$2$3</a>"); | ||||
|
r8528 | } | ||
|
r8839 | var points_to_pixels = function (points) { | ||
|
r7353 | // 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); | ||||
|
r8839 | }; | ||
|
r13206 | |||
var always_new = function (constructor) { | ||||
// wrapper around contructor to avoid requiring `var a = new constructor()` | ||||
// useful for passing constructors as callbacks, | ||||
// not for programmer laziness. | ||||
// from http://programmers.stackexchange.com/questions/118798 | ||||
return function () { | ||||
var obj = Object.create(constructor.prototype); | ||||
constructor.apply(obj, arguments); | ||||
return obj; | ||||
}; | ||||
}; | ||||
|
r7353 | |||
|
r13063 | var url_path_join = function () { | ||
// join a sequence of url components with '/' | ||||
var url = ''; | ||||
for (var i = 0; i < arguments.length; i++) { | ||||
|
r13080 | if (arguments[i] === '') { | ||
continue; | ||||
} | ||||
|
r13063 | if (url.length > 0 && url[url.length-1] != '/') { | ||
url = url + '/' + arguments[i]; | ||||
} else { | ||||
url = url + arguments[i]; | ||||
} | ||||
} | ||||
|
r15241 | url = url.replace(/\/\/+/, '/'); | ||
|
r13063 | return url; | ||
}; | ||||
|
r13686 | |||
|
r15241 | var parse_url = function (url) { | ||
|
r15243 | // an `a` element with an href allows attr-access to the parsed segments of a URL | ||
// a = parse_url("http://localhost:8888/path/name#hash") | ||||
// a.protocol = "http:" | ||||
// a.host = "localhost:8888" | ||||
// a.hostname = "localhost" | ||||
// a.port = 8888 | ||||
// a.pathname = "/path/name" | ||||
// a.hash = "#hash" | ||||
|
r15241 | var a = document.createElement("a"); | ||
a.href = url; | ||||
return a; | ||||
}; | ||||
|
r13686 | |||
var encode_uri_components = function (uri) { | ||||
// encode just the components of a multi-segment uri, | ||||
// leaving '/' separators | ||||
return uri.split('/').map(encodeURIComponent).join('/'); | ||||
|
r15241 | }; | ||
|
r13686 | |||
var url_join_encode = function () { | ||||
// join a sequence of url components with '/', | ||||
// encoding each component with encodeURIComponent | ||||
return encode_uri_components(url_path_join.apply(null, arguments)); | ||||
}; | ||||
|
r13063 | |||
|
r13120 | var splitext = function (filename) { | ||
// mimic Python os.path.splitext | ||||
// Returns ['base', '.ext'] | ||||
var idx = filename.lastIndexOf('.'); | ||||
if (idx > 0) { | ||||
return [filename.slice(0, idx), filename.slice(idx)]; | ||||
} else { | ||||
return [filename, '']; | ||||
} | ||||
|
r15233 | }; | ||
|
r15331 | var escape_html = function (text) { | ||
// escape text to HTML | ||||
return $("<div/>").text(text).html(); | ||||
} | ||||
|
r15240 | var get_body_data = function(key) { | ||
|
r15233 | // get a url-encoded item from body.data and decode it | ||
// we should never have any encoded URLs anywhere else in code | ||||
// until we are building an actual request | ||||
return decodeURIComponent($('body').data(key)); | ||||
}; | ||||
|
r13120 | |||
|
r9227 | // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript | ||
|
r12103 | var browser = (function() { | ||
|
r14205 | if (typeof navigator === 'undefined') { | ||
// navigator undefined in node | ||||
return 'None'; | ||||
} | ||||
|
r9227 | var N= navigator.appName, ua= navigator.userAgent, tem; | ||
var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); | ||||
if (M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1]; | ||||
M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?']; | ||||
return M; | ||||
})(); | ||||
|
r14816 | // http://stackoverflow.com/questions/11219582/how-to-detect-my-browser-version-and-operating-system-using-javascript | ||
var platform = (function () { | ||||
|
r14826 | if (typeof navigator === 'undefined') { | ||
// navigator undefined in node | ||||
return 'None'; | ||||
} | ||||
var OSName="None"; | ||||
|
r14816 | if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows"; | ||
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS"; | ||||
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX"; | ||||
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux"; | ||||
return OSName | ||||
})(); | ||||
|
r14033 | var is_or_has = function (a, b) { | ||
// Is b a child of a or a itself? | ||||
return a.has(b).length !==0 || a.is(b); | ||||
} | ||||
var is_focused = function (e) { | ||||
// Is element e, or one of its children focused? | ||||
e = $(e); | ||||
var target = $(document.activeElement); | ||||
if (target.length > 0) { | ||||
if (is_or_has(e, target)) { | ||||
return true; | ||||
} else { | ||||
return false; | ||||
} | ||||
} else { | ||||
return false; | ||||
} | ||||
} | ||||
|
r16629 | |||
|
r17830 | var ajax_error_msg = function (jqXHR) { | ||
// Return a JSON error message if there is one, | ||||
// otherwise the basic HTTP status text. | ||||
if (jqXHR.responseJSON && jqXHR.responseJSON.message) { | ||||
return jqXHR.responseJSON.message; | ||||
} else { | ||||
return jqXHR.statusText; | ||||
} | ||||
} | ||||
|
r16629 | var log_ajax_error = function (jqXHR, status, error) { | ||
// log ajax failures with informative messages | ||||
var msg = "API request failed (" + jqXHR.status + "): "; | ||||
console.log(jqXHR); | ||||
|
r17830 | msg += ajax_error_msg(jqXHR); | ||
|
r16629 | console.log(msg); | ||
}; | ||||
|
r14033 | |||
|
r4352 | return { | ||
|
r8565 | regex_split : regex_split, | ||
|
r4352 | uuid : uuid, | ||
|
r4361 | fixConsole : fixConsole, | ||
|
r7357 | fixCarriageReturn : fixCarriageReturn, | ||
|
r8528 | autoLinkUrls : autoLinkUrls, | ||
|
r9227 | points_to_pixels : points_to_pixels, | ||
|
r15240 | get_body_data : get_body_data, | ||
|
r15241 | parse_url : parse_url, | ||
|
r13063 | url_path_join : url_path_join, | ||
|
r13686 | url_join_encode : url_join_encode, | ||
encode_uri_components : encode_uri_components, | ||||
|
r13120 | splitext : splitext, | ||
|
r15331 | escape_html : escape_html, | ||
|
r13206 | always_new : always_new, | ||
|
r14033 | browser : browser, | ||
|
r14816 | platform: platform, | ||
|
r14033 | is_or_has : is_or_has, | ||
|
r16629 | is_focused : is_focused, | ||
|
r17830 | ajax_error_msg : ajax_error_msg, | ||
|
r16629 | log_ajax_error : log_ajax_error, | ||
|
r5479 | }; | ||
|
r4352 | |||
}(IPython)); | ||||
|
r9227 | |||