From 397fd8f73b3a6b493f8d9036fd2c472edc102245 Mon Sep 17 00:00:00 2001 From: Matthias Geier Date: Thu, 9 Jan 2020 21:29:25 +0100 Subject: [PATCH 01/35] Avoid a Sphinx warning: add_stylesheet -> add_css_file --- sphinx_copybutton/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index a1b0edd..baa7fde 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -21,7 +21,7 @@ def setup(app): app.add_config_value("copybutton_skip_text", ">>> ", "html") # Add relevant code to headers - app.add_stylesheet('copybutton.css') + app.add_css_file('copybutton.css') app.add_js_file('clipboard.min.js') app.add_js_file("copybutton.js") return {"version": __version__, From 3b66136f490b4d7391aba1be37faeb79283f0827 Mon Sep 17 00:00:00 2001 From: s-weigand Date: Thu, 16 Jan 2020 04:34:26 +0100 Subject: [PATCH 02/35] added .vscode to .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 97dd07a..8045a79 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,6 @@ venv.bak/ # Docs _build/ + +# VS code config +.vscode From 63203874bdda7e2a94dec64c9e06b508df4c8e1e Mon Sep 17 00:00:00 2001 From: s-weigand Date: Thu, 16 Jan 2020 04:36:01 +0100 Subject: [PATCH 03/35] set margin of 'a.copybtn > img' to prevent bad placement due to theme css rules applied on 'img' tags (themes: astropy-theme, py3doc-enhanced-theme, typlog-theme) --- sphinx_copybutton/_static/copybutton.css | 1 + 1 file changed, 1 insertion(+) diff --git a/sphinx_copybutton/_static/copybutton.css b/sphinx_copybutton/_static/copybutton.css index eb70931..7bf609e 100644 --- a/sphinx_copybutton/_static/copybutton.css +++ b/sphinx_copybutton/_static/copybutton.css @@ -15,6 +15,7 @@ div.highlight { a.copybtn > img { vertical-align: top; + margin: 0; } .highlight:hover .copybtn { From b93a9ce976820fd60cef974d7b51de70ab966667 Mon Sep 17 00:00:00 2001 From: s-weigand Date: Thu, 16 Jan 2020 04:48:17 +0100 Subject: [PATCH 04/35] Added fail save if 'copybuttonSkipText' is undefined caused copy function to break (themes: julia-theme, pytorch-theme) --- sphinx_copybutton/_static/copybutton.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sphinx_copybutton/_static/copybutton.js b/sphinx_copybutton/_static/copybutton.js index a5a316a..5f32b47 100644 --- a/sphinx_copybutton/_static/copybutton.js +++ b/sphinx_copybutton/_static/copybutton.js @@ -65,6 +65,9 @@ const temporarilyChangeTooltip = (el, newText) => { var copyTargetText = (trigger) => { var target = document.querySelector(trigger.attributes['data-clipboard-target'].value); var textContent = target.textContent.split('\n'); + if(copybuttonSkipText === undefined){ + var copybuttonSkipText = ""; + } textContent.forEach((line, index) => { if (line.startsWith(copybuttonSkipText)) { textContent[index] = line.slice(copybuttonSkipText.length) From eab13a8eee009b8938b646b7647d1e1dfed65a61 Mon Sep 17 00:00:00 2001 From: s-weigand Date: Thu, 16 Jan 2020 04:50:10 +0100 Subject: [PATCH 05/35] explicitly set 'border: none;' for 'a.copybtn' red line inside the icon with typlog-theme --- sphinx_copybutton/_static/copybutton.css | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sphinx_copybutton/_static/copybutton.css b/sphinx_copybutton/_static/copybutton.css index 7bf609e..c831990 100644 --- a/sphinx_copybutton/_static/copybutton.css +++ b/sphinx_copybutton/_static/copybutton.css @@ -6,7 +6,8 @@ a.copybtn { width: 1em; height: 1em; opacity: .3; - transition: opacity 0.5s; + transition: opacity 0.5s; + border: none; } div.highlight { From 325fd88ba613001bc6cde1ee447a1ac7bca5e4cd Mon Sep 17 00:00:00 2001 From: s-weigand Date: Thu, 16 Jan 2020 06:31:50 +0100 Subject: [PATCH 06/35] absolute position 'a.copybtn > img' theme: itcase-theme after js and css inclusion in the template is fixed --- sphinx_copybutton/_static/copybutton.css | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sphinx_copybutton/_static/copybutton.css b/sphinx_copybutton/_static/copybutton.css index c831990..f35ef3a 100644 --- a/sphinx_copybutton/_static/copybutton.css +++ b/sphinx_copybutton/_static/copybutton.css @@ -17,6 +17,9 @@ div.highlight { a.copybtn > img { vertical-align: top; margin: 0; + top: 0; + left: 0; + position: absolute; } .highlight:hover .copybtn { From 64520bf5988276f89ef867865d5b77361cbbb97e Mon Sep 17 00:00:00 2001 From: Matthias Geier Date: Fri, 17 Jan 2020 10:59:46 +0100 Subject: [PATCH 07/35] Remove unnecessary dependencies --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index 8dcf3db..128aa47 100644 --- a/setup.py +++ b/setup.py @@ -33,6 +33,5 @@ '_static/copybutton.js', '_static/copy-button.svg', '_static/clipboard.min.js']}, - install_requires=["flit", "setuptools", "wheel", "sphinx"], classifiers=["License :: OSI Approved :: MIT License"] ) From cadbc13ae7a55630609e091a521641ba731edc31 Mon Sep 17 00:00:00 2001 From: s-weigand Date: Thu, 16 Jan 2020 15:22:05 +0100 Subject: [PATCH 08/35] Refined 'copybuttonSkipText fail save' to work properly and give a warning in the console, explaining what went wrong and how to fix it --- sphinx_copybutton/_static/copybutton.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/sphinx_copybutton/_static/copybutton.js b/sphinx_copybutton/_static/copybutton.js index 5f32b47..b79df3d 100644 --- a/sphinx_copybutton/_static/copybutton.js +++ b/sphinx_copybutton/_static/copybutton.js @@ -65,9 +65,18 @@ const temporarilyChangeTooltip = (el, newText) => { var copyTargetText = (trigger) => { var target = document.querySelector(trigger.attributes['data-clipboard-target'].value); var textContent = target.textContent.split('\n'); - if(copybuttonSkipText === undefined){ - var copybuttonSkipText = ""; + // 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 + `); } + textContent.forEach((line, index) => { if (line.startsWith(copybuttonSkipText)) { textContent[index] = line.slice(copybuttonSkipText.length) From 0cef66bb9fa1359bdf1ca5ad66479690b7f83ef8 Mon Sep 17 00:00:00 2001 From: Tzach Livyatan Date: Wed, 22 Jan 2020 10:23:14 +0200 Subject: [PATCH 09/35] remove the last newline from teh coppied text --- sphinx_copybutton/_static/copybutton.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_copybutton/_static/copybutton.js b/sphinx_copybutton/_static/copybutton.js index a5a316a..b3b68b6 100644 --- a/sphinx_copybutton/_static/copybutton.js +++ b/sphinx_copybutton/_static/copybutton.js @@ -70,7 +70,7 @@ var copyTargetText = (trigger) => { textContent[index] = line.slice(copybuttonSkipText.length) } }); - return textContent.join('\n') + return textContent.slice(0, -1).join('\n') + textContent.slice(-1) } const addCopyButtonToCodeCells = () => { From 201a4838760f9aa9dd5846cdd7f235b0366c4ec8 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Sat, 1 Feb 2020 14:37:27 -0600 Subject: [PATCH 10/35] directly inserting skipText into js --- doc/index.rst | 10 ++++++-- setup.py | 5 +++- sphinx_copybutton/__init__.py | 10 ++++---- .../{copybutton.js => copybutton.js_t} | 23 ++++++------------- 4 files changed, 25 insertions(+), 23 deletions(-) rename sphinx_copybutton/_static/{copybutton.js => copybutton.js_t} (81%) diff --git a/doc/index.rst b/doc/index.rst index e43d0a6..0e840dc 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -96,8 +96,8 @@ 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 @@ -105,6 +105,12 @@ no text at all), add the following configuration to your ``conf.py`` file: 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 --------------------------------- diff --git a/setup.py b/setup.py index 128aa47..9797bc9 100644 --- a/setup.py +++ b/setup.py @@ -33,5 +33,8 @@ '_static/copybutton.js', '_static/copy-button.svg', '_static/clipboard.min.js']}, - classifiers=["License :: OSI Approved :: MIT License"] + classifiers=["License :: OSI Approved :: MIT License"], + install_requires=[ + "sphinx>=1.8" + ] ) diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index baa7fde..802ac76 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -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') diff --git a/sphinx_copybutton/_static/copybutton.js b/sphinx_copybutton/_static/copybutton.js_t similarity index 81% rename from sphinx_copybutton/_static/copybutton.js rename to sphinx_copybutton/_static/copybutton.js_t index b79df3d..4e9baef 100644 --- a/sphinx_copybutton/_static/copybutton.js +++ b/sphinx_copybutton/_static/copybutton.js_t @@ -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') } From 1f2afc3c5b43d34ae871c41c5346fafbdb1c26c1 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Mon, 3 Feb 2020 08:54:12 -0600 Subject: [PATCH 11/35] adding ability to only lines with prompts --- doc/conf.py | 4 +++ doc/index.rst | 44 ++++++++++++++++++----- sphinx_copybutton/__init__.py | 8 +++-- sphinx_copybutton/_static/copybutton.js_t | 31 ++++++++++++---- 4 files changed, 70 insertions(+), 17 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index 75d20c3..9051307 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -97,6 +97,10 @@ # # html_sidebars = {} +# CopyButton configuration +# copybutton_prompt_text = "... " +# copybutton_only_copy_prompt_lines = False +# copybutton_remove_prompts = False # -- Options for HTMLHelp output --------------------------------------------- diff --git a/doc/index.rst b/doc/index.rst index 0e840dc..da21707 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -13,7 +13,7 @@ Sphinx-copybutton Sphinx-copybutton does one thing: add little "copy" button to the right of your code blocks. That's it! It is a lightweight wrapper around the excellent (and also lightweight) Javascript library -`ClipboardJS `. +`ClipboardJS `_. **Here's an example** @@ -27,13 +27,20 @@ And here's a code block, note the copy button to the right! copy me! By default, ``sphinx-copybutton`` will remove Python prompts from -each line that begins with them. For example, try copying the text +each line that begins with them. If it finds lines that start with the +prompt text, all *other* lines will not be copied. +For example, try copying the text below: .. code-block:: python >>> a = 2 >>> print(a) + 2 + + >>> b = 'wow' + >>> print(b) + wow The text that ``sphinx-copybutton`` uses can be configured as well. See :ref:`configure_copy_text` for more information. @@ -70,8 +77,8 @@ extensions list. E.g.: ... ] -When you build your site, your code blocks should now have little copy buttons to their -right. Clicking the button will copy the code inside! +When you build your site, your code blocks should now have little copy buttons +to their right. Clicking the button will copy the code inside! Customization ============= @@ -92,16 +99,21 @@ overwrite sphinx-copybutton's behavior. .. _configure_copy_text: -Customize the text that is removed during copying -------------------------------------------------- +Strip and configure input prompts for code cells +------------------------------------------------ By default, ``sphinx-copybutton`` will remove Python prompts (">>> ") from -the beginning of each line. To change the text that is removed, add the +the beginning of each copied line. If it detects these prompts, then *only* +the lines that contain prompts will be copied (after removing the prompt text). +If no lines with prompts are found, then the full contents of the cell will be +copied. + +To change the text that is removed, add the following configuration to your ``conf.py`` file: .. code:: python - copybutton_skip_text = "sometexttoskip" + copybutton_prompt_text = "sometexttoskip" Note that this text will only be removed from lines that *begin* with the text. @@ -109,7 +121,21 @@ To skip this behavior and remove *no* text, use an empty string: .. code:: python - copybutton_skip_text = "" + copybutton_prompt_text = "" + +Configure whether *only* lines with prompts are copied +------------------------------------------------------ + +By default, if sphinx-copybutton detects lines that begin with code prompts, +it will *only* copy the text in those lines (after stripping the prompts). +This assumes that the rest of the code block contains outputs that shouldn't +be copied. + +To disable this behavior, use the following configuration in ``conf.py``: + +.. code:: python + + copybutton_only_copy_prompt_lines = False Use a different copy button image --------------------------------- diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index 802ac76..f968ecb 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -9,7 +9,9 @@ def scb_static_path(app): def add_to_context(app, config): # Update the global context - config.html_context.update({'copybutton_skip_text': config.copybutton_skip_text}) + config.html_context.update({'copybutton_prompt_text': config.copybutton_prompt_text}) + config.html_context.update({'copybutton_only_copy_prompt_lines': config.copybutton_only_copy_prompt_lines}) + config.html_context.update({'copybutton_remove_prompts': config.copybutton_remove_prompts}) def setup(app): print('Adding copy buttons to code blocks...') @@ -17,7 +19,9 @@ def setup(app): app.connect('builder-inited', scb_static_path) # configuration for this tool - app.add_config_value("copybutton_skip_text", ">>> ", "html") + app.add_config_value("copybutton_prompt_text", ">>> ", "html") + app.add_config_value("copybutton_only_copy_prompt_lines", True, "html") + app.add_config_value("copybutton_remove_prompts", True, "html") # Add configuration value to the template app.connect("config-inited", add_to_context) diff --git a/sphinx_copybutton/_static/copybutton.js_t b/sphinx_copybutton/_static/copybutton.js_t index 4e9baef..69a83f9 100644 --- a/sphinx_copybutton/_static/copybutton.js_t +++ b/sphinx_copybutton/_static/copybutton.js_t @@ -65,13 +65,32 @@ const temporarilyChangeTooltip = (el, newText) => { var copyTargetText = (trigger) => { var target = document.querySelector(trigger.attributes['data-clipboard-target'].value); var textContent = target.textContent.split('\n'); - 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) + var copybuttonPromptText = '{{ copybutton_prompt_text }}'; // Inserted from config + var onlyCopyPromptLines = {{ copybutton_only_copy_prompt_lines | lower }}; // Inserted from config + var removePrompts = {{ copybutton_remove_prompts | lower }}; // Inserted from config + + // Text content line filtering based on prompts (if a prompt text is given) + if (copybuttonPromptText.length > 0) { + // If only copying prompt lines, remove all lines that don't start w/ prompt + if (onlyCopyPromptLines) { + linesWithPrompt = textContent.filter((line) => { + return line.startsWith(copybuttonPromptText) || (line.length == 0); // Keep newlines + }); + // Check to make sure we have at least one non-empty line + var nonEmptyLines = linesWithPrompt.filter((line) => {return line.length > 0}); + // If we detected lines w/ prompt, then overwrite textContent w/ those lines + if ((linesWithPrompt.length > 0) && (nonEmptyLines.length > 0)) { + textContent = linesWithPrompt; } - }); + } + // Remove the starting prompt from any remaining lines + if (removePrompts) { + textContent.forEach((line, index) => { + if (line.startsWith(copybuttonPromptText)) { + textContent[index] = line.slice(copybuttonPromptText.length); + } + }); + } } return textContent.join('\n') } From 53dab72efbc305e8f133f4dc349bde5e4f9530c5 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Mon, 3 Feb 2020 09:00:45 -0600 Subject: [PATCH 12/35] fixing newline code --- sphinx_copybutton/_static/copybutton.js_t | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sphinx_copybutton/_static/copybutton.js_t b/sphinx_copybutton/_static/copybutton.js_t index 307c8e1..f9587d7 100644 --- a/sphinx_copybutton/_static/copybutton.js_t +++ b/sphinx_copybutton/_static/copybutton.js_t @@ -92,7 +92,12 @@ var copyTargetText = (trigger) => { }); } } - return textContent.slice(0, -1).join('\n') + textContent.slice(-1) + textContent = textContent.join('\n'); + // Remove a trailing newline to avoid auto-running when pasting + if (textContent.endsWith("\n")) { + textContent = textContent.slice(0, -1) + } + return textContent } const addCopyButtonToCodeCells = () => { From 3c5af8d1f04e64ebae13fa672157877a22130ec4 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Mon, 3 Feb 2020 08:27:45 -0700 Subject: [PATCH 13/35] docs --- doc/index.rst | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index da21707..08c8c2c 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -102,7 +102,7 @@ overwrite sphinx-copybutton's behavior. Strip and configure input prompts for code cells ------------------------------------------------ -By default, ``sphinx-copybutton`` will remove Python prompts (">>> ") from +By default, ``sphinx-copybutton`` will remove Python prompts (``>>>``) from the beginning of each copied line. If it detects these prompts, then *only* the lines that contain prompts will be copied (after removing the prompt text). If no lines with prompts are found, then the full contents of the cell will be @@ -111,7 +111,7 @@ copied. To change the text that is removed, add the following configuration to your ``conf.py`` file: -.. code:: python +.. code-block:: python copybutton_prompt_text = "sometexttoskip" @@ -119,12 +119,12 @@ 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 +.. code-block:: python copybutton_prompt_text = "" Configure whether *only* lines with prompts are copied ------------------------------------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ By default, if sphinx-copybutton detects lines that begin with code prompts, it will *only* copy the text in those lines (after stripping the prompts). @@ -133,10 +133,24 @@ be copied. To disable this behavior, use the following configuration in ``conf.py``: -.. code:: python +.. code-block:: python copybutton_only_copy_prompt_lines = False +Configure whether the input prompts should be stripped +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +according to the value of ``copybutton_prompt_text`` (by default, +this value is ``>>>``). + +To disable this behavior and copy the full text of lines with prompts +(for example, if you'd like to copy *only* the lines with prompts, but not +strip the prompts), use the following configuration in ``conf.py``: + +.. code-block:: python + + copybutton_remove_prompts = False + Use a different copy button image --------------------------------- @@ -144,8 +158,8 @@ To use a different image for your copy buttons, the easiest thing to do is to add a small bit of javascript to your Sphinx build that points the image to something new. Follow these steps: -1. Create a new javascript file in your site's static folder (e.g., `_static/js/custom.js`). - In it, put the following code: +1. Create a new javascript file in your site's static folder + (e.g., ``_static/js/custom.js``). In it, put the following code: .. code-block:: javascript From 6ea12bcae2cfdfb8d7204749684809f754ae8f3c Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Mon, 3 Feb 2020 08:51:53 -0700 Subject: [PATCH 14/35] adding ability to customize image --- doc/_static/test/TEST_COPYBUTTON.png | Bin 0 -> 611 bytes doc/conf.py | 1 + doc/index.rst | 38 ++++------------------ sphinx_copybutton/__init__.py | 2 ++ sphinx_copybutton/_static/copybutton.js_t | 2 +- 5 files changed, 11 insertions(+), 32 deletions(-) create mode 100644 doc/_static/test/TEST_COPYBUTTON.png diff --git a/doc/_static/test/TEST_COPYBUTTON.png b/doc/_static/test/TEST_COPYBUTTON.png new file mode 100644 index 0000000000000000000000000000000000000000..27873932770653045a5e7a180f00e1bc45eb4339 GIT binary patch literal 611 zcmV-p0-XJcP)pur00001b5ch_0Itp) z=>Px#1ZP1_K>z@;j|==^1poj532;bRa{vGqB>(^xB>_oNB=7(L0r*KoK~z{r?U+rg z0Z|yo$7@91^Abg5~#3^?I^07!0u6?bx=$VP>-#B9RD;Mx(5FJRa2R zb+)Z=nCWy1zuymrVPwT>wW3z5v2BI(8B!XJ20ot;;c!?Kfk1%Pl0?ekaG+c+qu=j~ zqFSxO>2yjX<#xN#>2%oP*E5++;PrYHsbBLEQf9LmrBVsQ;gFZ*a*6Bp`aUU@N(GC> z0+-9hOFEs#@pycn)HB56aYc%Q+{PRCV{ex-nM}y%^Jur*w0^7ALNb~BvFg8F>V8SM zTrS9DGSF(Z+#E%Uph)pWk$N_gO4)2S6pKYP8VylYDizr6cCM}{sTAjnAI;@swOZkDIIv2;NU?JSlgs6x*Xy~9qtPh#`#n4QS`?{gBe@hgl9M-yY}j6< z*=!<_NIc%;v3&(|FGYSGSw@JQ7|5~fR#UW;TztM%p-`az_~a{!oTO5z=cP8A4gD@U x8jWOyEU@(0&i4IRp;FuJ7Ts=_zUF^V4PX6~q*J)9!gT-u002ovPDHLkV1jx_Bh~-_ literal 0 HcmV?d00001 diff --git a/doc/conf.py b/doc/conf.py index 9051307..ddd1ba9 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -101,6 +101,7 @@ # copybutton_prompt_text = "... " # copybutton_only_copy_prompt_lines = False # copybutton_remove_prompts = False +# copybutton_image_path = "test/TEST_COPYBUTTON.png" # -- Options for HTMLHelp output --------------------------------------------- diff --git a/doc/index.rst b/doc/index.rst index 08c8c2c..e56ff1c 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -102,7 +102,7 @@ overwrite sphinx-copybutton's behavior. Strip and configure input prompts for code cells ------------------------------------------------ -By default, ``sphinx-copybutton`` will remove Python prompts (``>>>``) from +By default, ``sphinx-copybutton`` will remove Python prompts (">>> ") from the beginning of each copied line. If it detects these prompts, then *only* the lines that contain prompts will be copied (after removing the prompt text). 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``: Configure whether the input prompts should be stripped ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +By default, sphinx-copybutton will remove the prompt text from lines according to the value of ``copybutton_prompt_text`` (by default, -this value is ``>>>``). +this value is ">>> "). To disable this behavior and copy the full text of lines with prompts (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``: Use a different copy button image --------------------------------- -To use a different image for your copy buttons, the easiest thing to do is -to add a small bit of javascript to your Sphinx build that points the image -to something new. Follow these steps: +To use a different image for your copy buttons, do the following: -1. Create a new javascript file in your site's static folder - (e.g., ``_static/js/custom.js``). In it, put the following code: - - .. code-block:: javascript - - const updateCopyButtonImages = () => { - const copybuttonimages = document.querySelectorAll('a.copybtn img') - copybuttonimages.forEach((img, index) => { - img.setAttribute('src', 'path-to-new-image.svg') - }) - } - - runWhenDOMLoaded(updateCopyButtonImages) - - -2. Add this javascript file to your `conf.py` configuration like so: - - .. code-block:: python - - def setup(app): - app.add_javascript('js/custom.js'); - -This will replace the copybutton images each time the page loads! - -**If you know of a better way to do this with sphinx, please don't hesitate to -recommend something!** +1. Place the image in the ``_static/`` folder of your site. +2. Set the ``copybutton_image_path`` variable in your ``conf.py`` to be the + path to your image file, **relative to** ``_static/``. Development =========== diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index f968ecb..d1f05ce 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -12,6 +12,7 @@ def add_to_context(app, config): config.html_context.update({'copybutton_prompt_text': config.copybutton_prompt_text}) config.html_context.update({'copybutton_only_copy_prompt_lines': config.copybutton_only_copy_prompt_lines}) config.html_context.update({'copybutton_remove_prompts': config.copybutton_remove_prompts}) + config.html_context.update({'copybutton_image_path': config.copybutton_image_path}) def setup(app): print('Adding copy buttons to code blocks...') @@ -22,6 +23,7 @@ def setup(app): app.add_config_value("copybutton_prompt_text", ">>> ", "html") app.add_config_value("copybutton_only_copy_prompt_lines", True, "html") app.add_config_value("copybutton_remove_prompts", True, "html") + app.add_config_value("copybutton_image_path", "copy-button.svg", "html") # Add configuration value to the template app.connect("config-inited", add_to_context) diff --git a/sphinx_copybutton/_static/copybutton.js_t b/sphinx_copybutton/_static/copybutton.js_t index f9587d7..e91c792 100644 --- a/sphinx_copybutton/_static/copybutton.js_t +++ b/sphinx_copybutton/_static/copybutton.js_t @@ -117,7 +117,7 @@ const addCopyButtonToCodeCells = () => { const clipboardButton = id => ` - ${messages[locale]['copy_to_clipboard']} + ${messages[locale]['copy_to_clipboard']} ` codeCell.insertAdjacentHTML('afterend', clipboardButton(id)) }) From 241fe970d49342600c2daf19d95b97492dc50e53 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Mon, 3 Feb 2020 10:39:32 -0700 Subject: [PATCH 15/35] making no prompt text the default --- doc/conf.py | 2 +- doc/index.rst | 41 ++++++++++++++++++++++------------- sphinx_copybutton/__init__.py | 2 +- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index ddd1ba9..ba87009 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -98,7 +98,7 @@ # html_sidebars = {} # CopyButton configuration -# copybutton_prompt_text = "... " +# copybutton_prompt_text = ">>> " # copybutton_only_copy_prompt_lines = False # copybutton_remove_prompts = False # copybutton_image_path = "test/TEST_COPYBUTTON.png" diff --git a/doc/index.rst b/doc/index.rst index e56ff1c..263750f 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -102,26 +102,35 @@ overwrite sphinx-copybutton's behavior. Strip and configure input prompts for code cells ------------------------------------------------ -By default, ``sphinx-copybutton`` will remove Python prompts (">>> ") from -the beginning of each copied line. If it detects these prompts, then *only* -the lines that contain prompts will be copied (after removing the prompt text). -If no lines with prompts are found, then the full contents of the cell will be -copied. +By default, ``sphinx-copybutton`` will copy the entire contents of a code +block when the button is clicked. For many languages, it is common to +include **input prompts** with your examples, along with the outputs from +running the code. -To change the text that is removed, add the -following configuration to your ``conf.py`` file: +``sphinx-copybutton`` provides functionality to both +strip input prompts, as well as *only* select lines that begin with a prompt. +This allows users to click the button and *only* copy the input text, +excluding the prompts and outputs. -.. code-block:: python +To define the prompt text that you'd like removed from copied text in your code +blocks, use the following configuration value in your ``conf.py`` file: - copybutton_prompt_text = "sometexttoskip" +.. code-block:: -Note that this text will only be removed from lines that *begin* with the text. + copybutton_prompt_text = "myinputprompt" -To skip this behavior and remove *no* text, use an empty string: +When this variable is set, ``sphinx-copybutton`` will remove the prompt from +the beginning of any lines that start with the text you specify. In +addition, *only* the lines that contain prompts will be copied if any are +discovered. If no lines with prompts are found, then the full contents of +the cell will be copied. -.. code-block:: python +For example, to exclude traditional Python prompts from your copied code, +use the following configuration: - copybutton_prompt_text = "" +.. code-block:: + + copybutton_prompt_text = ">>> " Configure whether *only* lines with prompts are copied ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -137,12 +146,14 @@ To disable this behavior, use the following configuration in ``conf.py``: copybutton_only_copy_prompt_lines = False +In this case, all lines of the code blocks will be copied after the prompts +are stripped. + Configure whether the input prompts should be stripped ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ By default, sphinx-copybutton will remove the prompt text from lines -according to the value of ``copybutton_prompt_text`` (by default, -this value is ">>> "). +according to the value of ``copybutton_prompt_text``. To disable this behavior and copy the full text of lines with prompts (for example, if you'd like to copy *only* the lines with prompts, but not diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index d1f05ce..168aabc 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -20,7 +20,7 @@ def setup(app): app.connect('builder-inited', scb_static_path) # configuration for this tool - app.add_config_value("copybutton_prompt_text", ">>> ", "html") + app.add_config_value("copybutton_prompt_text", "", "html") app.add_config_value("copybutton_only_copy_prompt_lines", True, "html") app.add_config_value("copybutton_remove_prompts", True, "html") app.add_config_value("copybutton_image_path", "copy-button.svg", "html") From f4b031b6a7e305abbc030ffbf0955ee9d32a3dfb Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Thu, 6 Feb 2020 10:35:26 +1100 Subject: [PATCH 16/35] sphinx version update in rtd --- doc/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/requirements.txt b/doc/requirements.txt index 3390fa4..a1b2fc9 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -1,4 +1,4 @@ -sphinx +sphinx>=2 # Install ourselves . \ No newline at end of file From d15cc716c979de7e5944c70760196a0e0b9835e1 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Fri, 7 Feb 2020 07:59:55 +1100 Subject: [PATCH 17/35] adding template file to copybutton install --- doc/requirements.txt | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/requirements.txt b/doc/requirements.txt index a1b2fc9..3390fa4 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -1,4 +1,4 @@ -sphinx>=2 +sphinx # Install ourselves . \ No newline at end of file diff --git a/setup.py b/setup.py index 9797bc9..1cedf52 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ license='MIT License', packages=find_packages(), package_data={'sphinx_copybutton': ['_static/copybutton.css', - '_static/copybutton.js', + '_static/copybutton.js_t', '_static/copy-button.svg', '_static/clipboard.min.js']}, classifiers=["License :: OSI Approved :: MIT License"], From 8db141036aa067ac9a047473b3bbe7bc0f3e7ead Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Fri, 7 Feb 2020 08:07:36 +1100 Subject: [PATCH 18/35] documentation improvements for copying functionality --- doc/conf.py | 3 ++- doc/index.rst | 29 ++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index ba87009..9e7ee1d 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -98,7 +98,8 @@ # html_sidebars = {} # CopyButton configuration -# copybutton_prompt_text = ">>> " +copybutton_prompt_text = ">>> " +# Switches for testing but shouldn't be activated in the live docs # copybutton_only_copy_prompt_lines = False # copybutton_remove_prompts = False # copybutton_image_path = "test/TEST_COPYBUTTON.png" diff --git a/doc/index.rst b/doc/index.rst index 263750f..68f2ffb 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -26,11 +26,20 @@ And here's a code block, note the copy button to the right! copy me! -By default, ``sphinx-copybutton`` will remove Python prompts from -each line that begins with them. If it finds lines that start with the -prompt text, all *other* lines will not be copied. -For example, try copying the text -below: + +If the code block overlaps to the right of the text area, you can just click +the button to get the whole thing. + +.. code-block:: bash + + 123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789 + +You can configure ``sphinx-copybutton`` to detect *input prompts* in code +cells, and then both remove these prompts before copying, as well as skip +lines that *don't* start with prompts (in case they are output lines). + +For example, this site has been configured to strip Python prompts (">>> "). +Try copy-pasting the code block below. .. code-block:: python @@ -42,16 +51,6 @@ below: >>> print(b) wow -The text that ``sphinx-copybutton`` uses can be configured as well. See -:ref:`configure_copy_text` for more information. - -If the code block overlaps to the right of the text area, you can just click -the button to get the whole thing. - -.. code-block:: bash - - 123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789 - Installation ============ From e7b70fb95eb3d441df8c5c9bbf6ed62ff6bdda1a Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Fri, 7 Feb 2020 08:11:29 +1100 Subject: [PATCH 19/35] adding python ocde blocks to docs --- doc/index.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index 68f2ffb..8acb367 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -114,7 +114,7 @@ excluding the prompts and outputs. To define the prompt text that you'd like removed from copied text in your code blocks, use the following configuration value in your ``conf.py`` file: -.. code-block:: +.. code-block:: python copybutton_prompt_text = "myinputprompt" @@ -127,7 +127,7 @@ the cell will be copied. For example, to exclude traditional Python prompts from your copied code, use the following configuration: -.. code-block:: +.. code-block:: python copybutton_prompt_text = ">>> " From b8f785fc4f524fdbc66517faca93da322b59cbda Mon Sep 17 00:00:00 2001 From: Iwan Date: Wed, 26 Feb 2020 11:46:52 +0100 Subject: [PATCH 20/35] Use innerText instead of textContent To avoid copying styles and scripts, this makes it work with the sphinx-prompt extension. https://www.w3schools.com/jsref/prop_node_innertext.asp I hacked it like this in my conf.py file for now: ``` copybutton_prompt_text = """>>> '; textContent = target.innerText.split('\\n'); var dummy = ' """ ``` --- sphinx_copybutton/_static/copybutton.js_t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_copybutton/_static/copybutton.js_t b/sphinx_copybutton/_static/copybutton.js_t index e91c792..522ce50 100644 --- a/sphinx_copybutton/_static/copybutton.js_t +++ b/sphinx_copybutton/_static/copybutton.js_t @@ -64,7 +64,7 @@ const temporarilyChangeTooltip = (el, newText) => { // should then grab the text and replace pieces of text that shouldn't be used in output var copyTargetText = (trigger) => { var target = document.querySelector(trigger.attributes['data-clipboard-target'].value); - var textContent = target.textContent.split('\n'); + var textContent = target.innerText.split('\n'); var copybuttonPromptText = '{{ copybutton_prompt_text }}'; // Inserted from config var onlyCopyPromptLines = {{ copybutton_only_copy_prompt_lines | lower }}; // Inserted from config var removePrompts = {{ copybutton_remove_prompts | lower }}; // Inserted from config From 7eb3a7b315af55a47ded36a219a3a1096e956616 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Thu, 12 Mar 2020 20:29:34 -0700 Subject: [PATCH 21/35] adding selector option --- doc/conf.py | 2 ++ doc/index.rst | 23 ++++++++++++++++++++++- sphinx_copybutton/__init__.py | 2 ++ sphinx_copybutton/_static/copybutton.js_t | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index 9e7ee1d..1cafea7 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -103,6 +103,8 @@ # copybutton_only_copy_prompt_lines = False # copybutton_remove_prompts = False # copybutton_image_path = "test/TEST_COPYBUTTON.png" +# copybutton_selector = "div" + # -- Options for HTMLHelp output --------------------------------------------- diff --git a/doc/index.rst b/doc/index.rst index 8acb367..3ead497 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -163,7 +163,7 @@ strip the prompts), use the following configuration in ``conf.py``: copybutton_remove_prompts = False Use a different copy button image ---------------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ To use a different image for your copy buttons, do the following: @@ -171,6 +171,27 @@ To use a different image for your copy buttons, do the following: 2. Set the ``copybutton_image_path`` variable in your ``conf.py`` to be the path to your image file, **relative to** ``_static/``. + +Configure the CSS selector used to add copy buttons +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By default, ``sphinx-copybutton`` will add a copy button to all elements +that match the following selection: + +.. code-block:: css + + div.highlight pre + +To change this selector, use the following configuration in ``conf.py``: + +.. code-block:: python + + copybutton_selector = "your.selector" + +In this case, all elements that match ``your.selector`` will have a copy button +added to them. + + Development =========== diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index 168aabc..cd8b694 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -13,6 +13,7 @@ def add_to_context(app, config): config.html_context.update({'copybutton_only_copy_prompt_lines': config.copybutton_only_copy_prompt_lines}) config.html_context.update({'copybutton_remove_prompts': config.copybutton_remove_prompts}) config.html_context.update({'copybutton_image_path': config.copybutton_image_path}) + config.html_context.update({'copybutton_selector': config.copybutton_selector}) def setup(app): print('Adding copy buttons to code blocks...') @@ -24,6 +25,7 @@ def setup(app): app.add_config_value("copybutton_only_copy_prompt_lines", True, "html") app.add_config_value("copybutton_remove_prompts", True, "html") app.add_config_value("copybutton_image_path", "copy-button.svg", "html") + app.add_config_value("copybutton_selector", "div.highlight pre", "html") # Add configuration value to the template app.connect("config-inited", add_to_context) diff --git a/sphinx_copybutton/_static/copybutton.js_t b/sphinx_copybutton/_static/copybutton.js_t index 522ce50..1c1757d 100644 --- a/sphinx_copybutton/_static/copybutton.js_t +++ b/sphinx_copybutton/_static/copybutton.js_t @@ -109,7 +109,7 @@ const addCopyButtonToCodeCells = () => { } // Add copybuttons to all of our code cells - const codeCells = document.querySelectorAll('div.highlight pre') + const codeCells = document.querySelectorAll('{{ copybutton_selector }}') codeCells.forEach((codeCell, index) => { const id = codeCellId(index) codeCell.setAttribute('id', id) From 7c21e33d2c2c13f634987150199e367c813ceb0d Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Thu, 12 Mar 2020 20:44:14 -0700 Subject: [PATCH 22/35] version bump --- sphinx_copybutton/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index cd8b694..12948c8 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -1,7 +1,7 @@ """A small sphinx extension to add "copy" buttons to code blocks.""" import os -__version__ = "0.2.9dev0" +__version__ = "0.2.9" def scb_static_path(app): static_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '_static')) From d0f834ca0e2998cc11bc7e7b0486b7a0a29a16bd Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Thu, 12 Mar 2020 20:45:59 -0700 Subject: [PATCH 23/35] version dev bump --- sphinx_copybutton/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index 12948c8..d046d23 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -1,7 +1,7 @@ """A small sphinx extension to add "copy" buttons to code blocks.""" import os -__version__ = "0.2.9" +__version__ = "0.2.10dev0" def scb_static_path(app): static_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '_static')) From 9e78f8ea2f191c80d9d4ff34bbf4fe3857613050 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Fri, 13 Mar 2020 08:17:57 -0700 Subject: [PATCH 24/35] version bump --- MANIFEST.in | 1 + sphinx_copybutton/__init__.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index 443bb8b..e5583af 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,2 @@ graft doc/ +prune doc/_build diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index d046d23..a0e51ee 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -1,7 +1,7 @@ """A small sphinx extension to add "copy" buttons to code blocks.""" import os -__version__ = "0.2.10dev0" +__version__ = "0.2.10" def scb_static_path(app): static_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '_static')) From e4fe1fe02a16efaf0f088837a21063d04d1ec443 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Fri, 13 Mar 2020 08:21:59 -0700 Subject: [PATCH 25/35] version bump --- sphinx_copybutton/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index a0e51ee..6de1fd9 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -1,7 +1,7 @@ """A small sphinx extension to add "copy" buttons to code blocks.""" import os -__version__ = "0.2.10" +__version__ = "0.2.11dev0" def scb_static_path(app): static_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '_static')) From 5084d2b0358b6d2afe2b9d7ce5e549c35309bd09 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Wed, 1 Apr 2020 16:00:58 -0700 Subject: [PATCH 26/35] update docs and log becomes verbose --- doc/index.rst | 2 +- sphinx_copybutton/__init__.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index 3ead497..36aa7a8 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -10,7 +10,7 @@ Sphinx-copybutton :target: https://pypi.org/project/sphinx_copybutton :alt: PyPi page -Sphinx-copybutton does one thing: add little "copy" button to the right +Sphinx-copybutton does one thing: add a little "copy" button to the right of your code blocks. That's it! It is a lightweight wrapper around the excellent (and also lightweight) Javascript library `ClipboardJS `_. diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index 6de1fd9..53a7fd6 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -1,8 +1,11 @@ """A small sphinx extension to add "copy" buttons to code blocks.""" import os +from sphinx.util import logging __version__ = "0.2.11dev0" +logger = logging.getLogger(__name__) + 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) @@ -16,7 +19,7 @@ def add_to_context(app, config): config.html_context.update({'copybutton_selector': config.copybutton_selector}) def setup(app): - print('Adding copy buttons to code blocks...') + logger.verbose('Adding copy buttons to code blocks...') # Add our static path app.connect('builder-inited', scb_static_path) From f81650245a19742e4151c043d2c106242f7bd771 Mon Sep 17 00:00:00 2001 From: Matthias Geier Date: Sat, 18 Apr 2020 12:42:53 +0200 Subject: [PATCH 27/35] Make button un-selectable --- sphinx_copybutton/_static/copybutton.css | 1 + 1 file changed, 1 insertion(+) diff --git a/sphinx_copybutton/_static/copybutton.css b/sphinx_copybutton/_static/copybutton.css index f35ef3a..75b17a8 100644 --- a/sphinx_copybutton/_static/copybutton.css +++ b/sphinx_copybutton/_static/copybutton.css @@ -8,6 +8,7 @@ a.copybtn { opacity: .3; transition: opacity 0.5s; border: none; + user-select: none; } div.highlight { From 940bc8430eaa96e42698337d23cd77d3d48f6e88 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Mon, 20 Apr 2020 10:14:28 -0700 Subject: [PATCH 28/35] fixing setup import --- setup.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 1cedf52..91056c7 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,13 @@ import os +from pathlib import Path from setuptools import setup, find_packages -from sphinx_copybutton import __version__ + +lines = Path(__file__).parent.joinpath("sphinx_copybutton", "__init__.py").read_text() +for line in lines.split('\n'): + if line.startswith("__version__"): + break +version = line.split(" = ")[-1].strip('"') if (os.path.isdir('clipboard.js') and not os.path.islink('sphinx_copybutton/_static/clipboard.min.js')): @@ -20,7 +26,7 @@ setup( name='sphinx-copybutton', - version=__version__, + version=version, description="Add a copy button to each of your code cells.", long_description=readme_text, long_description_content_type='text/markdown', From eac11ccd122dfad4436923c049a6ad06502dea15 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Tue, 21 Apr 2020 15:42:22 -0700 Subject: [PATCH 29/35] sphinx copybutton info --- RELEASES.md | 6 +++--- doc/index.rst | 6 +++--- setup.py | 17 +++++++++++------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index a978c2f..9242cbb 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -11,19 +11,19 @@ To create a new release of Sphinx-Copybutton, you need to do these things: ## To create the release -To create a new release, [open an issue](https://github.com/choldgraf/sphinx-copybutton/issues/new) to keep +To create a new release, [open an issue](https://github.com/ExecutableBookProject/sphinx-copybutton/issues/new) to keep track of the to-do list for the release. Copy/paste the following markdown into the issue and check off the boxes as you complete items: ``` -- [ ] Ensure that the [Sphinx-Copybutton version number](https://github.com/choldgraf/sphinx-copybutton/blob/master/jupyter_book/__init__.py) +- [ ] Ensure that the [Sphinx-Copybutton version number](https://github.com/ExecutableBookProject/sphinx-copybutton/blob/master/jupyter_book/__init__.py) is correct, and remove the `dev0` part of the version number. Make a PR with the new number and merge into master. - [ ] Create a new distribution for Sphinx-Copybutton by [following the twine release instructions](https://twine.readthedocs.io/en/latest/#using-twine) - [ ] Confirm that the new version of Sphinx-Copybutton [is posted to pypi](https://pypi.org/project/sphinx-copybutton/) -- [ ] Bump the [Sphinx-Copybutton version number](https://github.com/choldgraf/sphinx-copybutton/blob/master/jupyter_book/__init__.py) to +- [ ] Bump the [Sphinx-Copybutton version number](https://github.com/ExecutableBookProject/sphinx-copybutton/blob/master/jupyter_book/__init__.py) to the next minor (or major) release and append `dev0` to the end. - [ ] Celebrate! You've just released a new version of Sphinx-Copybutton! ``` diff --git a/doc/index.rst b/doc/index.rst index 36aa7a8..0089596 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -60,7 +60,7 @@ You can install ``sphinx-copybutton`` with ``pip``: pip install sphinx-copybutton -`Here's a link to the sphinx-copybutton GitHub repository `_. +`Here's a link to the sphinx-copybutton GitHub repository `_. Usage ===== @@ -92,7 +92,7 @@ Customize the CSS To customize the display of the copy button, you can add your own CSS files that overwrite the CSS in the -`sphinx-copybutton CSS rules `_. +`sphinx-copybutton CSS rules `_. Just add these files to ``_static`` in your documentation folder, and it should overwrite sphinx-copybutton's behavior. @@ -198,7 +198,7 @@ Development If you'd like to develop or make contributions for sphinx-copybutton, fork the repository here: -https://github.com/choldgraf/sphinx-copybutton +https://github.com/ExecutableBookProject/sphinx-copybutton pull to your computer and install locally with ``pip``:: diff --git a/setup.py b/setup.py index 1cedf52..82a708f 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,6 @@ import os - +from pathlib import Path from setuptools import setup, find_packages -from sphinx_copybutton import __version__ if (os.path.isdir('clipboard.js') and not os.path.islink('sphinx_copybutton/_static/clipboard.min.js')): @@ -18,15 +17,21 @@ with open('./README.md', 'r') as ff: readme_text = ff.read() +# Parse version +init = Path(__file__).parent.joinpath("sphinx_copybutton", "__init__.py") +for line in init.read_text().split("\n"): + if line.startswith("__version__ ="): + break +version = line.split(" = ")[-1].strip('"') + setup( name='sphinx-copybutton', - version=__version__, + version=version, description="Add a copy button to each of your code cells.", long_description=readme_text, long_description_content_type='text/markdown', - author='Chris Holdgraf', - author_email='choldgraf@berkeley.edu', - url="https://github.com/choldgraf/sphinx-copybutton", + author='Executable Book Project', + url="https://github.com/ExecutableBookProject/sphinx-copybutton", license='MIT License', packages=find_packages(), package_data={'sphinx_copybutton': ['_static/copybutton.css', From bff4ba6d2b4de7e87c7020b1caebb2bae91707ba Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Tue, 21 Apr 2020 15:50:26 -0700 Subject: [PATCH 30/35] unnecessary lines in setup --- setup.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/setup.py b/setup.py index ad4940a..b6a973f 100644 --- a/setup.py +++ b/setup.py @@ -3,12 +3,6 @@ from setuptools import setup, find_packages -lines = Path(__file__).parent.joinpath("sphinx_copybutton", "__init__.py").read_text() -for line in lines.split('\n'): - if line.startswith("__version__"): - break -version = line.split(" = ")[-1].strip('"') - if (os.path.isdir('clipboard.js') and not os.path.islink('sphinx_copybutton/_static/clipboard.min.js')): raise SystemExit("Error: Support for symbolic links is required") From faf57dc8c25042c6460be77b205559e48fec4b88 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Thu, 23 Apr 2020 15:17:50 -0700 Subject: [PATCH 31/35] github action --- .github/workflows/artifacts.yml | 12 ++++++++ .github/workflows/integration.yml | 51 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 .github/workflows/artifacts.yml create mode 100644 .github/workflows/integration.yml diff --git a/.github/workflows/artifacts.yml b/.github/workflows/artifacts.yml new file mode 100644 index 0000000..cf5a036 --- /dev/null +++ b/.github/workflows/artifacts.yml @@ -0,0 +1,12 @@ +on: [status] + +jobs: + circleci_artifacts_redirector_job: + runs-on: ubuntu-latest + name: Run CircleCI artifacts redirector + steps: + - name: GitHub Action step + uses: larsoner/circleci-artifacts-redirector-action@master + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + artifact-path: 0/html/index.html diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml new file mode 100644 index 0000000..63acaca --- /dev/null +++ b/.github/workflows/integration.yml @@ -0,0 +1,51 @@ +name: continuous-integration + +on: [push, pull_request] + +jobs: + + docs: + + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.6, 3.7, 3.8] + + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v1 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e .[dev,sphinx] + + - name: Build docs + run: | + cd doc + make html + + publish: + + name: Publish to PyPi + needs: [docs] + if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags') + runs-on: ubuntu-latest + steps: + - name: Checkout source + uses: actions/checkout@v2 + - name: Set up Python 3.7 + uses: actions/setup-python@v1 + with: + python-version: 3.7 + - name: Build package + run: | + pip install wheel + python setup.py sdist bdist_wheel + - name: Publish + uses: pypa/gh-action-pypi-publish@v1.1.0 + with: + user: __token__ + password: ${{ secrets.PYPI_KEY }} From 0f55dc00ce8f1b7a59a1c31db825c65cb7db3306 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Thu, 23 Apr 2020 15:21:23 -0700 Subject: [PATCH 32/35] fixing workflow --- .github/workflows/integration.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 63acaca..6c69074 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -20,7 +20,9 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install -e .[dev,sphinx] + git submodule update --init + pip install -e . + pip install -r doc/requirements.txt - name: Build docs run: | From 04fa30710c67c3e5cfe37fdf5d6dfab09e3fa24b Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Thu, 23 Apr 2020 15:31:38 -0700 Subject: [PATCH 33/35] RLS: 0.2.11 --- sphinx_copybutton/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index 53a7fd6..af10a84 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -2,7 +2,7 @@ import os from sphinx.util import logging -__version__ = "0.2.11dev0" +__version__ = "0.2.11" logger = logging.getLogger(__name__) From a7a2d99ffd2f1db2e14992cbec785a491aae8a73 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Thu, 23 Apr 2020 15:32:11 -0700 Subject: [PATCH 34/35] DEV: 0.2.12dev0 --- sphinx_copybutton/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index af10a84..d74756d 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -2,7 +2,7 @@ import os from sphinx.util import logging -__version__ = "0.2.11" +__version__ = "0.2.12dev0" logger = logging.getLogger(__name__) From 68f14248d53b70440ae22ae88675e2249028c649 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Thu, 23 Apr 2020 15:35:17 -0700 Subject: [PATCH 35/35] RLS: v0.2.11 fix 1 --- .github/workflows/integration.yml | 1 + sphinx_copybutton/__init__.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 6c69074..6db8b40 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -45,6 +45,7 @@ jobs: - name: Build package run: | pip install wheel + git submodule update --init python setup.py sdist bdist_wheel - name: Publish uses: pypa/gh-action-pypi-publish@v1.1.0 diff --git a/sphinx_copybutton/__init__.py b/sphinx_copybutton/__init__.py index d74756d..af10a84 100644 --- a/sphinx_copybutton/__init__.py +++ b/sphinx_copybutton/__init__.py @@ -2,7 +2,7 @@ import os from sphinx.util import logging -__version__ = "0.2.12dev0" +__version__ = "0.2.11" logger = logging.getLogger(__name__)