Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a copy button in the code blocks for RST/Read the Docs

I have been working on a personal "how-to" guide, chronicling and keeping a journal of my studies as I go along.

I now have a, almost too long block of code that, when I've encountered this length of code myself, its always frustrating trying to highlight JUST the block without it highlighting the whole page, or not enough.

So, my question is, for rst (reStructuredText) .. code-block::'s, is there an add-on or a way to add in a copy button, for automatic highlighting or automatically adding the text to the users clipboard? Or would this be a more html-literal type of code I'd have to include in the build and reference it in the code block? If so, what would something like that look like as well?

like image 287
jpartain89 Avatar asked Aug 28 '16 01:08

jpartain89


People also ask

How do you use a sphinx code block?

Literal code blocks (ref) are introduced by ending a paragraph with the special marker :: . The literal block must be indented (and, like all paragraphs, separated from the surrounding ones by blank lines): This is a normal text paragraph.

What is RST Sphinx?

RST stands for ReStructured Text. It is the standard markup language used for documenting python packages. Sphinx is the Python package that generates an html website from RST files, and it is what we are using to generate this site.


1 Answers

There is a dedicated package for that called sphinx-copybutton.

It's straightforward to use.

# Install
pip install sphinx-copybutton
# Declare it in the conf.py
extensions = [
    ...
    'sphinx_copybutton'
    ...
]

And that's it, generate the doc and a copy button will appear automatically in each code block.

like image 51
Romain Avatar answered Nov 15 '22 10:11

Romain