@@ -13,7 +13,7 @@ Sphinx-copybutton
1313Sphinx-copybutton does one thing: add little "copy" button to the right
1414of your code blocks. That's it! It is a lightweight wrapper around the
1515excellent (and also lightweight) Javascript library
16- `ClipboardJS <https://clipboardjs.com/> `.
16+ `ClipboardJS <https://clipboardjs.com/ >`_ .
1717
1818**Here's an example **
1919
@@ -27,13 +27,20 @@ And here's a code block, note the copy button to the right!
2727 copy me!
2828
2929 By default, ``sphinx-copybutton `` will remove Python prompts from
30- each line that begins with them. For example, try copying the text
30+ each line that begins with them. If it finds lines that start with the
31+ prompt text, all *other * lines will not be copied.
32+ For example, try copying the text
3133below:
3234
3335.. code-block :: python
3436
3537 >> > a = 2
3638 >> > print (a)
39+ 2
40+
41+ >> > b = ' wow'
42+ >> > print (b)
43+ wow
3744
3845 The text that ``sphinx-copybutton `` uses can be configured as well. See
3946:ref: `configure_copy_text ` for more information.
@@ -70,8 +77,8 @@ extensions list. E.g.:
7077 ...
7178 ]
7279
73- When you build your site, your code blocks should now have little copy buttons to their
74- right. Clicking the button will copy the code inside!
80+ When you build your site, your code blocks should now have little copy buttons
81+ to their right. Clicking the button will copy the code inside!
7582
7683Customization
7784=============
@@ -92,52 +99,78 @@ overwrite sphinx-copybutton's behavior.
9299
93100.. _configure_copy_text :
94101
95- Customize the text that is removed during copying
96- -------------------------------------------------
102+ Strip and configure input prompts for code cells
103+ ------------------------------------------------
97104
98- By default, ``sphinx-copybutton `` will remove Python prompts (">>> ") from
99- the beginning of each line. To change the text that is removed (or to remove
100- no text at all), add the following configuration to your ``conf.py `` file:
105+ By default, ``sphinx-copybutton `` will copy the entire contents of a code
106+ block when the button is clicked. For many languages, it is common to
107+ include **input prompts ** with your examples, along with the outputs from
108+ running the code.
101109
102- .. code :: python
110+ ``sphinx-copybutton `` provides functionality to both
111+ strip input prompts, as well as *only * select lines that begin with a prompt.
112+ This allows users to click the button and *only * copy the input text,
113+ excluding the prompts and outputs.
103114
104- copybutton_skip_text = " sometexttoskip"
115+ To define the prompt text that you'd like removed from copied text in your code
116+ blocks, use the following configuration value in your ``conf.py `` file:
105117
106- Note that this text will only be removed from lines that * begin * with the text.
118+ .. code-block ::
107119
108- Use a different copy button image
109- ---------------------------------
120+ copybutton_prompt_text = "myinputprompt"
121+
122+ When this variable is set, ``sphinx-copybutton `` will remove the prompt from
123+ the beginning of any lines that start with the text you specify. In
124+ addition, *only * the lines that contain prompts will be copied if any are
125+ discovered. If no lines with prompts are found, then the full contents of
126+ the cell will be copied.
127+
128+ For example, to exclude traditional Python prompts from your copied code,
129+ use the following configuration:
130+
131+ .. code-block ::
132+
133+ copybutton_prompt_text = ">>> "
110134
111- To use a different image for your copy buttons, the easiest thing to do is
112- to add a small bit of javascript to your Sphinx build that points the image
113- to something new. Follow these steps:
135+ Configure whether *only * lines with prompts are copied
136+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114137
115- 1. Create a new javascript file in your site's static folder (e.g., `_static/js/custom.js `).
116- In it, put the following code:
138+ By default, if sphinx-copybutton detects lines that begin with code prompts,
139+ it will *only * copy the text in those lines (after stripping the prompts).
140+ This assumes that the rest of the code block contains outputs that shouldn't
141+ be copied.
117142
118- .. code-block :: javascript
143+ To disable this behavior, use the following configuration in `` conf.py ``:
119144
120- const updateCopyButtonImages = () => {
121- const copybuttonimages = document .querySelectorAll (' a.copybtn img' )
122- copybuttonimages .forEach ((img , index ) => {
123- img .setAttribute (' src' , ' path-to-new-image.svg' )
124- })
125- }
145+ .. code-block :: python
146+
147+ copybutton_only_copy_prompt_lines = False
126148
127- runWhenDOMLoaded (updateCopyButtonImages)
149+ In this case, all lines of the code blocks will be copied after the prompts
150+ are stripped.
128151
152+ Configure whether the input prompts should be stripped
153+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129154
130- 2. Add this javascript file to your `conf.py ` configuration like so:
155+ By default, sphinx-copybutton will remove the prompt text from lines
156+ according to the value of ``copybutton_prompt_text ``.
131157
132- .. code-block :: python
158+ To disable this behavior and copy the full text of lines with prompts
159+ (for example, if you'd like to copy *only * the lines with prompts, but not
160+ strip the prompts), use the following configuration in ``conf.py ``:
161+
162+ .. code-block :: python
133163
134- def setup (app ):
135- app.add_javascript(' js/custom.js' );
164+ copybutton_remove_prompts = False
165+
166+ Use a different copy button image
167+ ---------------------------------
136168
137- This will replace the copybutton images each time the page loads!
169+ To use a different image for your copy buttons, do the following:
138170
139- **If you know of a better way to do this with sphinx, please don't hesitate to
140- recommend something! **
171+ 1. Place the image in the ``_static/ `` folder of your site.
172+ 2. Set the ``copybutton_image_path `` variable in your ``conf.py `` to be the
173+ path to your image file, **relative to ** ``_static/ ``.
141174
142175Development
143176===========
0 commit comments