|
1 |
| -$(document).ready(function() { |
2 |
| - /* Add a [>>>] button on the top-right corner of code samples to hide |
| 1 | +// ``function*`` denotes a generator in JavaScript, see |
| 2 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function* |
| 3 | +function* getHideableCopyButtonElements(rootElement) { |
| 4 | + // yield all elements with the "go" (Generic.Output), |
| 5 | + // "gp" (Generic.Prompt), or "gt" (Generic.Traceback) CSS class |
| 6 | + for (const el of rootElement.querySelectorAll('.go, .gp, .gt')) { |
| 7 | + yield el |
| 8 | + } |
| 9 | + // tracebacks (.gt) contain bare text elements that need to be |
| 10 | + // wrapped in a span to hide or show the element |
| 11 | + for (let el of rootElement.querySelectorAll('.gt')) { |
| 12 | + while ((el = el.nextSibling) && el.nodeType !== Node.DOCUMENT_NODE) { |
| 13 | + // stop wrapping text nodes when we hit the next output or |
| 14 | + // prompt element |
| 15 | + if (el.nodeType === Node.ELEMENT_NODE && el.matches(".gp, .go")) { |
| 16 | + break |
| 17 | + } |
| 18 | + // if the node is a text node with content, wrap it in a |
| 19 | + // span element so that we can control visibility |
| 20 | + if (el.nodeType === Node.TEXT_NODE && el.textContent.trim()) { |
| 21 | + const wrapper = document.createElement("span") |
| 22 | + el.after(wrapper) |
| 23 | + wrapper.appendChild(el) |
| 24 | + el = wrapper |
| 25 | + } |
| 26 | + yield el |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +const loadCopyButton = () => { |
| 33 | + /* Add a [>>>] button in the top-right corner of code samples to hide |
3 | 34 | * the >>> and ... prompts and the output and thus make the code
|
4 | 35 | * copyable. */
|
5 |
| - var div = $('.highlight-python .highlight,' + |
6 |
| - '.highlight-python3 .highlight,' + |
7 |
| - '.highlight-pycon .highlight,' + |
8 |
| - '.highlight-pycon3 .highlight,' + |
9 |
| - '.highlight-default .highlight'); |
10 |
| - var pre = div.find('pre'); |
| 36 | + const hide_text = "Hide the prompts and output" |
| 37 | + const show_text = "Show the prompts and output" |
11 | 38 |
|
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', 'position': 'absolute', 'top': '0', 'right': '0', |
21 |
| - 'border-color': border_color, 'border-style': border_style, |
22 |
| - 'border-width': border_width, 'color': border_color, 'text-size': '75%', |
23 |
| - 'font-family': 'monospace', 'padding-left': '0.2em', 'padding-right': '0.2em', |
24 |
| - 'border-radius': '0 3px 0 0' |
| 39 | + const button = document.createElement("span") |
| 40 | + button.classList.add("copybutton") |
| 41 | + button.innerText = ">>>" |
| 42 | + button.title = hide_text |
| 43 | + button.dataset.hidden = "false" |
| 44 | + const buttonClick = event => { |
| 45 | + // define the behavior of the button when it's clicked |
| 46 | + event.preventDefault() |
| 47 | + const buttonEl = event.currentTarget |
| 48 | + const codeEl = buttonEl.nextElementSibling |
| 49 | + if (buttonEl.dataset.hidden === "false") { |
| 50 | + // hide the code output |
| 51 | + for (const el of getHideableCopyButtonElements(codeEl)) { |
| 52 | + el.hidden = true |
| 53 | + } |
| 54 | + buttonEl.title = show_text |
| 55 | + buttonEl.dataset.hidden = "true" |
| 56 | + } else { |
| 57 | + // show the code output |
| 58 | + for (const el of getHideableCopyButtonElements(codeEl)) { |
| 59 | + el.hidden = false |
| 60 | + } |
| 61 | + buttonEl.title = hide_text |
| 62 | + buttonEl.dataset.hidden = "false" |
| 63 | + } |
25 | 64 | }
|
26 | 65 |
|
| 66 | + const highlightedElements = document.querySelectorAll( |
| 67 | + ".highlight-python .highlight," |
| 68 | + + ".highlight-python3 .highlight," |
| 69 | + + ".highlight-pycon .highlight," |
| 70 | + + ".highlight-pycon3 .highlight," |
| 71 | + + ".highlight-default .highlight" |
| 72 | + ) |
| 73 | + |
27 | 74 | // create and add the button to all the code blocks that contain >>>
|
28 |
| - div.each(function(index) { |
29 |
| - var jthis = $(this); |
30 |
| - if (jthis.find('.gp').length > 0) { |
31 |
| - var button = $('<span class="copybutton">>>></span>'); |
32 |
| - button.css(button_styles) |
33 |
| - button.attr('title', hide_text); |
34 |
| - button.data('hidden', 'false'); |
35 |
| - jthis.prepend(button); |
36 |
| - } |
37 |
| - // tracebacks (.gt) contain bare text elements that need to be |
38 |
| - // wrapped in a span to work with .nextUntil() (see later) |
39 |
| - jthis.find('pre:has(.gt)').contents().filter(function() { |
40 |
| - return ((this.nodeType == 3) && (this.data.trim().length > 0)); |
41 |
| - }).wrap('<span>'); |
42 |
| - }); |
| 75 | + highlightedElements.forEach(el => { |
| 76 | + el.style.position = "relative" |
43 | 77 |
|
44 |
| - // define the behavior of the button when it's clicked |
45 |
| - $('.copybutton').click(function(e){ |
46 |
| - e.preventDefault(); |
47 |
| - var button = $(this); |
48 |
| - if (button.data('hidden') === 'false') { |
49 |
| - // hide the code output |
50 |
| - button.parent().find('.go, .gp, .gt').hide(); |
51 |
| - button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden'); |
52 |
| - button.css('text-decoration', 'line-through'); |
53 |
| - button.attr('title', show_text); |
54 |
| - button.data('hidden', 'true'); |
55 |
| - } else { |
56 |
| - // show the code output |
57 |
| - button.parent().find('.go, .gp, .gt').show(); |
58 |
| - button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible'); |
59 |
| - button.css('text-decoration', 'none'); |
60 |
| - button.attr('title', hide_text); |
61 |
| - button.data('hidden', 'false'); |
| 78 | + // if we find a console prompt (.gp), prepend the (deeply cloned) button |
| 79 | + const clonedButton = button.cloneNode(true) |
| 80 | + // the onclick attribute is not cloned, set it on the new element |
| 81 | + clonedButton.onclick = buttonClick |
| 82 | + if (el.querySelector(".gp") !== null) { |
| 83 | + el.prepend(clonedButton) |
62 | 84 | }
|
63 |
| - }); |
64 |
| -}); |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +if (document.readyState !== "loading") { |
| 89 | + loadCopyButton() |
| 90 | +} else { |
| 91 | + document.addEventListener("DOMContentLoaded", loadCopyButton) |
| 92 | +} |
0 commit comments