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
5 var div = $('.highlight-python .highlight,' +
6 '.highlight-python3 .highlight,' +
7 '.highlight-default .highlight')
8 var pre = div.find('pre');
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');
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'
25 // create and add the button to all the code blocks that contain >>>
26 div.each(function(index) {
28 if (jthis.find('.gp').length > 0) {
29 var button = $('<span class="copybutton">>>></span>');
30 button.css(button_styles)
31 button.attr('title', hide_text);
32 button.data('hidden', 'false');
33 jthis.prepend(button);
35 // tracebacks (.gt) contain bare text elements that need to be
36 // wrapped in a span to work with .nextUntil() (see later)
37 jthis.find('pre:has(.gt)').contents().filter(function() {
38 return ((this.nodeType == 3) && (this.data.trim().length > 0));
42 // define the behavior of the button when it's clicked
43 $('.copybutton').click(function(e){
46 if (button.data('hidden') === 'false') {
47 // hide the code output
48 button.parent().find('.go, .gp, .gt').hide();
49 button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
50 button.css('text-decoration', 'line-through');
51 button.attr('title', show_text);
52 button.data('hidden', 'true');
54 // show the code output
55 button.parent().find('.go, .gp, .gt').show();
56 button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
57 button.css('text-decoration', 'none');
58 button.attr('title', hide_text);
59 button.data('hidden', 'false');