blob: 716c9e472f4bebe97e3bb7ec8dd230aa574b7967 [file] [log] [blame]
Ezio Melottif54f6f52011-10-30 09:20:19 +02001$(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 Brandl6a96a2e2011-12-04 11:51:21 +010011 var hide_text = 'Hide the prompts and output';
12 var show_text = 'Show the prompts and output';
Ezio Melottif54f6f52011-10-30 09:20:19 +020013 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 Brandlab712142012-03-25 20:31:57 +020020 'font-family': 'monospace', 'padding-left': '0.2em', 'padding-right': '0.2em',
21 'border-radius': '0 3px 0 0'
Ezio Melottif54f6f52011-10-30 09:20:19 +020022 }
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">&gt;&gt;&gt;</span>');
29 button.css(button_styles)
30 button.attr('title', hide_text);
Berker Peksag25c0ef52016-03-02 19:40:08 +020031 button.data('hidden', 'false');
Ezio Melottif54f6f52011-10-30 09:20:19 +020032 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
Ezio Melotti90ba2ca2016-02-27 08:39:36 +020042 $('.copybutton').click(function(e){
43 e.preventDefault();
44 var button = $(this);
45 if (button.data('hidden') === 'false') {
46 // hide the code output
Ezio Melottif54f6f52011-10-30 09:20:19 +020047 button.parent().find('.go, .gp, .gt').hide();
48 button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
49 button.css('text-decoration', 'line-through');
50 button.attr('title', show_text);
Ezio Melotti90ba2ca2016-02-27 08:39:36 +020051 button.data('hidden', 'true');
52 } else {
53 // show the code output
Ezio Melottif54f6f52011-10-30 09:20:19 +020054 button.parent().find('.go, .gp, .gt').show();
55 button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
56 button.css('text-decoration', 'none');
57 button.attr('title', hide_text);
Ezio Melotti90ba2ca2016-02-27 08:39:36 +020058 button.data('hidden', 'false');
59 }
60 });
Ezio Melottif54f6f52011-10-30 09:20:19 +020061});
62