pytorch-lightning
78 строк · 2.7 Кб
1/* Copied from the official Python docs: https://docs.python.org/3/_static/copybutton.js */
2$(document).ready(function () {3/* Add a [>>>] button on the top-right corner of code samples to hide4* the >>> and ... prompts and the output and thus make the code
5* copyable. */
6var div = $(7".highlight-python .highlight," +8".highlight-python3 .highlight," +9".highlight-pycon .highlight," +10".highlight-default .highlight",11);12var pre = div.find("pre");13
14// get the styles from the current theme15pre.parent().parent().css("position", "relative");16var hide_text = "Hide the prompts and output";17var show_text = "Show the prompts and output";18var border_width = pre.css("border-top-width");19var border_style = pre.css("border-top-style");20var border_color = pre.css("border-top-color");21var button_styles = {22cursor: "pointer",23position: "absolute",24top: "0",25right: "0",26"border-color": border_color,27"border-style": border_style,28"border-width": border_width,29color: border_color,30"text-size": "75%",31"font-family": "monospace",32"padding-left": "0.2em",33"padding-right": "0.2em",34"border-radius": "0 3px 0 0",35};36
37// create and add the button to all the code blocks that contain >>>38div.each(function (index) {39var jthis = $(this);40if (jthis.find(".gp").length > 0) {41var button = $('<span class="copybutton">>>></span>');42button.css(button_styles);43button.attr("title", hide_text);44button.data("hidden", "false");45jthis.prepend(button);46}47// tracebacks (.gt) contain bare text elements that need to be48// wrapped in a span to work with .nextUntil() (see later)49jthis
50.find("pre:has(.gt)")51.contents()52.filter(function () {53return this.nodeType == 3 && this.data.trim().length > 0;54})55.wrap("<span>");56});57
58// define the behavior of the button when it's clicked59$(".copybutton").click(function (e) {60e.preventDefault();61var button = $(this);62if (button.data("hidden") === "false") {63// hide the code output64button.parent().find(".go, .gp, .gt").hide();65button.next("pre").find(".gt").nextUntil(".gp, .go").css("visibility", "hidden");66button.css("text-decoration", "line-through");67button.attr("title", show_text);68button.data("hidden", "true");69} else {70// show the code output71button.parent().find(".go, .gp, .gt").show();72button.next("pre").find(".gt").nextUntil(".gp, .go").css("visibility", "visible");73button.css("text-decoration", "none");74button.attr("title", hide_text);75button.data("hidden", "false");76}77});78});79