Back to home page

OSCL-LXR

 
 

    


0001 // Copyright 2014 PSF. Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
0002 // File originates from the cpython source found in Doc/tools/sphinxext/static/copybutton.js
0003 
0004 $(document).ready(function() {
0005     /* Add a [>>>] button on the top-right corner of code samples to hide
0006      * the >>> and ... prompts and the output and thus make the code
0007      * copyable. */
0008     var div = $('.highlight-python .highlight,' +
0009                 '.highlight-default .highlight,' +
0010                 '.highlight-python3 .highlight')
0011     var pre = div.find('pre');
0012 
0013     // get the styles from the current theme
0014     pre.parent().parent().css('position', 'relative');
0015     var hide_text = 'Hide the prompts and output';
0016     var show_text = 'Show the prompts and output';
0017     var border_width = pre.css('border-top-width');
0018     var border_style = pre.css('border-top-style');
0019     var border_color = pre.css('border-top-color');
0020     var button_styles = {
0021         'cursor':'pointer', 'position': 'absolute', 'top': '0', 'right': '0',
0022         'border-color': border_color, 'border-style': border_style,
0023         'border-width': border_width, 'color': border_color, 'text-size': '75%',
0024         'font-family': 'monospace', 'padding-left': '0.2em', 'padding-right': '0.2em',
0025         'border-radius': '0 3px 0 0'
0026     }
0027 
0028     // create and add the button to all the code blocks that contain >>>
0029     div.each(function(index) {
0030         var jthis = $(this);
0031         if (jthis.find('.gp').length > 0) {
0032             var button = $('<span class="copybutton">&gt;&gt;&gt;</span>');
0033             button.css(button_styles)
0034             button.attr('title', hide_text);
0035             button.data('hidden', 'false');
0036             jthis.prepend(button);
0037         }
0038         // tracebacks (.gt) contain bare text elements that need to be
0039         // wrapped in a span to work with .nextUntil() (see later)
0040         jthis.find('pre:has(.gt)').contents().filter(function() {
0041             return ((this.nodeType == 3) && (this.data.trim().length > 0));
0042         }).wrap('<span>');
0043     });
0044 
0045     // define the behavior of the button when it's clicked
0046     $('.copybutton').click(function(e){
0047         e.preventDefault();
0048         var button = $(this);
0049         if (button.data('hidden') === 'false') {
0050             // hide the code output
0051             button.parent().find('.go, .gp, .gt').hide();
0052             button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
0053             button.css('text-decoration', 'line-through');
0054             button.attr('title', show_text);
0055             button.data('hidden', 'true');
0056         } else {
0057             // show the code output
0058             button.parent().find('.go, .gp, .gt').show();
0059             button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
0060             button.css('text-decoration', 'none');
0061             button.attr('title', hide_text);
0062             button.data('hidden', 'false');
0063         }
0064     });
0065 });
0066