Debian Codegraph @

doc/_static/copybutton.js

raw symbols SCIP
1 // Copyright 2014 PSF. Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
2 // File originates from the cpython source found in Doc/tools/sphinxext/static/copybutton.js
3
4 // http://forum.jquery.com/topic/beginner-function-toggle-deprecated-what-to-use-instead#14737000003772143
5 $.fn.clicktoggle = function(a, b) {
6 return this.each(function() {
7 var clicked = false;
8 $(this).click(function() {
9 if (clicked) {
10 clicked = false;
11 return b.apply(this, arguments);
12 }
13 clicked = true;
14 return a.apply(this, arguments);
15 });
16 });
17 };
18
19 $(document).ready(function() {
20 /* Add a [>>>] button on the top-right corner of code samples to hide
21 * the >>> and ... prompts and the output and thus make the code
22 * copyable. */
23 var div = $('.highlight-default .highlight,' +
24 '.highlight-python .highlight' +
25 '.highlight-python3 .highlight')
26 var pre = div.find('pre');
27
28 // get the styles from the current theme
29 pre.parent().parent().css('position', 'relative');
30 var hide_text = 'Hide the prompts and output';
31 var show_text = 'Show the prompts and output';
32 var border_width = pre.css('border-top-width');
33 var border_style = pre.css('border-top-style');
34 var border_color = pre.css('border-top-color');
35 var button_styles = {
36 'cursor':'pointer', 'position': 'absolute', 'top': '0', 'right': '0',
37 'border-color': border_color, 'border-style': border_style,
38 'border-width': border_width, 'color': border_color, 'text-size': '75%',
39 'font-family': 'monospace', 'padding-left': '0.2em', 'padding-right': '0.2em',
40 'border-radius': '0 3px 0 0'
41 }
42
43 // create and add the button to all the code blocks that contain >>>
44 div.each(function(index) {
45 var jthis = $(this);
46 if (jthis.find('.gp').length > 0) {
47 var button = $('<span class="copybutton">&gt;&gt;&gt;</span>');
48 button.css(button_styles);
49 button.attr('title', hide_text);
50 jthis.prepend(button);
51 }
52 // tracebacks (.gt) contain bare text elements that need to be
53 // wrapped in a span to work with .nextUntil() (see later)
54 jthis.find('pre:has(.gt)').contents().filter(function() {
55 return ((this.nodeType == 3) && (this.data.trim().length > 0));
56 }).wrap('<span>');
57 });
58
59 // define the behavior of the button when it's clicked
60 $('.copybutton').clicktoggle(
61 function() {
62 var button = $(this);
63 button.parent().find('.go, .gp, .gt').hide();
64 button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
65 button.css('text-decoration', 'line-through');
66 button.attr('title', show_text);
67 },
68 function() {
69 var button = $(this);
70 button.parent().find('.go, .gp, .gt').show();
71 button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
72 button.css('text-decoration', 'none');
73 button.attr('title', hide_text);
74 });
75 });
76