Debian Codegraph @

docs/source/_static/show-code.js

raw
1 // Stolen from statsmodels and fixed up
2 // Here's what statsmodels' LICENSE.txt says:
3 //
4 // Copyright (C) 2006, Jonathan E. Taylor
5 // All rights reserved.
6 //
7 // Copyright (c) 2006-2008 Scipy Developers.
8 // All rights reserved.
9 //
10 // Copyright (c) 2009-2012 Statsmodels Developers.
11 // All rights reserved.
12 //
13 //
14 // Redistribution and use in source and binary forms, with or without
15 // modification, are permitted provided that the following conditions are met:
16 //
17 // a. Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimer.
19 // b. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 // c. Neither the name of Statsmodels nor the names of its contributors
23 // may be used to endorse or promote products derived from this software
24 // without specific prior written permission.
25 //
26 //
27 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 // ARE DISCLAIMED. IN NO EVENT SHALL STATSMODELS OR CONTRIBUTORS BE LIABLE FOR
31 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
37 // DAMAGE.
38
39
40 function htmlescape(text){
41 return (text.replace(/&/g, "&")
42 .replace(/</g, "&lt;")
43 .replace(/>/g, "&gt;")
44 .replace(/"/g, "&quot;")
45 .replace(/'/g, "&#39;"))
46 }
47
48 function scrapeText(codebox){
49 /// Returns input lines cleaned of prompt1 and prompt2
50 var lines = codebox.split('\n');
51 var newlines = new Array();
52 $.each(lines, function() {
53 if (this.match(/^In \[\d+]: /)){
54 newlines.push(this.replace(/^(\s)*In \[\d+]: /,""));
55 }
56 else if (this.match(/^(\s)*\.+:/)){
57 newlines.push(this.replace(/^(\s)*\.+: /,""));
58 }
59
60 }
61 );
62 return newlines.join('\\n');
63 }
64
65 $(document).ready(
66 function() {
67 // grab all code boxes
68 var ipythoncode = $(".highlight-ipython");
69 $.each(ipythoncode, function() {
70 var code = scrapeText($(this).text());
71 // give them a facebox pop-up with plain text code
72 $(this).append('<span style="text-align:right; display:block; margin-top:-10px; margin-left:10px; font-size:60%"><a href="javascript: jQuery.facebox(\'<textarea cols=80 rows=10 readonly style=margin:5px onmouseover=javascript:this.select();>'+htmlescape(htmlescape(code))+'</textarea>\');">View Code</a></span>');
73 $(this,"textarea").select();
74 });
75 });