python / python-docs-theme

Sphinx theme for Python documentation
Other
74 stars 58 forks source link

Proposal to add copy button to code examples [copy of gh-107024] #154

Open churnikov opened 11 months ago

churnikov commented 11 months ago

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

AA-Turner commented 11 months ago

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

churnikov commented 11 months ago

May I ask you, what do you mean by "We'd probably want to evaluate what exists in the literature thus far;"? :)

AA-Turner commented 11 months ago

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

ferdnyc commented 6 months ago

FYI, there's also a pre-made sphinx_copybutton extension available, that uses clipboard.js.

(Which, btw, can):

  1. Automatically remove prompts from the copied lines:

    copybutton_prompt_text = r">>> |\.\.\. "
    copybutton_prompt_is_regexp = True
    copybutton_remove_prompts = True  # (default)
  2. Copy only lines with prompts, when it finds prompts:

    copybutton_only_copy_prompt_lines = True  # (default)
  3. 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/