Ezio Melotti | f54f6f5 | 2011-10-30 09:20:19 +0200 | [diff] [blame] | 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'); |
Georg Brandl | 6a96a2e | 2011-12-04 11:51:21 +0100 | [diff] [blame] | 11 | var hide_text = 'Hide the prompts and output'; |
| 12 | var show_text = 'Show the prompts and output'; |
Ezio Melotti | f54f6f5 | 2011-10-30 09:20:19 +0200 | [diff] [blame] | 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%', |
Georg Brandl | ab71214 | 2012-03-25 20:31:57 +0200 | [diff] [blame] | 20 | 'font-family': 'monospace', 'padding-left': '0.2em', 'padding-right': '0.2em', |
| 21 | 'border-radius': '0 3px 0 0' |
Ezio Melotti | f54f6f5 | 2011-10-30 09:20:19 +0200 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | // create and add the button to all the code blocks that contain >>> |
| 25 | div.each(function(index) { |
| 26 | var jthis = $(this); |
| 27 | if (jthis.find('.gp').length > 0) { |
| 28 | var button = $('<span class="copybutton">>>></span>'); |
| 29 | button.css(button_styles) |
| 30 | button.attr('title', hide_text); |
| 31 | jthis.prepend(button); |
| 32 | } |
| 33 | // tracebacks (.gt) contain bare text elements that need to be |
| 34 | // wrapped in a span to work with .nextUntil() (see later) |
| 35 | jthis.find('pre:has(.gt)').contents().filter(function() { |
| 36 | return ((this.nodeType == 3) && (this.data.trim().length > 0)); |
| 37 | }).wrap('<span>'); |
| 38 | }); |
| 39 | |
| 40 | // define the behavior of the button when it's clicked |
Ezio Melotti | 90ba2ca | 2016-02-27 08:39:36 +0200 | [diff] [blame] | 41 | $('.copybutton').click(function(e){ |
| 42 | e.preventDefault(); |
| 43 | var button = $(this); |
| 44 | if (button.data('hidden') === 'false') { |
| 45 | // hide the code output |
Ezio Melotti | f54f6f5 | 2011-10-30 09:20:19 +0200 | [diff] [blame] | 46 | button.parent().find('.go, .gp, .gt').hide(); |
| 47 | button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden'); |
| 48 | button.css('text-decoration', 'line-through'); |
| 49 | button.attr('title', show_text); |
Ezio Melotti | 90ba2ca | 2016-02-27 08:39:36 +0200 | [diff] [blame] | 50 | button.data('hidden', 'true'); |
| 51 | } else { |
| 52 | // show the code output |
Ezio Melotti | f54f6f5 | 2011-10-30 09:20:19 +0200 | [diff] [blame] | 53 | button.parent().find('.go, .gp, .gt').show(); |
| 54 | button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible'); |
| 55 | button.css('text-decoration', 'none'); |
| 56 | button.attr('title', hide_text); |
Ezio Melotti | 90ba2ca | 2016-02-27 08:39:36 +0200 | [diff] [blame] | 57 | button.data('hidden', 'false'); |
| 58 | } |
| 59 | }); |
Ezio Melotti | f54f6f5 | 2011-10-30 09:20:19 +0200 | [diff] [blame] | 60 | }); |
| 61 | |