Show More
@@ -0,0 +1,54 b'' | |||||
|
1 | // Node.js script for markdown to html conversion | |||
|
2 | // This applies the same math extraction and marked settings | |||
|
3 | // that we use in the live notebook. | |||
|
4 | ||||
|
5 | // IPython static_path dir relative to here: | |||
|
6 | var static_path = __dirname + "/../../html/static/"; | |||
|
7 | ||||
|
8 | var fs = require('fs'); | |||
|
9 | var IPython; | |||
|
10 | // marked can be loaded with require, | |||
|
11 | // the others must be execfiled | |||
|
12 | var marked = require(static_path + 'components/marked/lib/marked.js'); | |||
|
13 | ||||
|
14 | eval(fs.readFileSync(static_path + "components/highlight.js/build/highlight.pack.js", 'utf8')); | |||
|
15 | eval(fs.readFileSync(static_path + "base/js/namespace.js", 'utf8')); | |||
|
16 | ||||
|
17 | eval(fs.readFileSync(static_path + "base/js/utils.js", 'utf8')); | |||
|
18 | eval(fs.readFileSync(static_path + "notebook/js/mathjaxutils.js", 'utf8')); | |||
|
19 | ||||
|
20 | // this is copied from notebook.main. Should it be moved somewhere we can reuse it? | |||
|
21 | marked.setOptions({ | |||
|
22 | gfm : true, | |||
|
23 | tables: true, | |||
|
24 | langPrefix: "language-", | |||
|
25 | highlight: function(code, lang) { | |||
|
26 | if (!lang) { | |||
|
27 | // no language, no highlight | |||
|
28 | return code; | |||
|
29 | } | |||
|
30 | var highlighted; | |||
|
31 | try { | |||
|
32 | highlighted = hljs.highlight(lang, code, false); | |||
|
33 | } catch(err) { | |||
|
34 | highlighted = hljs.highlightAuto(code); | |||
|
35 | } | |||
|
36 | return highlighted.value; | |||
|
37 | } | |||
|
38 | }); | |||
|
39 | ||||
|
40 | // read the markdown from stdin | |||
|
41 | var md=''; | |||
|
42 | process.stdin.on("data", function (data) { | |||
|
43 | md += data; | |||
|
44 | }); | |||
|
45 | ||||
|
46 | // perform the md2html transform once stdin is complete | |||
|
47 | process.stdin.on("end", function () { | |||
|
48 | var text_and_math = IPython.mathjaxutils.remove_math(md); | |||
|
49 | var text = text_and_math[0]; | |||
|
50 | var math = text_and_math[1]; | |||
|
51 | var html = marked.parser(marked.lexer(text)); | |||
|
52 | html = IPython.mathjaxutils.replace_math(html, math); | |||
|
53 | process.stdout.write(html); | |||
|
54 | }); |
@@ -1,504 +1,508 b'' | |||||
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 | I : 73, |
|
353 | I : 73, | |
354 | M : 77, |
|
354 | M : 77, | |
355 | // all three of these keys may be COMMAND on OS X: |
|
355 | // all three of these keys may be COMMAND on OS X: | |
356 | LEFT_SUPER : 91, |
|
356 | LEFT_SUPER : 91, | |
357 | RIGHT_SUPER : 92, |
|
357 | RIGHT_SUPER : 92, | |
358 | COMMAND : 93, |
|
358 | COMMAND : 93, | |
359 | }; |
|
359 | }; | |
360 |
|
360 | |||
361 | // trigger a key press event |
|
361 | // trigger a key press event | |
362 | var press = function (key) { |
|
362 | var press = function (key) { | |
363 | var key_press = $.Event('keydown', {which: key}); |
|
363 | var key_press = $.Event('keydown', {which: key}); | |
364 | $(document).trigger(key_press); |
|
364 | $(document).trigger(key_press); | |
365 | } |
|
365 | } | |
366 |
|
366 | |||
367 | var press_up = function() { press(keycodes.UP); }; |
|
367 | var press_up = function() { press(keycodes.UP); }; | |
368 | var press_down = function() { press(keycodes.DOWN); }; |
|
368 | var press_down = function() { press(keycodes.DOWN); }; | |
369 |
|
369 | |||
370 | var press_ctrl_enter = function() { |
|
370 | var press_ctrl_enter = function() { | |
371 | $(document).trigger($.Event('keydown', {which: keycodes.ENTER, ctrlKey: true})); |
|
371 | $(document).trigger($.Event('keydown', {which: keycodes.ENTER, ctrlKey: true})); | |
372 | }; |
|
372 | }; | |
373 |
|
373 | |||
374 | var press_shift_enter = function() { |
|
374 | var press_shift_enter = function() { | |
375 | $(document).trigger($.Event('keydown', {which: keycodes.ENTER, shiftKey: true})); |
|
375 | $(document).trigger($.Event('keydown', {which: keycodes.ENTER, shiftKey: true})); | |
376 | }; |
|
376 | }; | |
377 |
|
377 | |||
378 | // trigger the ctrl-m shortcut followed by one of our keys |
|
378 | // trigger the ctrl-m shortcut followed by one of our keys | |
379 | var press_ghetto = function(key) { |
|
379 | var press_ghetto = function(key) { | |
380 | $(document).trigger($.Event('keydown', {which: keycodes.M, ctrlKey: true})); |
|
380 | $(document).trigger($.Event('keydown', {which: keycodes.M, ctrlKey: true})); | |
381 | press(key); |
|
381 | press(key); | |
382 | }; |
|
382 | }; | |
383 |
|
383 | |||
384 |
|
384 | |||
385 | var points_to_pixels = function (points) { |
|
385 | var points_to_pixels = function (points) { | |
386 | // A reasonably good way of converting between points and pixels. |
|
386 | // A reasonably good way of converting between points and pixels. | |
387 | var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>'); |
|
387 | var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>'); | |
388 | $(body).append(test); |
|
388 | $(body).append(test); | |
389 | var pixel_per_point = test.width()/10000; |
|
389 | var pixel_per_point = test.width()/10000; | |
390 | test.remove(); |
|
390 | test.remove(); | |
391 | return Math.floor(points*pixel_per_point); |
|
391 | return Math.floor(points*pixel_per_point); | |
392 | }; |
|
392 | }; | |
393 |
|
393 | |||
394 | var always_new = function (constructor) { |
|
394 | var always_new = function (constructor) { | |
395 | // wrapper around contructor to avoid requiring `var a = new constructor()` |
|
395 | // wrapper around contructor to avoid requiring `var a = new constructor()` | |
396 | // useful for passing constructors as callbacks, |
|
396 | // useful for passing constructors as callbacks, | |
397 | // not for programmer laziness. |
|
397 | // not for programmer laziness. | |
398 | // from http://programmers.stackexchange.com/questions/118798 |
|
398 | // from http://programmers.stackexchange.com/questions/118798 | |
399 | return function () { |
|
399 | return function () { | |
400 | var obj = Object.create(constructor.prototype); |
|
400 | var obj = Object.create(constructor.prototype); | |
401 | constructor.apply(obj, arguments); |
|
401 | constructor.apply(obj, arguments); | |
402 | return obj; |
|
402 | return obj; | |
403 | }; |
|
403 | }; | |
404 | }; |
|
404 | }; | |
405 |
|
405 | |||
406 |
|
406 | |||
407 | var url_path_join = function () { |
|
407 | var url_path_join = function () { | |
408 | // join a sequence of url components with '/' |
|
408 | // join a sequence of url components with '/' | |
409 | var url = ''; |
|
409 | var url = ''; | |
410 | for (var i = 0; i < arguments.length; i++) { |
|
410 | for (var i = 0; i < arguments.length; i++) { | |
411 | if (arguments[i] === '') { |
|
411 | if (arguments[i] === '') { | |
412 | continue; |
|
412 | continue; | |
413 | } |
|
413 | } | |
414 | if (url.length > 0 && url[url.length-1] != '/') { |
|
414 | if (url.length > 0 && url[url.length-1] != '/') { | |
415 | url = url + '/' + arguments[i]; |
|
415 | url = url + '/' + arguments[i]; | |
416 | } else { |
|
416 | } else { | |
417 | url = url + arguments[i]; |
|
417 | url = url + arguments[i]; | |
418 | } |
|
418 | } | |
419 | } |
|
419 | } | |
420 | return url; |
|
420 | return url; | |
421 | }; |
|
421 | }; | |
422 |
|
422 | |||
423 |
|
423 | |||
424 | var encode_uri_components = function (uri) { |
|
424 | var encode_uri_components = function (uri) { | |
425 | // encode just the components of a multi-segment uri, |
|
425 | // encode just the components of a multi-segment uri, | |
426 | // leaving '/' separators |
|
426 | // leaving '/' separators | |
427 | return uri.split('/').map(encodeURIComponent).join('/'); |
|
427 | return uri.split('/').map(encodeURIComponent).join('/'); | |
428 | } |
|
428 | } | |
429 |
|
429 | |||
430 | var url_join_encode = function () { |
|
430 | var url_join_encode = function () { | |
431 | // join a sequence of url components with '/', |
|
431 | // join a sequence of url components with '/', | |
432 | // encoding each component with encodeURIComponent |
|
432 | // encoding each component with encodeURIComponent | |
433 | return encode_uri_components(url_path_join.apply(null, arguments)); |
|
433 | return encode_uri_components(url_path_join.apply(null, arguments)); | |
434 | }; |
|
434 | }; | |
435 |
|
435 | |||
436 |
|
436 | |||
437 | var splitext = function (filename) { |
|
437 | var splitext = function (filename) { | |
438 | // mimic Python os.path.splitext |
|
438 | // mimic Python os.path.splitext | |
439 | // Returns ['base', '.ext'] |
|
439 | // Returns ['base', '.ext'] | |
440 | var idx = filename.lastIndexOf('.'); |
|
440 | var idx = filename.lastIndexOf('.'); | |
441 | if (idx > 0) { |
|
441 | if (idx > 0) { | |
442 | return [filename.slice(0, idx), filename.slice(idx)]; |
|
442 | return [filename.slice(0, idx), filename.slice(idx)]; | |
443 | } else { |
|
443 | } else { | |
444 | return [filename, '']; |
|
444 | return [filename, '']; | |
445 | } |
|
445 | } | |
446 | } |
|
446 | } | |
447 |
|
447 | |||
448 |
|
448 | |||
449 | // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript |
|
449 | // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript | |
450 | var browser = (function() { |
|
450 | var browser = (function() { | |
|
451 | if (typeof navigator === 'undefined') { | |||
|
452 | // navigator undefined in node | |||
|
453 | return 'None'; | |||
|
454 | } | |||
451 | var N= navigator.appName, ua= navigator.userAgent, tem; |
|
455 | var N= navigator.appName, ua= navigator.userAgent, tem; | |
452 | var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); |
|
456 | var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); | |
453 | if (M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1]; |
|
457 | if (M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1]; | |
454 | M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?']; |
|
458 | M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?']; | |
455 | return M; |
|
459 | return M; | |
456 | })(); |
|
460 | })(); | |
457 |
|
461 | |||
458 | var is_or_has = function (a, b) { |
|
462 | var is_or_has = function (a, b) { | |
459 | // Is b a child of a or a itself? |
|
463 | // Is b a child of a or a itself? | |
460 | return a.has(b).length !==0 || a.is(b); |
|
464 | return a.has(b).length !==0 || a.is(b); | |
461 | } |
|
465 | } | |
462 |
|
466 | |||
463 | var is_focused = function (e) { |
|
467 | var is_focused = function (e) { | |
464 | // Is element e, or one of its children focused? |
|
468 | // Is element e, or one of its children focused? | |
465 | e = $(e); |
|
469 | e = $(e); | |
466 | var target = $(document.activeElement); |
|
470 | var target = $(document.activeElement); | |
467 | if (target.length > 0) { |
|
471 | if (target.length > 0) { | |
468 | if (is_or_has(e, target)) { |
|
472 | if (is_or_has(e, target)) { | |
469 | return true; |
|
473 | return true; | |
470 | } else { |
|
474 | } else { | |
471 | return false; |
|
475 | return false; | |
472 | } |
|
476 | } | |
473 | } else { |
|
477 | } else { | |
474 | return false; |
|
478 | return false; | |
475 | } |
|
479 | } | |
476 | } |
|
480 | } | |
477 |
|
481 | |||
478 |
|
482 | |||
479 | return { |
|
483 | return { | |
480 | regex_split : regex_split, |
|
484 | regex_split : regex_split, | |
481 | uuid : uuid, |
|
485 | uuid : uuid, | |
482 | fixConsole : fixConsole, |
|
486 | fixConsole : fixConsole, | |
483 | keycodes : keycodes, |
|
487 | keycodes : keycodes, | |
484 | press : press, |
|
488 | press : press, | |
485 | press_up : press_up, |
|
489 | press_up : press_up, | |
486 | press_down : press_down, |
|
490 | press_down : press_down, | |
487 | press_ctrl_enter : press_ctrl_enter, |
|
491 | press_ctrl_enter : press_ctrl_enter, | |
488 | press_shift_enter : press_shift_enter, |
|
492 | press_shift_enter : press_shift_enter, | |
489 | press_ghetto : press_ghetto, |
|
493 | press_ghetto : press_ghetto, | |
490 | fixCarriageReturn : fixCarriageReturn, |
|
494 | fixCarriageReturn : fixCarriageReturn, | |
491 | autoLinkUrls : autoLinkUrls, |
|
495 | autoLinkUrls : autoLinkUrls, | |
492 | points_to_pixels : points_to_pixels, |
|
496 | points_to_pixels : points_to_pixels, | |
493 | url_path_join : url_path_join, |
|
497 | url_path_join : url_path_join, | |
494 | url_join_encode : url_join_encode, |
|
498 | url_join_encode : url_join_encode, | |
495 | encode_uri_components : encode_uri_components, |
|
499 | encode_uri_components : encode_uri_components, | |
496 | splitext : splitext, |
|
500 | splitext : splitext, | |
497 | always_new : always_new, |
|
501 | always_new : always_new, | |
498 | browser : browser, |
|
502 | browser : browser, | |
499 | is_or_has : is_or_has, |
|
503 | is_or_has : is_or_has, | |
500 | is_focused : is_focused |
|
504 | is_focused : is_focused | |
501 | }; |
|
505 | }; | |
502 |
|
506 | |||
503 | }(IPython)); |
|
507 | }(IPython)); | |
504 |
|
508 |
@@ -1,259 +1,251 b'' | |||||
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 | // MathJax utility functions |
|
9 | // MathJax utility functions | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 |
|
12 | |||
13 | IPython.namespace('IPython.mathjaxutils'); |
|
13 | IPython.namespace('IPython.mathjaxutils'); | |
14 |
|
14 | |||
15 | IPython.mathjaxutils = (function (IPython) { |
|
15 | IPython.mathjaxutils = (function (IPython) { | |
16 | "use strict"; |
|
16 | "use strict"; | |
17 |
|
17 | |||
18 | var init = function () { |
|
18 | var init = function () { | |
19 | if (window.MathJax) { |
|
19 | if (window.MathJax) { | |
20 | // MathJax loaded |
|
20 | // MathJax loaded | |
21 | MathJax.Hub.Config({ |
|
21 | MathJax.Hub.Config({ | |
22 | tex2jax: { |
|
22 | tex2jax: { | |
23 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], |
|
23 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], | |
24 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ], |
|
24 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ], | |
25 | processEscapes: true, |
|
25 | processEscapes: true, | |
26 | processEnvironments: true |
|
26 | processEnvironments: true | |
27 | }, |
|
27 | }, | |
28 | // Center justify equations in code and markdown cells. Elsewhere |
|
28 | // Center justify equations in code and markdown cells. Elsewhere | |
29 | // we use CSS to left justify single line equations in code cells. |
|
29 | // we use CSS to left justify single line equations in code cells. | |
30 | displayAlign: 'center', |
|
30 | displayAlign: 'center', | |
31 | "HTML-CSS": { |
|
31 | "HTML-CSS": { | |
32 | styles: {'.MathJax_Display': {"margin": 0}}, |
|
32 | styles: {'.MathJax_Display': {"margin": 0}}, | |
33 | linebreaks: { automatic: true } |
|
33 | linebreaks: { automatic: true } | |
34 | } |
|
34 | } | |
35 | }); |
|
35 | }); | |
36 | MathJax.Hub.Configured(); |
|
36 | MathJax.Hub.Configured(); | |
37 | } else if (window.mathjax_url !== "") { |
|
37 | } else if (window.mathjax_url !== "") { | |
38 | // Don't have MathJax, but should. Show dialog. |
|
38 | // Don't have MathJax, but should. Show dialog. | |
39 | var message = $('<div/>') |
|
39 | var message = $('<div/>') | |
40 | .append( |
|
40 | .append( | |
41 | $("<p/></p>").addClass('dialog').text( |
|
41 | $("<p/></p>").addClass('dialog').text( | |
42 | "Math/LaTeX rendering will be disabled." |
|
42 | "Math/LaTeX rendering will be disabled." | |
43 | ) |
|
43 | ) | |
44 | ).append( |
|
44 | ).append( | |
45 | $("<p></p>").addClass('dialog').text( |
|
45 | $("<p></p>").addClass('dialog').text( | |
46 | "If you have administrative access to the notebook server and" + |
|
46 | "If you have administrative access to the notebook server and" + | |
47 | " a working internet connection, you can install a local copy" + |
|
47 | " a working internet connection, you can install a local copy" + | |
48 | " of MathJax for offline use with the following command on the server" + |
|
48 | " of MathJax for offline use with the following command on the server" + | |
49 | " at a Python or IPython prompt:" |
|
49 | " at a Python or IPython prompt:" | |
50 | ) |
|
50 | ) | |
51 | ).append( |
|
51 | ).append( | |
52 | $("<pre></pre>").addClass('dialog').text( |
|
52 | $("<pre></pre>").addClass('dialog').text( | |
53 | ">>> from IPython.external import mathjax; mathjax.install_mathjax()" |
|
53 | ">>> from IPython.external import mathjax; mathjax.install_mathjax()" | |
54 | ) |
|
54 | ) | |
55 | ).append( |
|
55 | ).append( | |
56 | $("<p></p>").addClass('dialog').text( |
|
56 | $("<p></p>").addClass('dialog').text( | |
57 | "This will try to install MathJax into the IPython source directory." |
|
57 | "This will try to install MathJax into the IPython source directory." | |
58 | ) |
|
58 | ) | |
59 | ).append( |
|
59 | ).append( | |
60 | $("<p></p>").addClass('dialog').text( |
|
60 | $("<p></p>").addClass('dialog').text( | |
61 | "If IPython is installed to a location that requires" + |
|
61 | "If IPython is installed to a location that requires" + | |
62 | " administrative privileges to write, you will need to make this call as" + |
|
62 | " administrative privileges to write, you will need to make this call as" + | |
63 | " an administrator, via 'sudo'." |
|
63 | " an administrator, via 'sudo'." | |
64 | ) |
|
64 | ) | |
65 | ).append( |
|
65 | ).append( | |
66 | $("<p></p>").addClass('dialog').text( |
|
66 | $("<p></p>").addClass('dialog').text( | |
67 | "When you start the notebook server, you can instruct it to disable MathJax support altogether:" |
|
67 | "When you start the notebook server, you can instruct it to disable MathJax support altogether:" | |
68 | ) |
|
68 | ) | |
69 | ).append( |
|
69 | ).append( | |
70 | $("<pre></pre>").addClass('dialog').text( |
|
70 | $("<pre></pre>").addClass('dialog').text( | |
71 | "$ ipython notebook --no-mathjax" |
|
71 | "$ ipython notebook --no-mathjax" | |
72 | ) |
|
72 | ) | |
73 | ).append( |
|
73 | ).append( | |
74 | $("<p></p>").addClass('dialog').text( |
|
74 | $("<p></p>").addClass('dialog').text( | |
75 | "which will prevent this dialog from appearing." |
|
75 | "which will prevent this dialog from appearing." | |
76 | ) |
|
76 | ) | |
77 | ); |
|
77 | ); | |
78 | IPython.dialog.modal({ |
|
78 | IPython.dialog.modal({ | |
79 | title : "Failed to retrieve MathJax from '" + window.mathjax_url + "'", |
|
79 | title : "Failed to retrieve MathJax from '" + window.mathjax_url + "'", | |
80 | body : message, |
|
80 | body : message, | |
81 | buttons : { |
|
81 | buttons : { | |
82 | OK : {class: "btn-danger"} |
|
82 | OK : {class: "btn-danger"} | |
83 | } |
|
83 | } | |
84 | }); |
|
84 | }); | |
85 | } |
|
85 | } | |
86 | }; |
|
86 | }; | |
87 |
|
87 | |||
88 | // Some magic for deferring mathematical expressions to MathJax |
|
88 | // Some magic for deferring mathematical expressions to MathJax | |
89 | // by hiding them from the Markdown parser. |
|
89 | // by hiding them from the Markdown parser. | |
90 | // Some of the code here is adapted with permission from Davide Cervone |
|
90 | // Some of the code here is adapted with permission from Davide Cervone | |
91 | // under the terms of the Apache2 license governing the MathJax project. |
|
91 | // under the terms of the Apache2 license governing the MathJax project. | |
92 | // Other minor modifications are also due to StackExchange and are used with |
|
92 | // Other minor modifications are also due to StackExchange and are used with | |
93 | // permission. |
|
93 | // permission. | |
94 |
|
94 | |||
95 | var inline = "$"; // the inline math delimiter |
|
95 | var inline = "$"; // the inline math delimiter | |
96 |
|
96 | |||
97 | // MATHSPLIT contains the pattern for math delimiters and special symbols |
|
97 | // MATHSPLIT contains the pattern for math delimiters and special symbols | |
98 | // needed for searching for math in the text input. |
|
98 | // needed for searching for math in the text input. | |
99 | var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@)/i; |
|
99 | var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@)/i; | |
100 |
|
100 | |||
101 | // The math is in blocks i through j, so |
|
101 | // The math is in blocks i through j, so | |
102 | // collect it into one block and clear the others. |
|
102 | // collect it into one block and clear the others. | |
103 | // Replace &, <, and > by named entities. |
|
103 | // Replace &, <, and > by named entities. | |
104 | // For IE, put <br> at the ends of comments since IE removes \n. |
|
104 | // For IE, put <br> at the ends of comments since IE removes \n. | |
105 | // Clear the current math positions and store the index of the |
|
105 | // Clear the current math positions and store the index of the | |
106 | // math, then push the math string onto the storage array. |
|
106 | // math, then push the math string onto the storage array. | |
107 | // The preProcess function is called on all blocks if it has been passed in |
|
107 | // The preProcess function is called on all blocks if it has been passed in | |
108 | var process_math = function (i, j, pre_process, math, blocks) { |
|
108 | var process_math = function (i, j, pre_process, math, blocks) { | |
109 | var hub = MathJax.Hub; |
|
|||
110 | var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&") // use HTML entity for & |
|
109 | var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&") // use HTML entity for & | |
111 | .replace(/</g, "<") // use HTML entity for < |
|
110 | .replace(/</g, "<") // use HTML entity for < | |
112 | .replace(/>/g, ">") // use HTML entity for > |
|
111 | .replace(/>/g, ">") // use HTML entity for > | |
113 | ; |
|
112 | ; | |
114 |
if ( |
|
113 | if (IPython.utils.browser === 'msie') { | |
115 | block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n"); |
|
114 | block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n"); | |
116 | } |
|
115 | } | |
117 | while (j > i) { |
|
116 | while (j > i) { | |
118 | blocks[j] = ""; |
|
117 | blocks[j] = ""; | |
119 | j--; |
|
118 | j--; | |
120 | } |
|
119 | } | |
121 | blocks[i] = "@@" + math.length + "@@"; // replace the current block text with a unique tag to find later |
|
120 | blocks[i] = "@@" + math.length + "@@"; // replace the current block text with a unique tag to find later | |
122 | if (pre_process){ |
|
121 | if (pre_process){ | |
123 | block = pre_process(block); |
|
122 | block = pre_process(block); | |
124 | } |
|
123 | } | |
125 | math.push(block); |
|
124 | math.push(block); | |
126 | return blocks; |
|
125 | return blocks; | |
127 | }; |
|
126 | }; | |
128 |
|
127 | |||
129 | // Break up the text into its component parts and search |
|
128 | // Break up the text into its component parts and search | |
130 | // through them for math delimiters, braces, linebreaks, etc. |
|
129 | // through them for math delimiters, braces, linebreaks, etc. | |
131 | // Math delimiters must match and braces must balance. |
|
130 | // Math delimiters must match and braces must balance. | |
132 | // Don't allow math to pass through a double linebreak |
|
131 | // Don't allow math to pass through a double linebreak | |
133 | // (which will be a paragraph). |
|
132 | // (which will be a paragraph). | |
134 | // |
|
133 | // | |
135 | var remove_math = function (text) { |
|
134 | var remove_math = function (text) { | |
136 | if (!window.MathJax) { |
|
|||
137 | return [text, null]; |
|
|||
138 | } |
|
|||
139 |
|
||||
140 | var math = []; // stores math strings for later |
|
135 | var math = []; // stores math strings for later | |
141 | var start; |
|
136 | var start; | |
142 | var end; |
|
137 | var end; | |
143 | var last; |
|
138 | var last; | |
144 | var braces; |
|
139 | var braces; | |
145 |
|
140 | |||
146 | // Except for extreme edge cases, this should catch precisely those pieces of the markdown |
|
141 | // Except for extreme edge cases, this should catch precisely those pieces of the markdown | |
147 | // source that will later be turned into code spans. While MathJax will not TeXify code spans, |
|
142 | // source that will later be turned into code spans. While MathJax will not TeXify code spans, | |
148 | // we still have to consider them at this point; the following issue has happened several times: |
|
143 | // we still have to consider them at this point; the following issue has happened several times: | |
149 | // |
|
144 | // | |
150 | // `$foo` and `$bar` are varibales. --> <code>$foo ` and `$bar</code> are variables. |
|
145 | // `$foo` and `$bar` are varibales. --> <code>$foo ` and `$bar</code> are variables. | |
151 |
|
146 | |||
152 | var hasCodeSpans = /`/.test(text), |
|
147 | var hasCodeSpans = /`/.test(text), | |
153 | de_tilde; |
|
148 | de_tilde; | |
154 | if (hasCodeSpans) { |
|
149 | if (hasCodeSpans) { | |
155 | text = text.replace(/~/g, "~T").replace(/(^|[^\\])(`+)([^\n]*?[^`\n])\2(?!`)/gm, function (wholematch) { |
|
150 | text = text.replace(/~/g, "~T").replace(/(^|[^\\])(`+)([^\n]*?[^`\n])\2(?!`)/gm, function (wholematch) { | |
156 | return wholematch.replace(/\$/g, "~D"); |
|
151 | return wholematch.replace(/\$/g, "~D"); | |
157 | }); |
|
152 | }); | |
158 | de_tilde = function (text) { |
|
153 | de_tilde = function (text) { | |
159 | return text.replace(/~([TD])/g, function (wholematch, character) { |
|
154 | return text.replace(/~([TD])/g, function (wholematch, character) { | |
160 | return { T: "~", D: "$" }[character]; |
|
155 | return { T: "~", D: "$" }[character]; | |
161 | }); |
|
156 | }); | |
162 | }; |
|
157 | }; | |
163 | } else { |
|
158 | } else { | |
164 | de_tilde = function (text) { return text; }; |
|
159 | de_tilde = function (text) { return text; }; | |
165 | } |
|
160 | } | |
166 |
|
161 | |||
167 | var blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT); |
|
162 | var blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT); | |
168 |
|
163 | |||
169 | for (var i = 1, m = blocks.length; i < m; i += 2) { |
|
164 | for (var i = 1, m = blocks.length; i < m; i += 2) { | |
170 | var block = blocks[i]; |
|
165 | var block = blocks[i]; | |
171 | if (block.charAt(0) === "@") { |
|
166 | if (block.charAt(0) === "@") { | |
172 | // |
|
167 | // | |
173 | // Things that look like our math markers will get |
|
168 | // Things that look like our math markers will get | |
174 | // stored and then retrieved along with the math. |
|
169 | // stored and then retrieved along with the math. | |
175 | // |
|
170 | // | |
176 | blocks[i] = "@@" + math.length + "@@"; |
|
171 | blocks[i] = "@@" + math.length + "@@"; | |
177 | math.push(block); |
|
172 | math.push(block); | |
178 | } |
|
173 | } | |
179 | else if (start) { |
|
174 | else if (start) { | |
180 | // |
|
175 | // | |
181 | // If we are in math, look for the end delimiter, |
|
176 | // If we are in math, look for the end delimiter, | |
182 | // but don't go past double line breaks, and |
|
177 | // but don't go past double line breaks, and | |
183 | // and balance braces within the math. |
|
178 | // and balance braces within the math. | |
184 | // |
|
179 | // | |
185 | if (block === end) { |
|
180 | if (block === end) { | |
186 | if (braces) { |
|
181 | if (braces) { | |
187 | last = i; |
|
182 | last = i; | |
188 | } |
|
183 | } | |
189 | else { |
|
184 | else { | |
190 | blocks = process_math(start, i, de_tilde, math, blocks); |
|
185 | blocks = process_math(start, i, de_tilde, math, blocks); | |
191 | start = null; |
|
186 | start = null; | |
192 | end = null; |
|
187 | end = null; | |
193 | last = null; |
|
188 | last = null; | |
194 | } |
|
189 | } | |
195 | } |
|
190 | } | |
196 | else if (block.match(/\n.*\n/)) { |
|
191 | else if (block.match(/\n.*\n/)) { | |
197 | if (last) { |
|
192 | if (last) { | |
198 | i = last; |
|
193 | i = last; | |
199 | blocks = process_math(start, i, de_tilde, math, blocks); |
|
194 | blocks = process_math(start, i, de_tilde, math, blocks); | |
200 | } |
|
195 | } | |
201 | start = null; |
|
196 | start = null; | |
202 | end = null; |
|
197 | end = null; | |
203 | last = null; |
|
198 | last = null; | |
204 | braces = 0; |
|
199 | braces = 0; | |
205 | } |
|
200 | } | |
206 | else if (block === "{") { |
|
201 | else if (block === "{") { | |
207 | braces++; |
|
202 | braces++; | |
208 | } |
|
203 | } | |
209 | else if (block === "}" && braces) { |
|
204 | else if (block === "}" && braces) { | |
210 | braces--; |
|
205 | braces--; | |
211 | } |
|
206 | } | |
212 | } |
|
207 | } | |
213 | else { |
|
208 | else { | |
214 | // |
|
209 | // | |
215 | // Look for math start delimiters and when |
|
210 | // Look for math start delimiters and when | |
216 | // found, set up the end delimiter. |
|
211 | // found, set up the end delimiter. | |
217 | // |
|
212 | // | |
218 | if (block === inline || block === "$$") { |
|
213 | if (block === inline || block === "$$") { | |
219 | start = i; |
|
214 | start = i; | |
220 | end = block; |
|
215 | end = block; | |
221 | braces = 0; |
|
216 | braces = 0; | |
222 | } |
|
217 | } | |
223 | else if (block.substr(1, 5) === "begin") { |
|
218 | else if (block.substr(1, 5) === "begin") { | |
224 | start = i; |
|
219 | start = i; | |
225 | end = "\\end" + block.substr(6); |
|
220 | end = "\\end" + block.substr(6); | |
226 | braces = 0; |
|
221 | braces = 0; | |
227 | } |
|
222 | } | |
228 | } |
|
223 | } | |
229 | } |
|
224 | } | |
230 | if (last) { |
|
225 | if (last) { | |
231 | blocks = process_math(start, last, de_tilde, math, blocks); |
|
226 | blocks = process_math(start, last, de_tilde, math, blocks); | |
232 | start = null; |
|
227 | start = null; | |
233 | end = null; |
|
228 | end = null; | |
234 | last = null; |
|
229 | last = null; | |
235 | } |
|
230 | } | |
236 | return [de_tilde(blocks.join("")), math]; |
|
231 | return [de_tilde(blocks.join("")), math]; | |
237 | }; |
|
232 | }; | |
238 |
|
233 | |||
239 | // |
|
234 | // | |
240 | // Put back the math strings that were saved, |
|
235 | // Put back the math strings that were saved, | |
241 | // and clear the math array (no need to keep it around). |
|
236 | // and clear the math array (no need to keep it around). | |
242 | // |
|
237 | // | |
243 | var replace_math = function (text, math) { |
|
238 | var replace_math = function (text, math) { | |
244 | if (!window.MathJax) { |
|
|||
245 | return text; |
|
|||
246 | } |
|
|||
247 | text = text.replace(/@@(\d+)@@/g, function (match, n) { |
|
239 | text = text.replace(/@@(\d+)@@/g, function (match, n) { | |
248 | return math[n]; |
|
240 | return math[n]; | |
249 | }); |
|
241 | }); | |
250 | return text; |
|
242 | return text; | |
251 | }; |
|
243 | }; | |
252 |
|
244 | |||
253 | return { |
|
245 | return { | |
254 | init : init, |
|
246 | init : init, | |
255 | remove_math : remove_math, |
|
247 | remove_math : remove_math, | |
256 | replace_math : replace_math |
|
248 | replace_math : replace_math | |
257 | }; |
|
249 | }; | |
258 |
|
250 | |||
259 | }(IPython)); |
|
251 | }(IPython)); |
@@ -1,74 +1,107 b'' | |||||
1 | """Markdown filters |
|
1 | """Markdown filters | |
2 | This file contains a collection of utility filters for dealing with |
|
2 | This file contains a collection of utility filters for dealing with | |
3 | markdown within Jinja templates. |
|
3 | markdown within Jinja templates. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from __future__ import print_function |
|
16 | from __future__ import print_function | |
17 |
|
17 | |||
18 | # Stdlib imports |
|
18 | # Stdlib imports | |
19 |
import |
|
19 | import os | |
20 | import subprocess |
|
20 | import subprocess | |
|
21 | from io import TextIOWrapper, BytesIO | |||
21 |
|
22 | |||
|
23 | # IPython imports | |||
22 | from IPython.nbconvert.utils.pandoc import pandoc |
|
24 | from IPython.nbconvert.utils.pandoc import pandoc | |
|
25 | from IPython.nbconvert.utils.exceptions import ConversionException | |||
|
26 | from IPython.utils.process import find_cmd, FindCmdError | |||
|
27 | from IPython.utils.py3compat import cast_bytes | |||
23 |
|
28 | |||
24 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
25 | # Functions |
|
30 | # Functions | |
26 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
|
32 | marked = os.path.join(os.path.dirname(__file__), "marked.js") | |||
27 |
|
33 | |||
28 | __all__ = [ |
|
34 | __all__ = [ | |
29 | 'markdown2html', |
|
35 | 'markdown2html', | |
|
36 | 'markdown2html_pandoc', | |||
|
37 | 'markdown2html_marked', | |||
30 | 'markdown2latex', |
|
38 | 'markdown2latex', | |
31 | 'markdown2rst' |
|
39 | 'markdown2rst', | |
32 | ] |
|
40 | ] | |
33 |
|
41 | |||
|
42 | class NodeJSMissing(ConversionException): | |||
|
43 | """Exception raised when node.js is missing.""" | |||
|
44 | pass | |||
|
45 | ||||
34 | def markdown2latex(source): |
|
46 | def markdown2latex(source): | |
35 | """Convert a markdown string to LaTeX via pandoc. |
|
47 | """Convert a markdown string to LaTeX via pandoc. | |
36 |
|
48 | |||
37 | This function will raise an error if pandoc is not installed. |
|
49 | This function will raise an error if pandoc is not installed. | |
38 | Any error messages generated by pandoc are printed to stderr. |
|
50 | Any error messages generated by pandoc are printed to stderr. | |
39 |
|
51 | |||
40 | Parameters |
|
52 | Parameters | |
41 | ---------- |
|
53 | ---------- | |
42 | source : string |
|
54 | source : string | |
43 | Input string, assumed to be valid markdown. |
|
55 | Input string, assumed to be valid markdown. | |
44 |
|
56 | |||
45 | Returns |
|
57 | Returns | |
46 | ------- |
|
58 | ------- | |
47 | out : string |
|
59 | out : string | |
48 | Output as returned by pandoc. |
|
60 | Output as returned by pandoc. | |
49 | """ |
|
61 | """ | |
50 | return pandoc(source, 'markdown', 'latex') |
|
62 | return pandoc(source, 'markdown', 'latex') | |
51 |
|
63 | |||
52 |
|
64 | def markdown2html_pandoc(source): | ||
53 | def markdown2html(source): |
|
|||
54 | """Convert a markdown string to HTML via pandoc""" |
|
65 | """Convert a markdown string to HTML via pandoc""" | |
55 | return pandoc(source, 'markdown', 'html', extra_args=['--mathjax']) |
|
66 | return pandoc(source, 'markdown', 'html', extra_args=['--mathjax']) | |
56 |
|
67 | |||
|
68 | def markdown2html_marked(source, encoding='utf-8'): | |||
|
69 | """Convert a markdown string to HTML via marked""" | |||
|
70 | command = ['node', marked] | |||
|
71 | try: | |||
|
72 | p = subprocess.Popen(command, | |||
|
73 | stdin=subprocess.PIPE, stdout=subprocess.PIPE | |||
|
74 | ) | |||
|
75 | except OSError as e: | |||
|
76 | raise NodeJSMissing( | |||
|
77 | "The command '%s' returned an error: %s.\n" % (" ".join(command), e) + | |||
|
78 | "Please check that Node.js is installed." | |||
|
79 | ) | |||
|
80 | out, _ = p.communicate(cast_bytes(source, encoding)) | |||
|
81 | out = TextIOWrapper(BytesIO(out), encoding, 'replace').read() | |||
|
82 | return out.rstrip('\n') | |||
|
83 | ||||
57 | def markdown2rst(source): |
|
84 | def markdown2rst(source): | |
58 | """Convert a markdown string to LaTeX via pandoc. |
|
85 | """Convert a markdown string to LaTeX via pandoc. | |
59 |
|
86 | |||
60 | This function will raise an error if pandoc is not installed. |
|
87 | This function will raise an error if pandoc is not installed. | |
61 | Any error messages generated by pandoc are printed to stderr. |
|
88 | Any error messages generated by pandoc are printed to stderr. | |
62 |
|
89 | |||
63 | Parameters |
|
90 | Parameters | |
64 | ---------- |
|
91 | ---------- | |
65 | source : string |
|
92 | source : string | |
66 | Input string, assumed to be valid markdown. |
|
93 | Input string, assumed to be valid markdown. | |
67 |
|
94 | |||
68 | Returns |
|
95 | Returns | |
69 | ------- |
|
96 | ------- | |
70 | out : string |
|
97 | out : string | |
71 | Output as returned by pandoc. |
|
98 | Output as returned by pandoc. | |
72 | """ |
|
99 | """ | |
73 | return pandoc(source, 'markdown', 'rst') |
|
100 | return pandoc(source, 'markdown', 'rst') | |
74 |
|
101 | |||
|
102 | try: | |||
|
103 | find_cmd('node') | |||
|
104 | except FindCmdError: | |||
|
105 | markdown2html = markdown2html_pandoc | |||
|
106 | else: | |||
|
107 | markdown2html = markdown2html_marked |
@@ -1,600 +1,601 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | This module defines the things that are used in setup.py for building IPython |
|
3 | This module defines the things that are used in setup.py for building IPython | |
4 |
|
4 | |||
5 | This includes: |
|
5 | This includes: | |
6 |
|
6 | |||
7 | * The basic arguments to setup |
|
7 | * The basic arguments to setup | |
8 | * Functions for finding things like packages, package data, etc. |
|
8 | * Functions for finding things like packages, package data, etc. | |
9 | * A function for checking dependencies. |
|
9 | * A function for checking dependencies. | |
10 | """ |
|
10 | """ | |
11 | from __future__ import print_function |
|
11 | from __future__ import print_function | |
12 |
|
12 | |||
13 | #------------------------------------------------------------------------------- |
|
13 | #------------------------------------------------------------------------------- | |
14 | # Copyright (C) 2008 The IPython Development Team |
|
14 | # Copyright (C) 2008 The IPython Development Team | |
15 | # |
|
15 | # | |
16 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | # Distributed under the terms of the BSD License. The full license is in | |
17 | # the file COPYING, distributed as part of this software. |
|
17 | # the file COPYING, distributed as part of this software. | |
18 | #------------------------------------------------------------------------------- |
|
18 | #------------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | #------------------------------------------------------------------------------- |
|
20 | #------------------------------------------------------------------------------- | |
21 | # Imports |
|
21 | # Imports | |
22 | #------------------------------------------------------------------------------- |
|
22 | #------------------------------------------------------------------------------- | |
23 | import errno |
|
23 | import errno | |
24 | import os |
|
24 | import os | |
25 | import sys |
|
25 | import sys | |
26 |
|
26 | |||
27 | from distutils.command.build_py import build_py |
|
27 | from distutils.command.build_py import build_py | |
28 | from distutils.command.build_scripts import build_scripts |
|
28 | from distutils.command.build_scripts import build_scripts | |
29 | from distutils.command.install import install |
|
29 | from distutils.command.install import install | |
30 | from distutils.command.install_scripts import install_scripts |
|
30 | from distutils.command.install_scripts import install_scripts | |
31 | from distutils.cmd import Command |
|
31 | from distutils.cmd import Command | |
32 | from glob import glob |
|
32 | from glob import glob | |
33 | from subprocess import call |
|
33 | from subprocess import call | |
34 |
|
34 | |||
35 | from setupext import install_data_ext |
|
35 | from setupext import install_data_ext | |
36 |
|
36 | |||
37 | #------------------------------------------------------------------------------- |
|
37 | #------------------------------------------------------------------------------- | |
38 | # Useful globals and utility functions |
|
38 | # Useful globals and utility functions | |
39 | #------------------------------------------------------------------------------- |
|
39 | #------------------------------------------------------------------------------- | |
40 |
|
40 | |||
41 | # A few handy globals |
|
41 | # A few handy globals | |
42 | isfile = os.path.isfile |
|
42 | isfile = os.path.isfile | |
43 | pjoin = os.path.join |
|
43 | pjoin = os.path.join | |
44 | repo_root = os.path.dirname(os.path.abspath(__file__)) |
|
44 | repo_root = os.path.dirname(os.path.abspath(__file__)) | |
45 |
|
45 | |||
46 | def oscmd(s): |
|
46 | def oscmd(s): | |
47 | print(">", s) |
|
47 | print(">", s) | |
48 | os.system(s) |
|
48 | os.system(s) | |
49 |
|
49 | |||
50 | # Py3 compatibility hacks, without assuming IPython itself is installed with |
|
50 | # Py3 compatibility hacks, without assuming IPython itself is installed with | |
51 | # the full py3compat machinery. |
|
51 | # the full py3compat machinery. | |
52 |
|
52 | |||
53 | try: |
|
53 | try: | |
54 | execfile |
|
54 | execfile | |
55 | except NameError: |
|
55 | except NameError: | |
56 | def execfile(fname, globs, locs=None): |
|
56 | def execfile(fname, globs, locs=None): | |
57 | locs = locs or globs |
|
57 | locs = locs or globs | |
58 | exec(compile(open(fname).read(), fname, "exec"), globs, locs) |
|
58 | exec(compile(open(fname).read(), fname, "exec"), globs, locs) | |
59 |
|
59 | |||
60 | # A little utility we'll need below, since glob() does NOT allow you to do |
|
60 | # A little utility we'll need below, since glob() does NOT allow you to do | |
61 | # exclusion on multiple endings! |
|
61 | # exclusion on multiple endings! | |
62 | def file_doesnt_endwith(test,endings): |
|
62 | def file_doesnt_endwith(test,endings): | |
63 | """Return true if test is a file and its name does NOT end with any |
|
63 | """Return true if test is a file and its name does NOT end with any | |
64 | of the strings listed in endings.""" |
|
64 | of the strings listed in endings.""" | |
65 | if not isfile(test): |
|
65 | if not isfile(test): | |
66 | return False |
|
66 | return False | |
67 | for e in endings: |
|
67 | for e in endings: | |
68 | if test.endswith(e): |
|
68 | if test.endswith(e): | |
69 | return False |
|
69 | return False | |
70 | return True |
|
70 | return True | |
71 |
|
71 | |||
72 | #--------------------------------------------------------------------------- |
|
72 | #--------------------------------------------------------------------------- | |
73 | # Basic project information |
|
73 | # Basic project information | |
74 | #--------------------------------------------------------------------------- |
|
74 | #--------------------------------------------------------------------------- | |
75 |
|
75 | |||
76 | # release.py contains version, authors, license, url, keywords, etc. |
|
76 | # release.py contains version, authors, license, url, keywords, etc. | |
77 | execfile(pjoin(repo_root, 'IPython','core','release.py'), globals()) |
|
77 | execfile(pjoin(repo_root, 'IPython','core','release.py'), globals()) | |
78 |
|
78 | |||
79 | # Create a dict with the basic information |
|
79 | # Create a dict with the basic information | |
80 | # This dict is eventually passed to setup after additional keys are added. |
|
80 | # This dict is eventually passed to setup after additional keys are added. | |
81 | setup_args = dict( |
|
81 | setup_args = dict( | |
82 | name = name, |
|
82 | name = name, | |
83 | version = version, |
|
83 | version = version, | |
84 | description = description, |
|
84 | description = description, | |
85 | long_description = long_description, |
|
85 | long_description = long_description, | |
86 | author = author, |
|
86 | author = author, | |
87 | author_email = author_email, |
|
87 | author_email = author_email, | |
88 | url = url, |
|
88 | url = url, | |
89 | download_url = download_url, |
|
89 | download_url = download_url, | |
90 | license = license, |
|
90 | license = license, | |
91 | platforms = platforms, |
|
91 | platforms = platforms, | |
92 | keywords = keywords, |
|
92 | keywords = keywords, | |
93 | classifiers = classifiers, |
|
93 | classifiers = classifiers, | |
94 | cmdclass = {'install_data': install_data_ext}, |
|
94 | cmdclass = {'install_data': install_data_ext}, | |
95 | ) |
|
95 | ) | |
96 |
|
96 | |||
97 |
|
97 | |||
98 | #--------------------------------------------------------------------------- |
|
98 | #--------------------------------------------------------------------------- | |
99 | # Find packages |
|
99 | # Find packages | |
100 | #--------------------------------------------------------------------------- |
|
100 | #--------------------------------------------------------------------------- | |
101 |
|
101 | |||
102 | def find_packages(): |
|
102 | def find_packages(): | |
103 | """ |
|
103 | """ | |
104 | Find all of IPython's packages. |
|
104 | Find all of IPython's packages. | |
105 | """ |
|
105 | """ | |
106 | excludes = ['deathrow', 'quarantine'] |
|
106 | excludes = ['deathrow', 'quarantine'] | |
107 | packages = [] |
|
107 | packages = [] | |
108 | for dir,subdirs,files in os.walk('IPython'): |
|
108 | for dir,subdirs,files in os.walk('IPython'): | |
109 | package = dir.replace(os.path.sep, '.') |
|
109 | package = dir.replace(os.path.sep, '.') | |
110 | if any(package.startswith('IPython.'+exc) for exc in excludes): |
|
110 | if any(package.startswith('IPython.'+exc) for exc in excludes): | |
111 | # package is to be excluded (e.g. deathrow) |
|
111 | # package is to be excluded (e.g. deathrow) | |
112 | continue |
|
112 | continue | |
113 | if '__init__.py' not in files: |
|
113 | if '__init__.py' not in files: | |
114 | # not a package |
|
114 | # not a package | |
115 | continue |
|
115 | continue | |
116 | packages.append(package) |
|
116 | packages.append(package) | |
117 | return packages |
|
117 | return packages | |
118 |
|
118 | |||
119 | #--------------------------------------------------------------------------- |
|
119 | #--------------------------------------------------------------------------- | |
120 | # Find package data |
|
120 | # Find package data | |
121 | #--------------------------------------------------------------------------- |
|
121 | #--------------------------------------------------------------------------- | |
122 |
|
122 | |||
123 | def find_package_data(): |
|
123 | def find_package_data(): | |
124 | """ |
|
124 | """ | |
125 | Find IPython's package_data. |
|
125 | Find IPython's package_data. | |
126 | """ |
|
126 | """ | |
127 | # This is not enough for these things to appear in an sdist. |
|
127 | # This is not enough for these things to appear in an sdist. | |
128 | # We need to muck with the MANIFEST to get this to work |
|
128 | # We need to muck with the MANIFEST to get this to work | |
129 |
|
129 | |||
130 | # exclude static things that we don't ship (e.g. mathjax) |
|
130 | # exclude static things that we don't ship (e.g. mathjax) | |
131 | excludes = ['mathjax'] |
|
131 | excludes = ['mathjax'] | |
132 |
|
132 | |||
133 | # add 'static/' prefix to exclusions, and tuplify for use in startswith |
|
133 | # add 'static/' prefix to exclusions, and tuplify for use in startswith | |
134 | excludes = tuple([os.path.join('static', ex) for ex in excludes]) |
|
134 | excludes = tuple([os.path.join('static', ex) for ex in excludes]) | |
135 |
|
135 | |||
136 | # walk notebook resources: |
|
136 | # walk notebook resources: | |
137 | cwd = os.getcwd() |
|
137 | cwd = os.getcwd() | |
138 | os.chdir(os.path.join('IPython', 'html')) |
|
138 | os.chdir(os.path.join('IPython', 'html')) | |
139 | static_walk = list(os.walk('static')) |
|
139 | static_walk = list(os.walk('static')) | |
140 | static_data = [] |
|
140 | static_data = [] | |
141 | for parent, dirs, files in static_walk: |
|
141 | for parent, dirs, files in static_walk: | |
142 | if parent.startswith(excludes): |
|
142 | if parent.startswith(excludes): | |
143 | continue |
|
143 | continue | |
144 | for f in files: |
|
144 | for f in files: | |
145 | static_data.append(os.path.join(parent, f)) |
|
145 | static_data.append(os.path.join(parent, f)) | |
146 |
|
146 | |||
147 | os.chdir(os.path.join('tests',)) |
|
147 | os.chdir(os.path.join('tests',)) | |
148 | js_tests = glob('casperjs/*.*') + glob('casperjs/*/*') |
|
148 | js_tests = glob('casperjs/*.*') + glob('casperjs/*/*') | |
149 |
|
149 | |||
150 | os.chdir(os.path.join(cwd, 'IPython', 'nbconvert')) |
|
150 | os.chdir(os.path.join(cwd, 'IPython', 'nbconvert')) | |
151 | nbconvert_templates = [os.path.join(dirpath, '*.*') |
|
151 | nbconvert_templates = [os.path.join(dirpath, '*.*') | |
152 | for dirpath, _, _ in os.walk('templates')] |
|
152 | for dirpath, _, _ in os.walk('templates')] | |
153 |
|
153 | |||
154 | os.chdir(cwd) |
|
154 | os.chdir(cwd) | |
155 |
|
155 | |||
156 | package_data = { |
|
156 | package_data = { | |
157 | 'IPython.config.profile' : ['README*', '*/*.py'], |
|
157 | 'IPython.config.profile' : ['README*', '*/*.py'], | |
158 | 'IPython.core.tests' : ['*.png', '*.jpg'], |
|
158 | 'IPython.core.tests' : ['*.png', '*.jpg'], | |
159 | 'IPython.lib.tests' : ['*.wav'], |
|
159 | 'IPython.lib.tests' : ['*.wav'], | |
160 | 'IPython.testing' : ['*.txt'], |
|
160 | 'IPython.testing' : ['*.txt'], | |
161 | 'IPython.testing.plugin' : ['*.txt'], |
|
161 | 'IPython.testing.plugin' : ['*.txt'], | |
162 | 'IPython.html' : ['templates/*'] + static_data, |
|
162 | 'IPython.html' : ['templates/*'] + static_data, | |
163 | 'IPython.html.tests' : js_tests, |
|
163 | 'IPython.html.tests' : js_tests, | |
164 | 'IPython.qt.console' : ['resources/icon/*.svg'], |
|
164 | 'IPython.qt.console' : ['resources/icon/*.svg'], | |
165 | 'IPython.nbconvert' : nbconvert_templates + |
|
165 | 'IPython.nbconvert' : nbconvert_templates + | |
166 | ['tests/files/*.*', 'exporters/tests/files/*.*'], |
|
166 | ['tests/files/*.*', 'exporters/tests/files/*.*'], | |
|
167 | 'IPython.nbconvert.filters' : ['marked.js'], | |||
167 | 'IPython.nbformat' : ['tests/*.ipynb'] |
|
168 | 'IPython.nbformat' : ['tests/*.ipynb'] | |
168 | } |
|
169 | } | |
169 | return package_data |
|
170 | return package_data | |
170 |
|
171 | |||
171 |
|
172 | |||
172 | #--------------------------------------------------------------------------- |
|
173 | #--------------------------------------------------------------------------- | |
173 | # Find data files |
|
174 | # Find data files | |
174 | #--------------------------------------------------------------------------- |
|
175 | #--------------------------------------------------------------------------- | |
175 |
|
176 | |||
176 | def make_dir_struct(tag,base,out_base): |
|
177 | def make_dir_struct(tag,base,out_base): | |
177 | """Make the directory structure of all files below a starting dir. |
|
178 | """Make the directory structure of all files below a starting dir. | |
178 |
|
179 | |||
179 | This is just a convenience routine to help build a nested directory |
|
180 | This is just a convenience routine to help build a nested directory | |
180 | hierarchy because distutils is too stupid to do this by itself. |
|
181 | hierarchy because distutils is too stupid to do this by itself. | |
181 |
|
182 | |||
182 | XXX - this needs a proper docstring! |
|
183 | XXX - this needs a proper docstring! | |
183 | """ |
|
184 | """ | |
184 |
|
185 | |||
185 | # we'll use these a lot below |
|
186 | # we'll use these a lot below | |
186 | lbase = len(base) |
|
187 | lbase = len(base) | |
187 | pathsep = os.path.sep |
|
188 | pathsep = os.path.sep | |
188 | lpathsep = len(pathsep) |
|
189 | lpathsep = len(pathsep) | |
189 |
|
190 | |||
190 | out = [] |
|
191 | out = [] | |
191 | for (dirpath,dirnames,filenames) in os.walk(base): |
|
192 | for (dirpath,dirnames,filenames) in os.walk(base): | |
192 | # we need to strip out the dirpath from the base to map it to the |
|
193 | # we need to strip out the dirpath from the base to map it to the | |
193 | # output (installation) path. This requires possibly stripping the |
|
194 | # output (installation) path. This requires possibly stripping the | |
194 | # path separator, because otherwise pjoin will not work correctly |
|
195 | # path separator, because otherwise pjoin will not work correctly | |
195 | # (pjoin('foo/','/bar') returns '/bar'). |
|
196 | # (pjoin('foo/','/bar') returns '/bar'). | |
196 |
|
197 | |||
197 | dp_eff = dirpath[lbase:] |
|
198 | dp_eff = dirpath[lbase:] | |
198 | if dp_eff.startswith(pathsep): |
|
199 | if dp_eff.startswith(pathsep): | |
199 | dp_eff = dp_eff[lpathsep:] |
|
200 | dp_eff = dp_eff[lpathsep:] | |
200 | # The output path must be anchored at the out_base marker |
|
201 | # The output path must be anchored at the out_base marker | |
201 | out_path = pjoin(out_base,dp_eff) |
|
202 | out_path = pjoin(out_base,dp_eff) | |
202 | # Now we can generate the final filenames. Since os.walk only produces |
|
203 | # Now we can generate the final filenames. Since os.walk only produces | |
203 | # filenames, we must join back with the dirpath to get full valid file |
|
204 | # filenames, we must join back with the dirpath to get full valid file | |
204 | # paths: |
|
205 | # paths: | |
205 | pfiles = [pjoin(dirpath,f) for f in filenames] |
|
206 | pfiles = [pjoin(dirpath,f) for f in filenames] | |
206 | # Finally, generate the entry we need, which is a pari of (output |
|
207 | # Finally, generate the entry we need, which is a pari of (output | |
207 | # path, files) for use as a data_files parameter in install_data. |
|
208 | # path, files) for use as a data_files parameter in install_data. | |
208 | out.append((out_path, pfiles)) |
|
209 | out.append((out_path, pfiles)) | |
209 |
|
210 | |||
210 | return out |
|
211 | return out | |
211 |
|
212 | |||
212 |
|
213 | |||
213 | def find_data_files(): |
|
214 | def find_data_files(): | |
214 | """ |
|
215 | """ | |
215 | Find IPython's data_files. |
|
216 | Find IPython's data_files. | |
216 |
|
217 | |||
217 | Most of these are docs. |
|
218 | Most of these are docs. | |
218 | """ |
|
219 | """ | |
219 |
|
220 | |||
220 | docdirbase = pjoin('share', 'doc', 'ipython') |
|
221 | docdirbase = pjoin('share', 'doc', 'ipython') | |
221 | manpagebase = pjoin('share', 'man', 'man1') |
|
222 | manpagebase = pjoin('share', 'man', 'man1') | |
222 |
|
223 | |||
223 | # Simple file lists can be made by hand |
|
224 | # Simple file lists can be made by hand | |
224 | manpages = [f for f in glob(pjoin('docs','man','*.1.gz')) if isfile(f)] |
|
225 | manpages = [f for f in glob(pjoin('docs','man','*.1.gz')) if isfile(f)] | |
225 | if not manpages: |
|
226 | if not manpages: | |
226 | # When running from a source tree, the manpages aren't gzipped |
|
227 | # When running from a source tree, the manpages aren't gzipped | |
227 | manpages = [f for f in glob(pjoin('docs','man','*.1')) if isfile(f)] |
|
228 | manpages = [f for f in glob(pjoin('docs','man','*.1')) if isfile(f)] | |
228 |
|
229 | |||
229 | igridhelpfiles = [f for f in glob(pjoin('IPython','extensions','igrid_help.*')) if isfile(f)] |
|
230 | igridhelpfiles = [f for f in glob(pjoin('IPython','extensions','igrid_help.*')) if isfile(f)] | |
230 |
|
231 | |||
231 | # For nested structures, use the utility above |
|
232 | # For nested structures, use the utility above | |
232 | example_files = make_dir_struct( |
|
233 | example_files = make_dir_struct( | |
233 | 'data', |
|
234 | 'data', | |
234 | pjoin('docs','examples'), |
|
235 | pjoin('docs','examples'), | |
235 | pjoin(docdirbase,'examples') |
|
236 | pjoin(docdirbase,'examples') | |
236 | ) |
|
237 | ) | |
237 | manual_files = make_dir_struct( |
|
238 | manual_files = make_dir_struct( | |
238 | 'data', |
|
239 | 'data', | |
239 | pjoin('docs','html'), |
|
240 | pjoin('docs','html'), | |
240 | pjoin(docdirbase,'manual') |
|
241 | pjoin(docdirbase,'manual') | |
241 | ) |
|
242 | ) | |
242 |
|
243 | |||
243 | # And assemble the entire output list |
|
244 | # And assemble the entire output list | |
244 | data_files = [ (manpagebase, manpages), |
|
245 | data_files = [ (manpagebase, manpages), | |
245 | (pjoin(docdirbase, 'extensions'), igridhelpfiles), |
|
246 | (pjoin(docdirbase, 'extensions'), igridhelpfiles), | |
246 | ] + manual_files + example_files |
|
247 | ] + manual_files + example_files | |
247 |
|
248 | |||
248 | return data_files |
|
249 | return data_files | |
249 |
|
250 | |||
250 |
|
251 | |||
251 | def make_man_update_target(manpage): |
|
252 | def make_man_update_target(manpage): | |
252 | """Return a target_update-compliant tuple for the given manpage. |
|
253 | """Return a target_update-compliant tuple for the given manpage. | |
253 |
|
254 | |||
254 | Parameters |
|
255 | Parameters | |
255 | ---------- |
|
256 | ---------- | |
256 | manpage : string |
|
257 | manpage : string | |
257 | Name of the manpage, must include the section number (trailing number). |
|
258 | Name of the manpage, must include the section number (trailing number). | |
258 |
|
259 | |||
259 | Example |
|
260 | Example | |
260 | ------- |
|
261 | ------- | |
261 |
|
262 | |||
262 | >>> make_man_update_target('ipython.1') #doctest: +NORMALIZE_WHITESPACE |
|
263 | >>> make_man_update_target('ipython.1') #doctest: +NORMALIZE_WHITESPACE | |
263 | ('docs/man/ipython.1.gz', |
|
264 | ('docs/man/ipython.1.gz', | |
264 | ['docs/man/ipython.1'], |
|
265 | ['docs/man/ipython.1'], | |
265 | 'cd docs/man && gzip -9c ipython.1 > ipython.1.gz') |
|
266 | 'cd docs/man && gzip -9c ipython.1 > ipython.1.gz') | |
266 | """ |
|
267 | """ | |
267 | man_dir = pjoin('docs', 'man') |
|
268 | man_dir = pjoin('docs', 'man') | |
268 | manpage_gz = manpage + '.gz' |
|
269 | manpage_gz = manpage + '.gz' | |
269 | manpath = pjoin(man_dir, manpage) |
|
270 | manpath = pjoin(man_dir, manpage) | |
270 | manpath_gz = pjoin(man_dir, manpage_gz) |
|
271 | manpath_gz = pjoin(man_dir, manpage_gz) | |
271 | gz_cmd = ( "cd %(man_dir)s && gzip -9c %(manpage)s > %(manpage_gz)s" % |
|
272 | gz_cmd = ( "cd %(man_dir)s && gzip -9c %(manpage)s > %(manpage_gz)s" % | |
272 | locals() ) |
|
273 | locals() ) | |
273 | return (manpath_gz, [manpath], gz_cmd) |
|
274 | return (manpath_gz, [manpath], gz_cmd) | |
274 |
|
275 | |||
275 | # The two functions below are copied from IPython.utils.path, so we don't need |
|
276 | # The two functions below are copied from IPython.utils.path, so we don't need | |
276 | # to import IPython during setup, which fails on Python 3. |
|
277 | # to import IPython during setup, which fails on Python 3. | |
277 |
|
278 | |||
278 | def target_outdated(target,deps): |
|
279 | def target_outdated(target,deps): | |
279 | """Determine whether a target is out of date. |
|
280 | """Determine whether a target is out of date. | |
280 |
|
281 | |||
281 | target_outdated(target,deps) -> 1/0 |
|
282 | target_outdated(target,deps) -> 1/0 | |
282 |
|
283 | |||
283 | deps: list of filenames which MUST exist. |
|
284 | deps: list of filenames which MUST exist. | |
284 | target: single filename which may or may not exist. |
|
285 | target: single filename which may or may not exist. | |
285 |
|
286 | |||
286 | If target doesn't exist or is older than any file listed in deps, return |
|
287 | If target doesn't exist or is older than any file listed in deps, return | |
287 | true, otherwise return false. |
|
288 | true, otherwise return false. | |
288 | """ |
|
289 | """ | |
289 | try: |
|
290 | try: | |
290 | target_time = os.path.getmtime(target) |
|
291 | target_time = os.path.getmtime(target) | |
291 | except os.error: |
|
292 | except os.error: | |
292 | return 1 |
|
293 | return 1 | |
293 | for dep in deps: |
|
294 | for dep in deps: | |
294 | dep_time = os.path.getmtime(dep) |
|
295 | dep_time = os.path.getmtime(dep) | |
295 | if dep_time > target_time: |
|
296 | if dep_time > target_time: | |
296 | #print "For target",target,"Dep failed:",dep # dbg |
|
297 | #print "For target",target,"Dep failed:",dep # dbg | |
297 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
298 | #print "times (dep,tar):",dep_time,target_time # dbg | |
298 | return 1 |
|
299 | return 1 | |
299 | return 0 |
|
300 | return 0 | |
300 |
|
301 | |||
301 |
|
302 | |||
302 | def target_update(target,deps,cmd): |
|
303 | def target_update(target,deps,cmd): | |
303 | """Update a target with a given command given a list of dependencies. |
|
304 | """Update a target with a given command given a list of dependencies. | |
304 |
|
305 | |||
305 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
306 | target_update(target,deps,cmd) -> runs cmd if target is outdated. | |
306 |
|
307 | |||
307 | This is just a wrapper around target_outdated() which calls the given |
|
308 | This is just a wrapper around target_outdated() which calls the given | |
308 | command if target is outdated.""" |
|
309 | command if target is outdated.""" | |
309 |
|
310 | |||
310 | if target_outdated(target,deps): |
|
311 | if target_outdated(target,deps): | |
311 | os.system(cmd) |
|
312 | os.system(cmd) | |
312 |
|
313 | |||
313 | #--------------------------------------------------------------------------- |
|
314 | #--------------------------------------------------------------------------- | |
314 | # Find scripts |
|
315 | # Find scripts | |
315 | #--------------------------------------------------------------------------- |
|
316 | #--------------------------------------------------------------------------- | |
316 |
|
317 | |||
317 | def find_entry_points(): |
|
318 | def find_entry_points(): | |
318 | """Find IPython's scripts. |
|
319 | """Find IPython's scripts. | |
319 |
|
320 | |||
320 | if entry_points is True: |
|
321 | if entry_points is True: | |
321 | return setuptools entry_point-style definitions |
|
322 | return setuptools entry_point-style definitions | |
322 | else: |
|
323 | else: | |
323 | return file paths of plain scripts [default] |
|
324 | return file paths of plain scripts [default] | |
324 |
|
325 | |||
325 | suffix is appended to script names if entry_points is True, so that the |
|
326 | suffix is appended to script names if entry_points is True, so that the | |
326 | Python 3 scripts get named "ipython3" etc. |
|
327 | Python 3 scripts get named "ipython3" etc. | |
327 | """ |
|
328 | """ | |
328 | ep = [ |
|
329 | ep = [ | |
329 | 'ipython%s = IPython:start_ipython', |
|
330 | 'ipython%s = IPython:start_ipython', | |
330 | 'ipcontroller%s = IPython.parallel.apps.ipcontrollerapp:launch_new_instance', |
|
331 | 'ipcontroller%s = IPython.parallel.apps.ipcontrollerapp:launch_new_instance', | |
331 | 'ipengine%s = IPython.parallel.apps.ipengineapp:launch_new_instance', |
|
332 | 'ipengine%s = IPython.parallel.apps.ipengineapp:launch_new_instance', | |
332 | 'ipcluster%s = IPython.parallel.apps.ipclusterapp:launch_new_instance', |
|
333 | 'ipcluster%s = IPython.parallel.apps.ipclusterapp:launch_new_instance', | |
333 | 'iptest%s = IPython.testing.iptestcontroller:main', |
|
334 | 'iptest%s = IPython.testing.iptestcontroller:main', | |
334 | ] |
|
335 | ] | |
335 | suffix = str(sys.version_info[0]) |
|
336 | suffix = str(sys.version_info[0]) | |
336 | return [e % '' for e in ep] + [e % suffix for e in ep] |
|
337 | return [e % '' for e in ep] + [e % suffix for e in ep] | |
337 |
|
338 | |||
338 | script_src = """#!{executable} |
|
339 | script_src = """#!{executable} | |
339 | # This script was automatically generated by setup.py |
|
340 | # This script was automatically generated by setup.py | |
340 | if __name__ == '__main__': |
|
341 | if __name__ == '__main__': | |
341 | from {mod} import {func} |
|
342 | from {mod} import {func} | |
342 | {func}() |
|
343 | {func}() | |
343 | """ |
|
344 | """ | |
344 |
|
345 | |||
345 | class build_scripts_entrypt(build_scripts): |
|
346 | class build_scripts_entrypt(build_scripts): | |
346 | def run(self): |
|
347 | def run(self): | |
347 | self.mkpath(self.build_dir) |
|
348 | self.mkpath(self.build_dir) | |
348 | outfiles = [] |
|
349 | outfiles = [] | |
349 | for script in find_entry_points(): |
|
350 | for script in find_entry_points(): | |
350 | name, entrypt = script.split('=') |
|
351 | name, entrypt = script.split('=') | |
351 | name = name.strip() |
|
352 | name = name.strip() | |
352 | entrypt = entrypt.strip() |
|
353 | entrypt = entrypt.strip() | |
353 | outfile = os.path.join(self.build_dir, name) |
|
354 | outfile = os.path.join(self.build_dir, name) | |
354 | outfiles.append(outfile) |
|
355 | outfiles.append(outfile) | |
355 | print('Writing script to', outfile) |
|
356 | print('Writing script to', outfile) | |
356 |
|
357 | |||
357 | mod, func = entrypt.split(':') |
|
358 | mod, func = entrypt.split(':') | |
358 | with open(outfile, 'w') as f: |
|
359 | with open(outfile, 'w') as f: | |
359 | f.write(script_src.format(executable=sys.executable, |
|
360 | f.write(script_src.format(executable=sys.executable, | |
360 | mod=mod, func=func)) |
|
361 | mod=mod, func=func)) | |
361 |
|
362 | |||
362 | return outfiles, outfiles |
|
363 | return outfiles, outfiles | |
363 |
|
364 | |||
364 | class install_lib_symlink(Command): |
|
365 | class install_lib_symlink(Command): | |
365 | user_options = [ |
|
366 | user_options = [ | |
366 | ('install-dir=', 'd', "directory to install to"), |
|
367 | ('install-dir=', 'd', "directory to install to"), | |
367 | ] |
|
368 | ] | |
368 |
|
369 | |||
369 | def initialize_options(self): |
|
370 | def initialize_options(self): | |
370 | self.install_dir = None |
|
371 | self.install_dir = None | |
371 |
|
372 | |||
372 | def finalize_options(self): |
|
373 | def finalize_options(self): | |
373 | self.set_undefined_options('symlink', |
|
374 | self.set_undefined_options('symlink', | |
374 | ('install_lib', 'install_dir'), |
|
375 | ('install_lib', 'install_dir'), | |
375 | ) |
|
376 | ) | |
376 |
|
377 | |||
377 | def run(self): |
|
378 | def run(self): | |
378 | if sys.platform == 'win32': |
|
379 | if sys.platform == 'win32': | |
379 | raise Exception("This doesn't work on Windows.") |
|
380 | raise Exception("This doesn't work on Windows.") | |
380 | pkg = os.path.join(os.getcwd(), 'IPython') |
|
381 | pkg = os.path.join(os.getcwd(), 'IPython') | |
381 | dest = os.path.join(self.install_dir, 'IPython') |
|
382 | dest = os.path.join(self.install_dir, 'IPython') | |
382 | if os.path.islink(dest): |
|
383 | if os.path.islink(dest): | |
383 | print('removing existing symlink at %s' % dest) |
|
384 | print('removing existing symlink at %s' % dest) | |
384 | os.unlink(dest) |
|
385 | os.unlink(dest) | |
385 | print('symlinking %s -> %s' % (pkg, dest)) |
|
386 | print('symlinking %s -> %s' % (pkg, dest)) | |
386 | os.symlink(pkg, dest) |
|
387 | os.symlink(pkg, dest) | |
387 |
|
388 | |||
388 | class unsymlink(install): |
|
389 | class unsymlink(install): | |
389 | def run(self): |
|
390 | def run(self): | |
390 | dest = os.path.join(self.install_lib, 'IPython') |
|
391 | dest = os.path.join(self.install_lib, 'IPython') | |
391 | if os.path.islink(dest): |
|
392 | if os.path.islink(dest): | |
392 | print('removing symlink at %s' % dest) |
|
393 | print('removing symlink at %s' % dest) | |
393 | os.unlink(dest) |
|
394 | os.unlink(dest) | |
394 | else: |
|
395 | else: | |
395 | print('No symlink exists at %s' % dest) |
|
396 | print('No symlink exists at %s' % dest) | |
396 |
|
397 | |||
397 | class install_symlinked(install): |
|
398 | class install_symlinked(install): | |
398 | def run(self): |
|
399 | def run(self): | |
399 | if sys.platform == 'win32': |
|
400 | if sys.platform == 'win32': | |
400 | raise Exception("This doesn't work on Windows.") |
|
401 | raise Exception("This doesn't work on Windows.") | |
401 |
|
402 | |||
402 | # Run all sub-commands (at least those that need to be run) |
|
403 | # Run all sub-commands (at least those that need to be run) | |
403 | for cmd_name in self.get_sub_commands(): |
|
404 | for cmd_name in self.get_sub_commands(): | |
404 | self.run_command(cmd_name) |
|
405 | self.run_command(cmd_name) | |
405 |
|
406 | |||
406 | # 'sub_commands': a list of commands this command might have to run to |
|
407 | # 'sub_commands': a list of commands this command might have to run to | |
407 | # get its work done. See cmd.py for more info. |
|
408 | # get its work done. See cmd.py for more info. | |
408 | sub_commands = [('install_lib_symlink', lambda self:True), |
|
409 | sub_commands = [('install_lib_symlink', lambda self:True), | |
409 | ('install_scripts_sym', lambda self:True), |
|
410 | ('install_scripts_sym', lambda self:True), | |
410 | ] |
|
411 | ] | |
411 |
|
412 | |||
412 | class install_scripts_for_symlink(install_scripts): |
|
413 | class install_scripts_for_symlink(install_scripts): | |
413 | """Redefined to get options from 'symlink' instead of 'install'. |
|
414 | """Redefined to get options from 'symlink' instead of 'install'. | |
414 |
|
415 | |||
415 | I love distutils almost as much as I love setuptools. |
|
416 | I love distutils almost as much as I love setuptools. | |
416 | """ |
|
417 | """ | |
417 | def finalize_options(self): |
|
418 | def finalize_options(self): | |
418 | self.set_undefined_options('build', ('build_scripts', 'build_dir')) |
|
419 | self.set_undefined_options('build', ('build_scripts', 'build_dir')) | |
419 | self.set_undefined_options('symlink', |
|
420 | self.set_undefined_options('symlink', | |
420 | ('install_scripts', 'install_dir'), |
|
421 | ('install_scripts', 'install_dir'), | |
421 | ('force', 'force'), |
|
422 | ('force', 'force'), | |
422 | ('skip_build', 'skip_build'), |
|
423 | ('skip_build', 'skip_build'), | |
423 | ) |
|
424 | ) | |
424 |
|
425 | |||
425 | #--------------------------------------------------------------------------- |
|
426 | #--------------------------------------------------------------------------- | |
426 | # Verify all dependencies |
|
427 | # Verify all dependencies | |
427 | #--------------------------------------------------------------------------- |
|
428 | #--------------------------------------------------------------------------- | |
428 |
|
429 | |||
429 | def check_for_dependencies(): |
|
430 | def check_for_dependencies(): | |
430 | """Check for IPython's dependencies. |
|
431 | """Check for IPython's dependencies. | |
431 |
|
432 | |||
432 | This function should NOT be called if running under setuptools! |
|
433 | This function should NOT be called if running under setuptools! | |
433 | """ |
|
434 | """ | |
434 | from setupext.setupext import ( |
|
435 | from setupext.setupext import ( | |
435 | print_line, print_raw, print_status, |
|
436 | print_line, print_raw, print_status, | |
436 | check_for_sphinx, check_for_pygments, |
|
437 | check_for_sphinx, check_for_pygments, | |
437 | check_for_nose, check_for_pexpect, |
|
438 | check_for_nose, check_for_pexpect, | |
438 | check_for_pyzmq, check_for_readline, |
|
439 | check_for_pyzmq, check_for_readline, | |
439 | check_for_jinja2, check_for_tornado |
|
440 | check_for_jinja2, check_for_tornado | |
440 | ) |
|
441 | ) | |
441 | print_line() |
|
442 | print_line() | |
442 | print_raw("BUILDING IPYTHON") |
|
443 | print_raw("BUILDING IPYTHON") | |
443 | print_status('python', sys.version) |
|
444 | print_status('python', sys.version) | |
444 | print_status('platform', sys.platform) |
|
445 | print_status('platform', sys.platform) | |
445 | if sys.platform == 'win32': |
|
446 | if sys.platform == 'win32': | |
446 | print_status('Windows version', sys.getwindowsversion()) |
|
447 | print_status('Windows version', sys.getwindowsversion()) | |
447 |
|
448 | |||
448 | print_raw("") |
|
449 | print_raw("") | |
449 | print_raw("OPTIONAL DEPENDENCIES") |
|
450 | print_raw("OPTIONAL DEPENDENCIES") | |
450 |
|
451 | |||
451 | check_for_sphinx() |
|
452 | check_for_sphinx() | |
452 | check_for_pygments() |
|
453 | check_for_pygments() | |
453 | check_for_nose() |
|
454 | check_for_nose() | |
454 | check_for_pexpect() |
|
455 | check_for_pexpect() | |
455 | check_for_pyzmq() |
|
456 | check_for_pyzmq() | |
456 | check_for_tornado() |
|
457 | check_for_tornado() | |
457 | check_for_readline() |
|
458 | check_for_readline() | |
458 | check_for_jinja2() |
|
459 | check_for_jinja2() | |
459 |
|
460 | |||
460 | #--------------------------------------------------------------------------- |
|
461 | #--------------------------------------------------------------------------- | |
461 | # VCS related |
|
462 | # VCS related | |
462 | #--------------------------------------------------------------------------- |
|
463 | #--------------------------------------------------------------------------- | |
463 |
|
464 | |||
464 | # utils.submodule has checks for submodule status |
|
465 | # utils.submodule has checks for submodule status | |
465 | execfile(pjoin('IPython','utils','submodule.py'), globals()) |
|
466 | execfile(pjoin('IPython','utils','submodule.py'), globals()) | |
466 |
|
467 | |||
467 | class UpdateSubmodules(Command): |
|
468 | class UpdateSubmodules(Command): | |
468 | """Update git submodules |
|
469 | """Update git submodules | |
469 |
|
470 | |||
470 | IPython's external javascript dependencies live in a separate repo. |
|
471 | IPython's external javascript dependencies live in a separate repo. | |
471 | """ |
|
472 | """ | |
472 | description = "Update git submodules" |
|
473 | description = "Update git submodules" | |
473 | user_options = [] |
|
474 | user_options = [] | |
474 |
|
475 | |||
475 | def initialize_options(self): |
|
476 | def initialize_options(self): | |
476 | pass |
|
477 | pass | |
477 |
|
478 | |||
478 | def finalize_options(self): |
|
479 | def finalize_options(self): | |
479 | pass |
|
480 | pass | |
480 |
|
481 | |||
481 | def run(self): |
|
482 | def run(self): | |
482 | failure = False |
|
483 | failure = False | |
483 | try: |
|
484 | try: | |
484 | self.spawn('git submodule init'.split()) |
|
485 | self.spawn('git submodule init'.split()) | |
485 | self.spawn('git submodule update --recursive'.split()) |
|
486 | self.spawn('git submodule update --recursive'.split()) | |
486 | except Exception as e: |
|
487 | except Exception as e: | |
487 | failure = e |
|
488 | failure = e | |
488 | print(e) |
|
489 | print(e) | |
489 |
|
490 | |||
490 | if not check_submodule_status(repo_root) == 'clean': |
|
491 | if not check_submodule_status(repo_root) == 'clean': | |
491 | print("submodules could not be checked out") |
|
492 | print("submodules could not be checked out") | |
492 | sys.exit(1) |
|
493 | sys.exit(1) | |
493 |
|
494 | |||
494 |
|
495 | |||
495 | def git_prebuild(pkg_dir, build_cmd=build_py): |
|
496 | def git_prebuild(pkg_dir, build_cmd=build_py): | |
496 | """Return extended build or sdist command class for recording commit |
|
497 | """Return extended build or sdist command class for recording commit | |
497 |
|
498 | |||
498 | records git commit in IPython.utils._sysinfo.commit |
|
499 | records git commit in IPython.utils._sysinfo.commit | |
499 |
|
500 | |||
500 | for use in IPython.utils.sysinfo.sys_info() calls after installation. |
|
501 | for use in IPython.utils.sysinfo.sys_info() calls after installation. | |
501 |
|
502 | |||
502 | Also ensures that submodules exist prior to running |
|
503 | Also ensures that submodules exist prior to running | |
503 | """ |
|
504 | """ | |
504 |
|
505 | |||
505 | class MyBuildPy(build_cmd): |
|
506 | class MyBuildPy(build_cmd): | |
506 | ''' Subclass to write commit data into installation tree ''' |
|
507 | ''' Subclass to write commit data into installation tree ''' | |
507 | def run(self): |
|
508 | def run(self): | |
508 | build_cmd.run(self) |
|
509 | build_cmd.run(self) | |
509 | # this one will only fire for build commands |
|
510 | # this one will only fire for build commands | |
510 | if hasattr(self, 'build_lib'): |
|
511 | if hasattr(self, 'build_lib'): | |
511 | self._record_commit(self.build_lib) |
|
512 | self._record_commit(self.build_lib) | |
512 |
|
513 | |||
513 | def make_release_tree(self, base_dir, files): |
|
514 | def make_release_tree(self, base_dir, files): | |
514 | # this one will fire for sdist |
|
515 | # this one will fire for sdist | |
515 | build_cmd.make_release_tree(self, base_dir, files) |
|
516 | build_cmd.make_release_tree(self, base_dir, files) | |
516 | self._record_commit(base_dir) |
|
517 | self._record_commit(base_dir) | |
517 |
|
518 | |||
518 | def _record_commit(self, base_dir): |
|
519 | def _record_commit(self, base_dir): | |
519 | import subprocess |
|
520 | import subprocess | |
520 | proc = subprocess.Popen('git rev-parse --short HEAD', |
|
521 | proc = subprocess.Popen('git rev-parse --short HEAD', | |
521 | stdout=subprocess.PIPE, |
|
522 | stdout=subprocess.PIPE, | |
522 | stderr=subprocess.PIPE, |
|
523 | stderr=subprocess.PIPE, | |
523 | shell=True) |
|
524 | shell=True) | |
524 | repo_commit, _ = proc.communicate() |
|
525 | repo_commit, _ = proc.communicate() | |
525 | repo_commit = repo_commit.strip().decode("ascii") |
|
526 | repo_commit = repo_commit.strip().decode("ascii") | |
526 |
|
527 | |||
527 | out_pth = pjoin(base_dir, pkg_dir, 'utils', '_sysinfo.py') |
|
528 | out_pth = pjoin(base_dir, pkg_dir, 'utils', '_sysinfo.py') | |
528 | if os.path.isfile(out_pth) and not repo_commit: |
|
529 | if os.path.isfile(out_pth) and not repo_commit: | |
529 | # nothing to write, don't clobber |
|
530 | # nothing to write, don't clobber | |
530 | return |
|
531 | return | |
531 |
|
532 | |||
532 | print("writing git commit '%s' to %s" % (repo_commit, out_pth)) |
|
533 | print("writing git commit '%s' to %s" % (repo_commit, out_pth)) | |
533 |
|
534 | |||
534 | # remove to avoid overwriting original via hard link |
|
535 | # remove to avoid overwriting original via hard link | |
535 | try: |
|
536 | try: | |
536 | os.remove(out_pth) |
|
537 | os.remove(out_pth) | |
537 | except (IOError, OSError): |
|
538 | except (IOError, OSError): | |
538 | pass |
|
539 | pass | |
539 | with open(out_pth, 'w') as out_file: |
|
540 | with open(out_pth, 'w') as out_file: | |
540 | out_file.writelines([ |
|
541 | out_file.writelines([ | |
541 | '# GENERATED BY setup.py\n', |
|
542 | '# GENERATED BY setup.py\n', | |
542 | 'commit = "%s"\n' % repo_commit, |
|
543 | 'commit = "%s"\n' % repo_commit, | |
543 | ]) |
|
544 | ]) | |
544 | return require_submodules(MyBuildPy) |
|
545 | return require_submodules(MyBuildPy) | |
545 |
|
546 | |||
546 |
|
547 | |||
547 | def require_submodules(command): |
|
548 | def require_submodules(command): | |
548 | """decorator for instructing a command to check for submodules before running""" |
|
549 | """decorator for instructing a command to check for submodules before running""" | |
549 | class DecoratedCommand(command): |
|
550 | class DecoratedCommand(command): | |
550 | def run(self): |
|
551 | def run(self): | |
551 | if not check_submodule_status(repo_root) == 'clean': |
|
552 | if not check_submodule_status(repo_root) == 'clean': | |
552 | print("submodules missing! Run `setup.py submodule` and try again") |
|
553 | print("submodules missing! Run `setup.py submodule` and try again") | |
553 | sys.exit(1) |
|
554 | sys.exit(1) | |
554 | command.run(self) |
|
555 | command.run(self) | |
555 | return DecoratedCommand |
|
556 | return DecoratedCommand | |
556 |
|
557 | |||
557 | #--------------------------------------------------------------------------- |
|
558 | #--------------------------------------------------------------------------- | |
558 | # Notebook related |
|
559 | # Notebook related | |
559 | #--------------------------------------------------------------------------- |
|
560 | #--------------------------------------------------------------------------- | |
560 |
|
561 | |||
561 | class CompileCSS(Command): |
|
562 | class CompileCSS(Command): | |
562 | """Recompile Notebook CSS |
|
563 | """Recompile Notebook CSS | |
563 |
|
564 | |||
564 | Regenerate the compiled CSS from LESS sources. |
|
565 | Regenerate the compiled CSS from LESS sources. | |
565 |
|
566 | |||
566 | Requires various dev dependencies, such as fabric and lessc. |
|
567 | Requires various dev dependencies, such as fabric and lessc. | |
567 | """ |
|
568 | """ | |
568 | description = "Recompile Notebook CSS" |
|
569 | description = "Recompile Notebook CSS" | |
569 | user_options = [] |
|
570 | user_options = [] | |
570 |
|
571 | |||
571 | def initialize_options(self): |
|
572 | def initialize_options(self): | |
572 | pass |
|
573 | pass | |
573 |
|
574 | |||
574 | def finalize_options(self): |
|
575 | def finalize_options(self): | |
575 | pass |
|
576 | pass | |
576 |
|
577 | |||
577 | def run(self): |
|
578 | def run(self): | |
578 | call("fab css", shell=True, cwd=pjoin(repo_root, "IPython", "html")) |
|
579 | call("fab css", shell=True, cwd=pjoin(repo_root, "IPython", "html")) | |
579 |
|
580 | |||
580 | class JavascriptVersion(Command): |
|
581 | class JavascriptVersion(Command): | |
581 | """write the javascript version to notebook javascript""" |
|
582 | """write the javascript version to notebook javascript""" | |
582 | description = "Write IPython version to javascript" |
|
583 | description = "Write IPython version to javascript" | |
583 | user_options = [] |
|
584 | user_options = [] | |
584 |
|
585 | |||
585 | def initialize_options(self): |
|
586 | def initialize_options(self): | |
586 | pass |
|
587 | pass | |
587 |
|
588 | |||
588 | def finalize_options(self): |
|
589 | def finalize_options(self): | |
589 | pass |
|
590 | pass | |
590 |
|
591 | |||
591 | def run(self): |
|
592 | def run(self): | |
592 | nsfile = pjoin(repo_root, "IPython", "html", "static", "base", "js", "namespace.js") |
|
593 | nsfile = pjoin(repo_root, "IPython", "html", "static", "base", "js", "namespace.js") | |
593 | with open(nsfile) as f: |
|
594 | with open(nsfile) as f: | |
594 | lines = f.readlines() |
|
595 | lines = f.readlines() | |
595 | with open(nsfile, 'w') as f: |
|
596 | with open(nsfile, 'w') as f: | |
596 | for line in lines: |
|
597 | for line in lines: | |
597 | if line.startswith("IPython.version"): |
|
598 | if line.startswith("IPython.version"): | |
598 | line = 'IPython.version = "{0}";\n'.format(version) |
|
599 | line = 'IPython.version = "{0}";\n'.format(version) | |
599 | f.write(line) |
|
600 | f.write(line) | |
600 |
|
601 |
General Comments 0
You need to be logged in to leave comments.
Login now