Skip to content

Commit 6ea12bc

Browse files
committed
adding ability to customize image
1 parent 3c5af8d commit 6ea12bc

File tree

5 files changed

+11
-32
lines changed

5 files changed

+11
-32
lines changed
611 Bytes
Loading

doc/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
# copybutton_prompt_text = "... "
102102
# copybutton_only_copy_prompt_lines = False
103103
# copybutton_remove_prompts = False
104+
# copybutton_image_path = "test/TEST_COPYBUTTON.png"
104105

105106
# -- Options for HTMLHelp output ---------------------------------------------
106107

doc/index.rst

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ overwrite sphinx-copybutton's behavior.
102102
Strip and configure input prompts for code cells
103103
------------------------------------------------
104104

105-
By default, ``sphinx-copybutton`` will remove Python prompts (``>>>``) from
105+
By default, ``sphinx-copybutton`` will remove Python prompts (">>> ") from
106106
the beginning of each copied line. If it detects these prompts, then *only*
107107
the lines that contain prompts will be copied (after removing the prompt text).
108108
If no lines with prompts are found, then the full contents of the cell will be
@@ -140,8 +140,9 @@ To disable this behavior, use the following configuration in ``conf.py``:
140140
Configure whether the input prompts should be stripped
141141
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142142

143+
By default, sphinx-copybutton will remove the prompt text from lines
143144
according to the value of ``copybutton_prompt_text`` (by default,
144-
this value is ``>>>``).
145+
this value is ">>> ").
145146

146147
To disable this behavior and copy the full text of lines with prompts
147148
(for example, if you'd like to copy *only* the lines with prompts, but not
@@ -154,36 +155,11 @@ strip the prompts), use the following configuration in ``conf.py``:
154155
Use a different copy button image
155156
---------------------------------
156157

157-
To use a different image for your copy buttons, the easiest thing to do is
158-
to add a small bit of javascript to your Sphinx build that points the image
159-
to something new. Follow these steps:
158+
To use a different image for your copy buttons, do the following:
160159

161-
1. Create a new javascript file in your site's static folder
162-
(e.g., ``_static/js/custom.js``). In it, put the following code:
163-
164-
.. code-block:: javascript
165-
166-
const updateCopyButtonImages = () => {
167-
const copybuttonimages = document.querySelectorAll('a.copybtn img')
168-
copybuttonimages.forEach((img, index) => {
169-
img.setAttribute('src', 'path-to-new-image.svg')
170-
})
171-
}
172-
173-
runWhenDOMLoaded(updateCopyButtonImages)
174-
175-
176-
2. Add this javascript file to your `conf.py` configuration like so:
177-
178-
.. code-block:: python
179-
180-
def setup(app):
181-
app.add_javascript('js/custom.js');
182-
183-
This will replace the copybutton images each time the page loads!
184-
185-
**If you know of a better way to do this with sphinx, please don't hesitate to
186-
recommend something!**
160+
1. Place the image in the ``_static/`` folder of your site.
161+
2. Set the ``copybutton_image_path`` variable in your ``conf.py`` to be the
162+
path to your image file, **relative to** ``_static/``.
187163

188164
Development
189165
===========

sphinx_copybutton/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def add_to_context(app, config):
1212
config.html_context.update({'copybutton_prompt_text': config.copybutton_prompt_text})
1313
config.html_context.update({'copybutton_only_copy_prompt_lines': config.copybutton_only_copy_prompt_lines})
1414
config.html_context.update({'copybutton_remove_prompts': config.copybutton_remove_prompts})
15+
config.html_context.update({'copybutton_image_path': config.copybutton_image_path})
1516

1617
def setup(app):
1718
print('Adding copy buttons to code blocks...')
@@ -22,6 +23,7 @@ def setup(app):
2223
app.add_config_value("copybutton_prompt_text", ">>> ", "html")
2324
app.add_config_value("copybutton_only_copy_prompt_lines", True, "html")
2425
app.add_config_value("copybutton_remove_prompts", True, "html")
26+
app.add_config_value("copybutton_image_path", "copy-button.svg", "html")
2527

2628
# Add configuration value to the template
2729
app.connect("config-inited", add_to_context)

sphinx_copybutton/_static/copybutton.js_t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const addCopyButtonToCodeCells = () => {
117117

118118
const clipboardButton = id =>
119119
`<a class="copybtn o-tooltip--left" style="background-color: ${pre_bg}" data-tooltip="${messages[locale]['copy']}" data-clipboard-target="#${id}">
120-
<img src="${DOCUMENTATION_OPTIONS.URL_ROOT}_static/copy-button.svg" alt="${messages[locale]['copy_to_clipboard']}">
120+
<img src="${DOCUMENTATION_OPTIONS.URL_ROOT}_static/{{ copybutton_image_path }}" alt="${messages[locale]['copy_to_clipboard']}">
121121
</a>`
122122
codeCell.insertAdjacentHTML('afterend', clipboardButton(id))
123123
})

0 commit comments

Comments
 (0)