Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59dba38

Browse files
committed
Add a button to the code examples in the doc to show/hide the prompts and output.
1 parent ef4e2fa commit 59dba38

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Doc/tools/sphinxext/layout.html

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

0 commit comments

Comments
 (0)