Add copy button to code examples
Hi!
I'm planning to move this work to this repo.
@AA-Turner Suggested to me in this PR https://github.com/python/cpython/pull/107037#issuecomment-1654256388 to do that. Please tell me, what do you think :) Do you agree that that's a good idea?
If so, I guess I would need to rewrite this function, right? https://github.com/python/python-docs-theme/blob/main/python_docs_theme/static/copybutton.js
We'd probably want to evaluate what exists in the literature thus far; it might be that the copybutton.js that I rewrote is redundant. It functions to hide certain Pygments classes denoting output from interactive console display blocks.
Should we decide to add a "click to copy" button, I think the functionality should be merged with the existing button.
A
May I ask you, what do you mean by "We'd probably want to evaluate what exists in the literature thus far;"? :)
E.g. https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard; https://clipboardjs.com/ -- there are many ways to and libraries for interacting with the clipboard -- ought we abandon our JavaScript, ought we adopt a pre-existing library, ought we use the browser APIs, etc.
A
FYI, there's also a pre-made sphinx_copybutton extension available, that uses clipboard.js.
(Which, btw, can):
-
Automatically remove prompts from the copied lines:
copybutton_prompt_text = r">>> |\.\.\. " copybutton_prompt_is_regexp = True copybutton_remove_prompts = True # (default) -
Copy only lines with prompts, when it finds prompts:
copybutton_only_copy_prompt_lines = True # (default) -
Copy multiple wrapped lines
and re-assemble them, if the first one starts with a prompt:copybutton_line_continuation_character = "\\"
Full docs: https://sphinx-copybutton.readthedocs.io/