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 1, 2020
1 parent 105fa2b commit 5ec34d2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
2 changes: 2 additions & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
#
# html_theme_options = {}

copybutton_skip_text = ">>>"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
Expand Down
9 changes: 5 additions & 4 deletions sphinx_copybutton/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ 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, pagename, templatename, context, doctree):
context['copybutton_skip_text'] = app.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("html-page-context", 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,17 +65,7 @@ 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

textContent.forEach((line, index) => {
if (line.startsWith(copybuttonSkipText)) {
Expand Down

0 comments on commit 5ec34d2

Please sign in to comment.