Menu

#62 code-block should support :caption:

Default
open
nobody
None
5
2021-08-20
2019-04-09
No

I expect

This to work

```
.. code-block::
:caption: Hello

      print("ciao")

```

## Instead

D000 Error in "code-block" directive:
unknown option: "caption".

Discussion

  • David Goodger

    David Goodger - 2019-04-09

    Ticket moved from /p/docutils/bugs/361/

     
  • David Goodger

    David Goodger - 2019-04-09

    Not a bug. Moved to Feature Requests.

     
  • Roberto Polli

    Roberto Polli - 2019-04-09

    Ok! This affects other packages too https://bugs.launchpad.net/doc8/+bug/1735779

     
  • Günter Milde

    Günter Milde - 2019-04-15

    Note, that the referenced doc8 bug references Sphinx documentation.
    Sphinx builds on Docutils adding syntax.

    The "code" directive in Docutils has only the options "name", "class", and "number-lines".
    http://docutils.sourceforge.net/docs/ref/rst/directives.html#code

    I do not know whether doc8 uses Sphinx or Docutils and hence whether

    • the bug report is invalid, or
    • the reply "bug is in the docutils dependency" is wrong.
     
  • Günter Milde

    Günter Milde - 2019-06-24

    With the workaround, there is no longer an error but the caption is silently ignored.
    A full fix would require changes to the code generating the document tree and maybe also to the
    document model -- unless we can re-use some existing combination of nodes that combines a caption and a literal block.

     
  • Gary T. Giesen

    Gary T. Giesen - 2021-08-20

    Vale (https://github.com/errata-ai/vale) uses rst2html to parse rST files to apply style guides, and the lack of caption support for code blocks is causing it choke and start checking text within the code block (which it should not do). Is it possible to get caption support in rst2html?

    https://www.sphinx-doc.org/en/1.4.9/markup/code.html#caption-and-name

     
    • Günter Milde

      Günter Milde - 2021-09-15

      The "caption" option of the code-block directive is one of several
      extensions made by Sphinx to the rST syntax (compare
      https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html
      and https://docutils.sourceforge.io/docs/ref/rst/directives.html).

      If "vale" wants to support Sphinx extensions, using Sphinx
      modules may be the better option.

      When adding "caption" to the "code" directive in Docutils, we should
      carefully consider how to represent a code block with caption in the
      document tree (https://docutils.sourceforge.io/docs/ref/doctree.html), one
      option would be a topic with title and code block. This can be
      already achieved in a clean way by writing:

      .. topic:: example code
      
         .. code:: python
      
            print("hello")
      

      in the rST source.

      See also the ideas for a "literal" directive
      and generic "float" elements in the Docutils TODO list.
      https://docutils.sourceforge.io/docs/dev/todo.html#body-literal

       

Log in to post a comment.