Skip to content

Commit

Permalink
directly inserting skipText into js
Browse files Browse the repository at this point in the history
  • Loading branch information
choldgraf committed Feb 2, 2020
1 parent 105fa2b commit 55a5a78
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
10 changes: 8 additions & 2 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,21 @@ Customize the text that is removed during copying
-------------------------------------------------

By default, ``sphinx-copybutton`` will remove Python prompts (">>> ") from
the beginning of each line. To change the text that is removed (or to remove
no text at all), add the following configuration to your ``conf.py`` file:
the beginning of each line. To change the text that is removed, add the
following configuration to your ``conf.py`` file:

.. code:: python
copybutton_skip_text = "sometexttoskip"
Note that this text will only be removed from lines that *begin* with the text.

To skip this behavior and remove *no* text, use an empty string:

.. code:: python
copybutton_skip_text = ""
Use a different copy button image
---------------------------------

Expand Down
10 changes: 6 additions & 4 deletions sphinx_copybutton/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ def scb_static_path(app):
static_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '_static'))
app.config.html_static_path.append(static_path)

def add_skip_text_js(app):
skip_text = app.config['copybutton_skip_text']
app.add_js_file(None, body="var copybuttonSkipText = '{}';".format(skip_text))
def add_to_context(app, config):
# Update the global context
config.html_context.update({'copybutton_skip_text': config.copybutton_skip_text})

def setup(app):
print('Adding copy buttons to code blocks...')
# Add our static path
app.connect('builder-inited', scb_static_path)
app.connect('builder-inited', add_skip_text_js)

# configuration for this tool
app.add_config_value("copybutton_skip_text", ">>> ", "html")

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

# Add relevant code to headers
app.add_css_file('copybutton.css')
app.add_js_file('clipboard.min.js')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,14 @@ const temporarilyChangeTooltip = (el, newText) => {
var copyTargetText = (trigger) => {
var target = document.querySelector(trigger.attributes['data-clipboard-target'].value);
var textContent = target.textContent.split('\n');
// Prevent breaking of the copy functionality, for themes which don't
// set copybuttonSkipText properly.
if(! ("copybuttonSkipText" in window)){
var copybuttonSkipText = ">>> ";
console.warn(`sphinx_copybutton:
The theme that was used to generate this document, does not support the setting for 'copybutton_skip_text',
which is why its default of '>>> ' was used.
Please tell the theme developers to include javascript files with the template tag 'js_tag' for sphinx>=1.8.
Example: https://github.com/readthedocs/sphinx_rtd_theme/blob/ab7d388448258a24f8f4fa96dccb69d24f571736/sphinx_rtd_theme/layout.html#L30
`);
var copybuttonSkipText = '{{ copybutton_skip_text }}' // Inserted from config
if (copybuttonSkipText.length > 0) {
textContent.forEach((line, index) => {
if (line.startsWith(copybuttonSkipText)) {
textContent[index] = line.slice(copybuttonSkipText.length)
}
});
}

textContent.forEach((line, index) => {
if (line.startsWith(copybuttonSkipText)) {
textContent[index] = line.slice(copybuttonSkipText.length)
}
});
return textContent.join('\n')
}

Expand Down

0 comments on commit 55a5a78

Please sign in to comment.