Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
myst_enable_extensions = ["colon_fence"]

# CopyButton configuration
copybutton_prompt_text = r">>> |\.\.\. |\$ |In \[\d*\]: | {2,5}\.\.\.: | {5,8}: "
copybutton_prompt_text = r">>> |\.\.\. |\$ |In \[\d*\]: | {2,5}\.{3,}: | {5,8}: "
copybutton_prompt_is_regexp = True
copybutton_line_continuation_character = "\\"
copybutton_here_doc_delimiter = "EOT"
Expand Down
19 changes: 14 additions & 5 deletions docs/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ i.e. at [RegEx101].
For example this documentation uses the following configuration:

```python
copybutton_prompt_text = r">>> |\.\.\. |\$ |In \[\d*\]: | {2,5}\.\.\.: | {5,8}: "
copybutton_prompt_text = r">>> |\.\.\. |\$ |In \[\d*\]: | {2,5}\.{3,}: | {5,8}: "
copybutton_prompt_is_regexp = True
```

Expand All @@ -106,13 +106,16 @@ Which matches the following prompts and their continuations if they exist:
- ``r'\$ '``
- ``'$ '``
* - ``ipython`` and ``qtconsole`` + continuation
- ``r'In \[\d*\]: | {2,5}\.\.\.: '``
- ``'In []: '``, ``'In [999]: '``, ``' ...: '``, ``' ...: '``
- ``r'In \[\d*\]: | {2,5}\.{3,}: '``
- ``'In []: '``, ``'In [999]: '``, ``' ...: '``, ``' ....: '``, ``' .....: '``, ``' ...: '``
* - ``jupyter-console`` + continuation
- ``r'In \[\d*\]: | {5,8}: '``
- ``'In []: '``, ``'In [999]: '``, ``' ...: '``, ``' ...: '``
```

The IPython Sphinx directive adds continuation dots as the cell number grows (for example, `In [10]:` uses ` ....: `).
The `\.{3,}` pattern matches these longer prompts as well as the three-dot prompts used by interactive IPython and QtConsole.

An example usage would be the `ipython`-directive:

```restructuredtext
Expand All @@ -123,7 +126,10 @@ An example usage would be the `ipython`-directive:
In [1]: first
...: continuation
output
In [2]: second
In [10]: first
....: continuation
output
In [11]: second

``jupyter`` style:

Expand All @@ -141,7 +147,10 @@ An example usage would be the `ipython`-directive:
In [1]: first
...: continuation
output
In [2]: second
In [10]: first
....: continuation
output
In [11]: second
```

`jupyter` style:
Expand Down
20 changes: 20 additions & 0 deletions sphinx_copybutton/_static/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import test from 'ava';
import { readFileSync } from 'fs';
import { join } from 'path';
import { formatCopyText, filterText } from "./copybutton_funcs";

const parameters = [
Expand Down Expand Up @@ -300,3 +302,21 @@ filterParameters.forEach((p) => {
});
})
*/


// Exercise the configuration that readers copy and the documentation itself uses.
const docsConfig = readFileSync(join(__dirname, '../../docs/conf.py'), 'utf8');
const documentedPrompt = docsConfig.match(/^copybutton_prompt_text = r"(.*)"$/m)[1];
const usage = readFileSync(join(__dirname, '../../docs/use.md'), 'utf8');
test('documented prompt matches the documentation configuration', t => {
t.true(usage.includes(`copybutton_prompt_text = r"${documentedPrompt}"`));
});

[1, 10, 100, 1000].forEach(count => {
test(`documented prompt copies IPython cell ${count} continuations`, t => {
// IPython's Sphinx directive uses two more dots than counter digits.
const continuation = ' ' + '.'.repeat(String(count).length + 2) + ': ';
const text = `In [${count}]: for value in [1, 2]:\n${continuation} print(value)\n1\n2`;
t.is(formatCopyText(text, documentedPrompt, true), 'for value in [1, 2]:\n print(value)');
});
});