Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit e70bdcf

Browse files
committed
add copybutton from cpython sources
allows toggling the input markers in source examples to allow copy & pasting them.
1 parent 65c59fe commit e70bdcf

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

_theme/scipy/layout.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@
9696
HAS_SOURCE: {{ has_source|lower }}
9797
};
9898
</script>
99-
<script type="text/javascript" src="{{ pathto('_static/js/jquery.min.js', 1) }}"></script>
100-
<script type="text/javascript" src="{{ pathto('_static/js/bootstrap.min.js', 1) }}"></script>
10199
{%- for scriptfile in script_files %}
102100
<script type="text/javascript" src="{{ pathto(scriptfile, 1) }}"></script>
103101
{%- endfor %}
102+
<script type="text/javascript" src="{{ pathto('_static/js/jquery.min.js', 1) }}"></script>
103+
<script type="text/javascript" src="{{ pathto('_static/js/bootstrap.min.js', 1) }}"></script>
104+
<script type="text/javascript" src="{{ pathto('_static/js/copybutton.js', 1) }}"></script>
104105
{%- endmacro %}
105106

106107
{%- macro css() %}

_theme/scipy/static/js/copybutton.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2014 PSF. Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
2+
// File originates from the cpython source found in Doc/tools/sphinxext/static/copybutton.js
3+
4+
$(document).ready(function() {
5+
/* Add a [>>>] button on the top-right corner of code samples to hide
6+
* the >>> and ... prompts and the output and thus make the code
7+
* copyable. */
8+
var div = $('.highlight-python .highlight,' +
9+
'.highlight-python3 .highlight')
10+
var pre = div.find('pre');
11+
12+
// get the styles from the current theme
13+
pre.parent().parent().css('position', 'relative');
14+
var hide_text = 'Hide the prompts and output';
15+
var show_text = 'Show the prompts and output';
16+
var border_width = pre.css('border-top-width');
17+
var border_style = pre.css('border-top-style');
18+
var border_color = pre.css('border-top-color');
19+
var button_styles = {
20+
'cursor':'pointer', 'position': 'absolute', 'top': '0', 'right': '0',
21+
'border-color': border_color, 'border-style': border_style,
22+
'border-width': border_width, 'color': border_color, 'text-size': '75%',
23+
'font-family': 'monospace', 'padding-left': '0.2em', 'padding-right': '0.2em',
24+
'border-radius': '0 3px 0 0'
25+
}
26+
27+
// create and add the button to all the code blocks that contain >>>
28+
div.each(function(index) {
29+
var jthis = $(this);
30+
if (jthis.find('.gp').length > 0) {
31+
var button = $('<span class="copybutton">&gt;&gt;&gt;</span>');
32+
button.css(button_styles)
33+
button.attr('title', hide_text);
34+
jthis.prepend(button);
35+
}
36+
// tracebacks (.gt) contain bare text elements that need to be
37+
// wrapped in a span to work with .nextUntil() (see later)
38+
jthis.find('pre:has(.gt)').contents().filter(function() {
39+
return ((this.nodeType == 3) && (this.data.trim().length > 0));
40+
}).wrap('<span>');
41+
});
42+
43+
// define the behavior of the button when it's clicked
44+
$('.copybutton').toggle(
45+
function() {
46+
var button = $(this);
47+
button.parent().find('.go, .gp, .gt').hide();
48+
button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
49+
button.css('text-decoration', 'line-through');
50+
button.attr('title', show_text);
51+
},
52+
function() {
53+
var button = $(this);
54+
button.parent().find('.go, .gp, .gt').show();
55+
button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
56+
button.css('text-decoration', 'none');
57+
button.attr('title', hide_text);
58+
});
59+
});
60+

0 commit comments

Comments
 (0)