Skip to content

Commit 22a2485

Browse files
sappelhoffamotl
andauthored
✨ NEW: Add line continuation matching and "HERE-documents" (#126)
Co-authored-by: Andreas Motl <[email protected]>
1 parent 780b954 commit 22a2485

File tree

8 files changed

+3195
-27
lines changed

8 files changed

+3195
-27
lines changed

doc/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
# CopyButton configuration
101101
copybutton_prompt_text = r">>> |\.\.\. |\$ |In \[\d*\]: | {2,5}\.\.\.: | {5,8}: "
102102
copybutton_prompt_is_regexp = True
103+
copybutton_line_continuation_character = "\\"
104+
copybutton_here_doc_delimiter = "EOT"
103105
# Switches for testing but shouldn't be activated in the live docs
104106
# copybutton_only_copy_prompt_lines = False
105107
# copybutton_remove_prompts = False

doc/index.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,68 @@ To disable copying empty lines, use the following configuration in ``conf.py``:
280280
281281
copybutton_copy_empty_lines = False
282282
283+
Honor line continuation characters when copying multline-snippets
284+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285+
286+
Sometimes you may wish to copy a code block like this one:
287+
288+
.. code-block:: bash
289+
290+
$ datalad download-url http://www.tldp.org/LDP/Bash-Beginners-Guide/Bash-Beginners-Guide.pdf \
291+
--dataset . \
292+
-m "add beginners guide on bash" \
293+
-O books/bash_guide.pdf
294+
295+
Assuming that you specified ``$`` as your prompt, sphinx-copybutton will only copy
296+
the first line by default.
297+
298+
To copy all lines above, you can use the following configuration:
299+
300+
.. code-block:: python
301+
302+
copybutton_line_continuation_character = "\\"
303+
304+
Note that if we want to define ``\`` as the line continuation character, we have to "escape"
305+
it with another ``\``, as with any Python string that should carry a literal ``\``.
306+
307+
Next, this configuration will make the code look for lines to copy based on the rules above,
308+
but if one of the lines to be copied contains a line continuation character,
309+
then the next line will be automatically copied, regardless of whether it matches the other
310+
rules.
311+
312+
Honor HERE-document syntax when copying multline-snippets
313+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
314+
315+
`HERE-documents <https://en.wikipedia.org/wiki/Here_document>`_ are a form of multiline string literals
316+
in which line breaks and other whitespace (including indentation) is preserved.
317+
For example:
318+
319+
.. code-block:: bash
320+
321+
$ cat << EOT > notes.txt
322+
This is an example sentence.
323+
Put some indentation on this line.
324+
325+
EOT
326+
327+
Executing this codeblock in the terminal will create a file ``notes.txt`` with the exact
328+
text from line two of the codeblock until (but not including) the final line containing ``EOT``.
329+
330+
However, assuming that you specified ``$`` as your prompt, sphinx-copybutton will only copy
331+
the first line by default.
332+
333+
sphinx-copybutton can be configured to copy the whole "HERE-document" by using the following
334+
configuration:
335+
336+
.. code-block:: python
337+
338+
copybutton_here_doc_delimiter = "EOT"
339+
340+
This will continue to look for lines to copy based on the rules above,
341+
but if one of the lines to be copied contains the defined delimiter (here: ``EOT``),
342+
then all following lines will be copied literally until the next delimiter is
343+
encountered in a line.
344+
283345
Use a different copy button image
284346
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285347

0 commit comments

Comments
 (0)