Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the code and code-block directives in ReST?

In every renderer I've tried, code and code-block seem to have the exact same result: a block of code. Both generate the exact same html -- including classes and tag kind (using I think it was docutils?):

<pre class="code [language] literal-block">

So, what's the difference? Why do we have both? Are there any renderers that render them differently? Is there a semantic difference?

like image 962
potato Avatar asked Jan 18 '16 01:01

potato


People also ask

What is a Toctree?

.. contents is a doctutils directive (the underlying library which defines ReST and associated utilities) and automatically generates a table of contents from headlines within the current topic. .. toctree is a Sphinx-defined directive in which you explicitly list documents whose TOCs will be listed out.

What is a directive Sphinx?

In Sphinx/Docutils, directives have a different meaning. Directives in sphinx-lesson are actually the special case of the generic directive class called admonitions. Directives are used to set off a certain block of text. They can be used as an aside or block (e.g. exercise , instructor-note ).

What is code block in Python?

A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block.


1 Answers

code is a reStructuredText directive. code-block is a Sphinx directive.

The code-block has a different set of options to the codedirective. E.g. :emphasize-lines:

As you are using Sphinx I would recommend using the code-block directive.

When using code-block I always get the correct highlighting. When using code I sometimes get colors and sometimes just literal code blocks. I have yet to figure out what combinations of setting in conf.py that predictably generates colored output.

The code directive does have the advantage that the document can be used both in a Sphinx document tree and at the same time as a stand-alone reStructuredText document.

like image 157
P.G. Avatar answered Oct 06 '22 11:10

P.G.