Skip to content

Commit 5bc0cf9

Browse files
committed
Add button to hide >>> and output of doctest snippets
1 parent 99e77ad commit 5bc0cf9

3 files changed

Lines changed: 9470 additions & 0 deletions

File tree

‎doc/source/_static/copybutton.js‎

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

0 commit comments

Comments
 (0)