1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
-
-
- function* getHideableCopyButtonElements(rootElement) {
-
-
- for (const el of rootElement.querySelectorAll('.go, .gp, .gt')) {
- yield el
- }
-
-
- for (let el of rootElement.querySelectorAll('.gt')) {
- while ((el = el.nextSibling) && el.nodeType !== Node.DOCUMENT_NODE) {
-
-
- if (el.nodeType === Node.ELEMENT_NODE && el.matches(".gp, .go")) {
- break
- }
-
-
- if (el.nodeType === Node.TEXT_NODE && el.textContent.trim()) {
- const wrapper = document.createElement("span")
- el.after(wrapper)
- wrapper.appendChild(el)
- el = wrapper
- }
- yield el
- }
- }
- }
-
-
- const loadCopyButton = () => {
-
-
- const hide_text = "Hide the prompts and output"
- const show_text = "Show the prompts and output"
-
- const button = document.createElement("span")
- button.classList.add("copybutton")
- button.innerText = ">>>"
- button.title = hide_text
- button.dataset.hidden = "false"
- const buttonClick = event => {
-
- event.preventDefault()
- const buttonEl = event.currentTarget
- const codeEl = buttonEl.nextElementSibling
- if (buttonEl.dataset.hidden === "false") {
-
- for (const el of getHideableCopyButtonElements(codeEl)) {
- el.hidden = true
- }
- buttonEl.title = show_text
- buttonEl.dataset.hidden = "true"
- } else {
-
- for (const el of getHideableCopyButtonElements(codeEl)) {
- el.hidden = false
- }
- buttonEl.title = hide_text
- buttonEl.dataset.hidden = "false"
- }
- }
-
- const highlightedElements = document.querySelectorAll(
- ".highlight-python .highlight,"
- + ".highlight-python3 .highlight,"
- + ".highlight-pycon .highlight,"
- + ".highlight-pycon3 .highlight,"
- + ".highlight-default .highlight"
- )
-
-
- highlightedElements.forEach(el => {
- el.style.position = "relative"
-
-
- const clonedButton = button.cloneNode(true)
-
- clonedButton.onclick = buttonClick
- if (el.querySelector(".gp") !== null) {
- el.prepend(clonedButton)
- }
- })
- }
-
- if (document.readyState !== "loading") {
- loadCopyButton()
- } else {
- document.addEventListener("DOMContentLoaded", loadCopyButton)
- }
|