PageSourceSearch

https://docs.pypsa.org/latest/assets/javascripts/copy-doctest.js

js pypsa.org collected 2026-09-24 09:21:40 UTC 1,058 bytes, 32 lines download raw bytes

1// SPDX-FileCopyrightText: PyPSA Contributors
2//
3// SPDX-License-Identifier: MIT
4
5// Strip doctest prompts (>>> / ...) and output lines from the copy button, so
6// users copy runnable code while the source keeps >>> for the doctest suite.
7document.addEventListener("DOMContentLoaded", () => {
8  document.querySelectorAll(".highlight").forEach((block) => {
9    const btn = block.querySelector(".md-clipboard");
10    const code = block.querySelector("code");
11    if (!btn || !code) return;
12
13    const raw = code.innerText;
14    if (!/^\s*>>> /m.test(raw)) return; // only REPL blocks
15
16    // Capture phase + stopPropagation runs before ClipboardJS's delegated handler.
17    btn.addEventListener(
18      "click",
19      (e) => {
20        e.stopPropagation();
21        e.preventDefault();
22        const cleaned = raw
23          .split("\n")
24          .filter((l) => /^\s*(>>>|\.\.\.) /.test(l))
25          .map((l) => l.replace(/^\s*(>>>|\.\.\.) ?/, ""))
26          .join("\n");
27        navigator.clipboard.writeText(cleaned + "\n");
28      },
29      true,
30    );
31  });
32});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.