|
84 | 84 | figure. This overwrites the caption given in the content, when the plot |
85 | 85 | is generated from a file. |
86 | 86 |
|
| 87 | +``:code-caption:`` : str |
| 88 | + If specified, the option's argument will be used as a caption for the |
| 89 | + code block (when ``:include-source:`` is used). This is added as the |
| 90 | + ``:caption:`` option to the ``.. code-block::`` directive. |
| 91 | +
|
87 | 92 | Additionally, this directive supports all the options of the `image directive |
88 | 93 | <https://docutils.sourceforge.io/docs/ref/rst/directives.html#image>`_, |
89 | 94 | except for ``:target:`` (since plot will add its own target). These include |
@@ -281,6 +286,7 @@ class PlotDirective(Directive): |
281 | 286 | 'context': _option_context, |
282 | 287 | 'nofigs': directives.flag, |
283 | 288 | 'caption': directives.unchanged, |
| 289 | + 'code-caption': directives.unchanged, |
284 | 290 | } |
285 | 291 |
|
286 | 292 | def run(self): |
@@ -952,8 +958,11 @@ def run(arguments, content, options, state_machine, state, lineno): |
952 | 958 | if is_doctest: |
953 | 959 | lines = ['', *code_piece.splitlines()] |
954 | 960 | else: |
955 | | - lines = ['.. code-block:: python', '', |
956 | | - *textwrap.indent(code_piece, ' ').splitlines()] |
| 961 | + lines = ['.. code-block:: python'] |
| 962 | + if 'code-caption' in options: |
| 963 | + code_caption = options['code-caption'].replace('\n', ' ') |
| 964 | + lines.append(f' :caption: {code_caption}') |
| 965 | + lines.extend(['', *textwrap.indent(code_piece, ' ').splitlines()]) |
957 | 966 | source_code = "\n".join(lines) |
958 | 967 | else: |
959 | 968 | source_code = "" |
|
0 commit comments