Issues (587)

docs/_static/copybutton.js (1 issue)

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-default .highlight,' +
6
                '.highlight-python .highlight,' +
7
                '.highlight-python3 .highlight'
8
                )
9
    var pre = div.find('pre');
10
11
    // get the styles from the current theme
12
    pre.parent().parent().css('position', 'relative');
13
    var hide_text = 'Hide the prompts and output';
14
    var show_text = 'Show the prompts and output';
15
    var border_width = pre.css('border-top-width');
16
    var border_style = pre.css('border-top-style');
17
    var border_color = pre.css('border-top-color');
18
    var button_styles = {
19
        'cursor':'pointer', 'position': 'absolute', 'top': '0', 'right': '0',
20
        'border-color': border_color, 'border-style': border_style,
21
        'border-width': border_width, 'color': border_color, 'text-size': '75%',
22
        'font-family': 'monospace', 'padding-left': '0.2em', 'padding-right': '0.2em',
23
        'border-radius': '0 3px 0 0'
24
    }
25
26
    // create and add the button to all the code blocks that contain >>>
27
    div.each(function(index) {
0 ignored issues
show
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
28
        var jthis = $(this);
29
        if (jthis.find('.gp').length > 0) {
30
            var button = $('<span class="copybutton">&gt;&gt;&gt;</span>');
31
            button.css(button_styles)
32
            button.attr('title', hide_text);
33
            button.data('hidden', 'false');
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').click(function(e){
45
        e.preventDefault();
46
        var button = $(this);
47
        if (button.data('hidden') === 'false') {
48
            // hide the code output
49
            button.parent().find('.go, .gp, .gt').hide();
50
            button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
51
            button.css('text-decoration', 'line-through');
52
            button.attr('title', show_text);
53
            button.data('hidden', 'true');
54
        } else {
55
            // show the code output
56
            button.parent().find('.go, .gp, .gt').show();
57
            button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
58
            button.css('text-decoration', 'none');
59
            button.attr('title', hide_text);
60
            button.data('hidden', 'false');
61
        }
62
    });
63
});
64