Show More
@@ -1,149 +1,149 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
2 | // Copyright (C) 2008-2011 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 |
|
11 | |||
12 | IPython.namespace('IPython.utils'); |
|
12 | IPython.namespace('IPython.utils'); | |
13 |
|
13 | |||
14 | IPython.utils = (function (IPython) { |
|
14 | IPython.utils = (function (IPython) { | |
15 |
|
15 | |||
16 | var uuid = function () { |
|
16 | var uuid = function () { | |
17 | // http://www.ietf.org/rfc/rfc4122.txt |
|
17 | // http://www.ietf.org/rfc/rfc4122.txt | |
18 | var s = []; |
|
18 | var s = []; | |
19 | var hexDigits = "0123456789ABCDEF"; |
|
19 | var hexDigits = "0123456789ABCDEF"; | |
20 | for (var i = 0; i < 32; i++) { |
|
20 | for (var i = 0; i < 32; i++) { | |
21 | s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); |
|
21 | s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); | |
22 | } |
|
22 | } | |
23 | s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 |
|
23 | s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 | |
24 | s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 |
|
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 | var uuid = s.join(""); |
|
26 | var uuid = s.join(""); | |
27 | return uuid; |
|
27 | return uuid; | |
28 | }; |
|
28 | }; | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | //Fix raw text to parse correctly in crazy XML |
|
31 | //Fix raw text to parse correctly in crazy XML | |
32 | function xmlencode(string) { |
|
32 | function xmlencode(string) { | |
33 | return string.replace(/\&/g,'&'+'amp;') |
|
33 | return string.replace(/\&/g,'&'+'amp;') | |
34 | .replace(/</g,'&'+'lt;') |
|
34 | .replace(/</g,'&'+'lt;') | |
35 | .replace(/>/g,'&'+'gt;') |
|
35 | .replace(/>/g,'&'+'gt;') | |
36 | .replace(/\'/g,'&'+'apos;') |
|
36 | .replace(/\'/g,'&'+'apos;') | |
37 | .replace(/\"/g,'&'+'quot;') |
|
37 | .replace(/\"/g,'&'+'quot;') | |
38 | .replace(/`/g,'&'+'#96;'); |
|
38 | .replace(/`/g,'&'+'#96;'); | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | //Map from terminal commands to CSS classes |
|
42 | //Map from terminal commands to CSS classes | |
43 | ansi_colormap = { |
|
43 | ansi_colormap = { | |
44 | "30":"ansiblack", "31":"ansired", |
|
44 | "30":"ansiblack", "31":"ansired", | |
45 | "32":"ansigreen", "33":"ansiyellow", |
|
45 | "32":"ansigreen", "33":"ansiyellow", | |
46 | "34":"ansiblue", "35":"ansipurple","36":"ansicyan", |
|
46 | "34":"ansiblue", "35":"ansipurple","36":"ansicyan", | |
47 | "37":"ansigrey", "01":"ansibold" |
|
47 | "37":"ansigrey", "01":"ansibold" | |
48 | }; |
|
48 | }; | |
49 |
|
49 | |||
50 | // Transform ANSI color escape codes into HTML <span> tags with css |
|
50 | // Transform ANSI color escape codes into HTML <span> tags with css | |
51 | // classes listed in the above ansi_colormap object. The actual color used |
|
51 | // classes listed in the above ansi_colormap object. The actual color used | |
52 | // are set in the css file. |
|
52 | // are set in the css file. | |
53 | function fixConsole(txt) { |
|
53 | function fixConsole(txt) { | |
54 | txt = xmlencode(txt); |
|
54 | txt = xmlencode(txt); | |
55 | var re = /\033\[([\dA-Fa-f;]*?)m/; |
|
55 | var re = /\033\[([\dA-Fa-f;]*?)m/; | |
56 | var opened = false; |
|
56 | var opened = false; | |
57 | var cmds = []; |
|
57 | var cmds = []; | |
58 | var opener = ""; |
|
58 | var opener = ""; | |
59 | var closer = ""; |
|
59 | var closer = ""; | |
60 | while (re.test(txt)) { |
|
60 | while (re.test(txt)) { | |
61 | var cmds = txt.match(re)[1].split(";"); |
|
61 | var cmds = txt.match(re)[1].split(";"); | |
62 | closer = opened?"</span>":""; |
|
62 | closer = opened?"</span>":""; | |
63 | opened = cmds.length > 1 || cmds[0] != 0; |
|
63 | opened = cmds.length > 1 || cmds[0] != 0; | |
64 | var rep = []; |
|
64 | var rep = []; | |
65 | for (var i in cmds) |
|
65 | for (var i in cmds) | |
66 | if (typeof(ansi_colormap[cmds[i]]) != "undefined") |
|
66 | if (typeof(ansi_colormap[cmds[i]]) != "undefined") | |
67 | rep.push(ansi_colormap[cmds[i]]); |
|
67 | rep.push(ansi_colormap[cmds[i]]); | |
68 | opener = rep.length > 0?"<span class=\""+rep.join(" ")+"\">":""; |
|
68 | opener = rep.length > 0?"<span class=\""+rep.join(" ")+"\">":""; | |
69 | txt = txt.replace(re, closer + opener); |
|
69 | txt = txt.replace(re, closer + opener); | |
70 | } |
|
70 | } | |
71 | if (opened) txt += "</span>"; |
|
71 | if (opened) txt += "</span>"; | |
72 | return txt; |
|
72 | return txt; | |
73 | } |
|
73 | } | |
74 |
|
74 | |||
75 | // Remove chunks that should be overridden by the effect of |
|
75 | // Remove chunks that should be overridden by the effect of | |
76 | // carriage return characters |
|
76 | // carriage return characters | |
77 | function fixCarriageReturn(txt) { |
|
77 | function fixCarriageReturn(txt) { | |
78 | tmp = txt; |
|
78 | tmp = txt; | |
79 | do { |
|
79 | do { | |
80 | txt = tmp; |
|
80 | txt = tmp; | |
81 | tmp = txt.replace(/^.*\r(?!\n)/gm, ''); |
|
81 | tmp = txt.replace(/^.*\r(?!\n)/gm, ''); | |
82 | } while (tmp.length < txt.length); |
|
82 | } while (tmp.length < txt.length); | |
83 | return txt; |
|
83 | return txt; | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | grow = function(element) { |
|
86 | grow = function(element) { | |
87 | // Grow the cell by hand. This is used upon reloading from JSON, when the |
|
87 | // Grow the cell by hand. This is used upon reloading from JSON, when the | |
88 | // autogrow handler is not called. |
|
88 | // autogrow handler is not called. | |
89 | var dom = element.get(0); |
|
89 | var dom = element.get(0); | |
90 | var lines_count = 0; |
|
90 | var lines_count = 0; | |
91 | // modified split rule from |
|
91 | // modified split rule from | |
92 | // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 |
|
92 | // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 | |
93 | var lines = dom.value.split(/\r|\r\n|\n/); |
|
93 | var lines = dom.value.split(/\r|\r\n|\n/); | |
94 | lines_count = lines.length; |
|
94 | lines_count = lines.length; | |
95 | if (lines_count >= 1) { |
|
95 | if (lines_count >= 1) { | |
96 | dom.rows = lines_count; |
|
96 | dom.rows = lines_count; | |
97 | } else { |
|
97 | } else { | |
98 | dom.rows = 1; |
|
98 | dom.rows = 1; | |
99 | } |
|
99 | } | |
100 | }; |
|
100 | }; | |
101 |
|
101 | |||
102 | // some keycodes that seem to be platform/browser independant |
|
102 | // some keycodes that seem to be platform/browser independant | |
103 | var keycodes ={ |
|
103 | var keycodes ={ | |
104 | BACKSPACE: 8, |
|
104 | BACKSPACE: 8, | |
105 | TAB : 9, |
|
105 | TAB : 9, | |
106 | ENTER : 13, |
|
106 | ENTER : 13, | |
107 | SHIFT : 16, |
|
107 | SHIFT : 16, | |
108 | CTRL : 17, |
|
108 | CTRL : 17, | |
109 | CONTROL : 17, |
|
109 | CONTROL : 17, | |
110 | ALT : 18, |
|
110 | ALT : 18, | |
111 | ESC : 27, |
|
111 | ESC : 27, | |
112 | SPACE : 32, |
|
112 | SPACE : 32, | |
113 | PGUP : 33, |
|
113 | PGUP : 33, | |
114 | PGDOWN : 34, |
|
114 | PGDOWN : 34, | |
115 | LEFT_ARROW: 37, |
|
115 | LEFT_ARROW: 37, | |
116 | LEFTARROW: 37, |
|
116 | LEFTARROW: 37, | |
117 | LEFT : 37, |
|
117 | LEFT : 37, | |
118 | UP_ARROW : 38, |
|
118 | UP_ARROW : 38, | |
119 | UPARROW : 38, |
|
119 | UPARROW : 38, | |
120 | UP : 38, |
|
120 | UP : 38, | |
121 | RIGHT_ARROW:39, |
|
121 | RIGHT_ARROW:39, | |
122 | RIGHTARROW:39, |
|
122 | RIGHTARROW:39, | |
123 | RIGHT : 39, |
|
123 | RIGHT : 39, | |
124 | DOWN_ARROW: 40, |
|
124 | DOWN_ARROW: 40, | |
125 | DOWNARROW: 40, |
|
125 | DOWNARROW: 40, | |
126 | DOWN : 40, |
|
126 | DOWN : 40, | |
127 | }; |
|
127 | }; | |
128 |
|
128 | |||
129 |
|
129 | |||
130 | points_to_pixels = function (points) { |
|
130 | points_to_pixels = function (points) { | |
131 | // A reasonably good way of converting between points and pixels. |
|
131 | // A reasonably good way of converting between points and pixels. | |
132 | var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>'); |
|
132 | var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>'); | |
133 | $(body).append(test); |
|
133 | $(body).append(test); | |
134 | var pixel_per_point = test.width()/10000; |
|
134 | var pixel_per_point = test.width()/10000; | |
135 | test.remove(); |
|
135 | test.remove(); | |
136 | return Math.floor(points*pixel_per_point); |
|
136 | return Math.floor(points*pixel_per_point); | |
137 | } |
|
137 | } | |
138 |
|
138 | |||
139 |
|
139 | |||
140 | return { |
|
140 | return { | |
141 | uuid : uuid, |
|
141 | uuid : uuid, | |
142 | fixConsole : fixConsole, |
|
142 | fixConsole : fixConsole, | |
143 | keycodes : keycodes, |
|
143 | keycodes : keycodes, | |
144 | grow : grow, |
|
144 | grow : grow, | |
145 | fixCarriageReturn : fixCarriageReturn |
|
145 | fixCarriageReturn : fixCarriageReturn, | |
146 | points_to_pixels : points_to_pixels |
|
146 | points_to_pixels : points_to_pixels | |
147 | }; |
|
147 | }; | |
148 |
|
148 | |||
149 | }(IPython)); |
|
149 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now