Skip to content

Commit be9a0e3

Browse files
committed
2 parents 1718310 + 9283d9c commit be9a0e3

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

doc/index.rst

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ Sphinx-copybutton
1111
:alt: PyPi page
1212

1313
Sphinx-copybutton does one thing: add little "copy" button to the right
14-
of your code blocks. That's it!
14+
of your code blocks. That's it! It is a lightweight wrapper around the
15+
excellent (and also lightweight) Javascript library
16+
`ClipboardJS <https://clipboardjs.com/>`.
1517

1618
**Here's an example**
1719

@@ -24,6 +26,18 @@ And here's a code block, note the copy button to the right!
2426
2527
copy me!
2628
29+
By default, ``sphinx-copybutton`` will remove Python prompts from
30+
each line that begins with them. For example, try copying the text
31+
below:
32+
33+
.. code-block:: python
34+
35+
>>> a = 2
36+
>>> print(a)
37+
38+
The text that ``sphinx-copybutton`` uses can be configured as well. See
39+
:ref:`configure_copy_text` for more information.
40+
2741
If the code block overlaps to the right of the text area, you can just click
2842
the button to get the whole thing.
2943

@@ -73,9 +87,24 @@ Customize the CSS
7387
To customize the display of the copy button, you can add your own CSS files
7488
that overwrite the CSS in the
7589
`sphinx-copybutton CSS rules <https://github.com/choldgraf/sphinx-copybutton/blob/master/_static/copybutton.css>`_.
76-
Just add these files to `_static` in your documentation folder, and it should
90+
Just add these files to ``_static`` in your documentation folder, and it should
7791
overwrite sphinx-copybutton's behavior.
7892

93+
.. _configure_copy_text:
94+
95+
Customize the text that is removed during copying
96+
-------------------------------------------------
97+
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:
101+
102+
.. code:: python
103+
104+
copybutton_skip_text = "sometexttoskip"
105+
106+
Note that this text will only be removed from lines that *begin* with the text.
107+
79108
Use a different copy button image
80109
---------------------------------
81110

sphinx_copybutton/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@ def scb_static_path(app):
77
static_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '_static'))
88
app.config.html_static_path.append(static_path)
99

10+
def add_skip_text_js(app):
11+
skip_text = app.config['copybutton_skip_text']
12+
app.add_js_file(None, body="var copybuttonSkipText = '{}';".format(skip_text))
13+
1014
def setup(app):
1115
print('Adding copy buttons to code blocks...')
1216
# Add our static path
1317
app.connect('builder-inited', scb_static_path)
18+
app.connect('builder-inited', add_skip_text_js)
19+
20+
# configuration for this tool
21+
app.add_config_value("copybutton_skip_text", ">>> ", "html")
1422

1523
# Add relevant code to headers
1624
app.add_stylesheet('copybutton.css')
17-
app.add_javascript('clipboard.min.js')
18-
app.add_javascript("copybutton.js")
25+
app.add_js_file('clipboard.min.js')
26+
app.add_js_file("copybutton.js")
1927
return {"version": __version__,
2028
"parallel_read_safe": True,
2129
"parallel_write_safe": True}

sphinx_copybutton/_static/copybutton.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ const temporarilyChangeTooltip = (el, newText) => {
6060
setTimeout(() => el.setAttribute('data-tooltip', oldText), 2000)
6161
}
6262

63+
// Callback when a copy button is clicked. Will be passed the node that was clicked
64+
// should then grab the text and replace pieces of text that shouldn't be used in output
65+
var copyTargetText = (trigger) => {
66+
var target = document.querySelector(trigger.attributes['data-clipboard-target'].value);
67+
var textContent = target.textContent.split('\n');
68+
textContent.forEach((line, index) => {
69+
if (line.startsWith(copybuttonSkipText)) {
70+
textContent[index] = line.slice(copybuttonSkipText.length)
71+
}
72+
});
73+
return textContent.join('\n')
74+
}
75+
6376
const addCopyButtonToCodeCells = () => {
6477
// If ClipboardJS hasn't loaded, wait a bit and try again. This
6578
// happens because we load ClipboardJS asynchronously.
@@ -68,6 +81,7 @@ const addCopyButtonToCodeCells = () => {
6881
return
6982
}
7083

84+
// Add copybuttons to all of our code cells
7185
const codeCells = document.querySelectorAll('div.highlight pre')
7286
codeCells.forEach((codeCell, index) => {
7387
const id = codeCellId(index)
@@ -81,7 +95,10 @@ const addCopyButtonToCodeCells = () => {
8195
codeCell.insertAdjacentHTML('afterend', clipboardButton(id))
8296
})
8397

84-
const clipboard = new ClipboardJS('.copybtn')
98+
// Initialize with a callback so we can modify the text before copy
99+
const clipboard = new ClipboardJS('.copybtn', {text: copyTargetText})
100+
101+
// Update UI with error/success messages
85102
clipboard.on('success', event => {
86103
clearSelection()
87104
temporarilyChangeTooltip(event.trigger, messages[locale]['copy_success'])

0 commit comments

Comments
 (0)