Debian Codegraph @

astropy_sphinx_theme/bootstrap-astropy/static/copybutton.js

raw
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 = $(
6 ".highlight-python .highlight," +
7 ".highlight-python3 .highlight," +
8 ".highlight-default .highlight",
9 );
10 var pre = div.find("pre");
11
12 // get the styles from the current theme
13 pre.parent().parent().css("position", "relative");
14 var hide_text = "Hide the prompts and output";
15 var show_text = "Show the prompts and output";
16 var border_width = pre.css("border-top-width");
17 var border_style = pre.css("border-top-style");
18 var border_color = pre.css("border-top-color");
19 var button_styles = {
20 cursor: "pointer",
21 position: "absolute",
22 top: "0",
23 right: "0",
24 "border-color": border_color,
25 "border-style": border_style,
26 "border-width": border_width,
27 color: border_color,
28 "text-size": "75%",
29 "font-family": "monospace",
30 "padding-left": "0.2em",
31 "padding-right": "0.2em",
32 "border-radius": "0 3px 0 0",
33 };
34
35 // create and add the button to all the code blocks that contain >>>
36 div.each(function (index) {
37 var jthis = $(this);
38 if (jthis.find(".gp").length > 0) {
39 var button = $('<span class="copybutton">&gt;&gt;&gt;</span>');
40 button.css(button_styles);
41 button.attr("title", hide_text);
42 button.data("hidden", "false");
43 jthis.prepend(button);
44 }
45 // tracebacks (.gt) contain bare text elements that need to be
46 // wrapped in a span to work with .nextUntil() (see later)
47 jthis
48 .find("pre:has(.gt)")
49 .contents()
50 .filter(function () {
51 return this.nodeType == 3 && this.data.trim().length > 0;
52 })
53 .wrap("<span>");
54 });
55
56 // define the behavior of the button when it's clicked
57 $(".copybutton").click(function (e) {
58 e.preventDefault();
59 var button = $(this);
60 if (button.data("hidden") === "false") {
61 // hide the code output
62 button.parent().find(".go, .gp, .gt").hide();
63 button
64 .next("pre")
65 .find(".gt")
66 .nextUntil(".gp, .go")
67 .css("visibility", "hidden");
68 button.css("text-decoration", "line-through");
69 button.attr("title", show_text);
70 button.data("hidden", "true");
71 } else {
72 // show the code output
73 button.parent().find(".go, .gp, .gt").show();
74 button
75 .next("pre")
76 .find(".gt")
77 .nextUntil(".gp, .go")
78 .css("visibility", "visible");
79 button.css("text-decoration", "none");
80 button.attr("title", hide_text);
81 button.data("hidden", "false");
82 }
83 });
84 });