Show More
@@ -1,101 +1,101 | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // Utilities |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | IPython.namespace('IPython.utils') |
|
13 | 13 | |
|
14 | 14 | IPython.utils = (function (IPython) { |
|
15 | 15 | |
|
16 | 16 | var uuid = function () { |
|
17 | 17 | // http://www.ietf.org/rfc/rfc4122.txt |
|
18 | 18 | var s = []; |
|
19 | 19 | var hexDigits = "0123456789ABCDEF"; |
|
20 | 20 | for (var i = 0; i < 32; i++) { |
|
21 | 21 | s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); |
|
22 | 22 | } |
|
23 | 23 | s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 |
|
24 | 24 | s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 |
|
25 | 25 | |
|
26 | 26 | var uuid = s.join(""); |
|
27 | 27 | return uuid; |
|
28 | 28 | }; |
|
29 | 29 | |
|
30 | 30 | |
|
31 | 31 | //Fix raw text to parse correctly in crazy XML |
|
32 | 32 | function xmlencode(string) { |
|
33 | 33 | return string.replace(/\&/g,'&'+'amp;') |
|
34 | 34 | .replace(/</g,'&'+'lt;') |
|
35 | 35 | .replace(/>/g,'&'+'gt;') |
|
36 | 36 | .replace(/\'/g,'&'+'apos;') |
|
37 | 37 | .replace(/\"/g,'&'+'quot;') |
|
38 | 38 | .replace(/`/g,'&'+'#96;') |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | //Map from terminal commands to CSS classes |
|
43 | 43 | ansi_colormap = { |
|
44 | 44 | "30":"ansiblack", "31":"ansired", |
|
45 | 45 | "32":"ansigreen", "33":"ansiyellow", |
|
46 | 46 | "34":"ansiblue", "35":"ansipurple","36":"ansicyan", |
|
47 | 47 | "37":"ansigrey", "01":"ansibold" |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | // Transform ANI color escape codes into HTML <span> tags with css |
|
51 | 51 | // classes listed in the above ansi_colormap object. The actual color used |
|
52 | 52 | // are set in the css file. |
|
53 | 53 | function fixConsole(txt) { |
|
54 | 54 | txt = xmlencode(txt) |
|
55 | 55 | var re = /\033\[([\d;]*?)m/ |
|
56 | 56 | var opened = false |
|
57 | 57 | var cmds = [] |
|
58 | 58 | var opener = "" |
|
59 | 59 | var closer = "" |
|
60 | 60 | |
|
61 | 61 | while (re.test(txt)) { |
|
62 | 62 | var cmds = txt.match(re)[1].split(";") |
|
63 | 63 | closer = opened?"</span>":"" |
|
64 | 64 | opened = cmds.length > 1 || cmds[0] != 0 |
|
65 | 65 | var rep = [] |
|
66 | 66 | for (var i in cmds) |
|
67 | 67 | if (typeof(ansi_colormap[cmds[i]]) != "undefined") |
|
68 | 68 | rep.push(ansi_colormap[cmds[i]]) |
|
69 | 69 | opener = rep.length > 0?"<span class=\""+rep.join(" ")+"\">":"" |
|
70 | 70 | txt = txt.replace(re, closer + opener) |
|
71 | 71 | } |
|
72 | 72 | if (opened) txt += "</span>" |
|
73 |
return txt |
|
|
73 | return txt | |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | |
|
77 | 77 | grow = function(element) { |
|
78 | 78 | // Grow the cell by hand. This is used upon reloading from JSON, when the |
|
79 | 79 | // autogrow handler is not called. |
|
80 | 80 | var dom = element.get(0); |
|
81 | 81 | var lines_count = 0; |
|
82 | 82 | // modified split rule from |
|
83 | 83 | // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 |
|
84 | 84 | var lines = dom.value.split(/\r|\r\n|\n/); |
|
85 | 85 | lines_count = lines.length; |
|
86 | 86 | if (lines_count >= 1) { |
|
87 | 87 | dom.rows = lines_count; |
|
88 | 88 | } else { |
|
89 | 89 | dom.rows = 1; |
|
90 | 90 | } |
|
91 | 91 | }; |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | return { |
|
95 | 95 | uuid : uuid, |
|
96 | 96 | fixConsole : fixConsole, |
|
97 | 97 | grow : grow |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | }(IPython)); |
|
101 | 101 |
General Comments 0
You need to be logged in to leave comments.
Login now