summaryrefslogtreecommitdiff
path: root/sphinx/directives/code.py
diff options
context:
space:
mode:
authortk0miya <i.tkomiya@gmail.com>2014-09-19 12:04:25 +0900
committertk0miya <i.tkomiya@gmail.com>2014-09-19 12:04:25 +0900
commitd0764521996ffdae0fa9e8dba120fcbd182fafbd (patch)
tree0c18633f1c819d7ac673f24e33c4f488b4d57b44 /sphinx/directives/code.py
parent007a15454fe790cf17ebe0934c03b03055b1375a (diff)
downloadsphinx-git-d0764521996ffdae0fa9e8dba120fcbd182fafbd.tar.gz
do nested_parse() :caption: of code-block directive
Diffstat (limited to 'sphinx/directives/code.py')
-rw-r--r--sphinx/directives/code.py38
1 files changed, 29 insertions, 9 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py
index 6ea525b0f..855437594 100644
--- a/sphinx/directives/code.py
+++ b/sphinx/directives/code.py
@@ -13,6 +13,7 @@ from difflib import unified_diff
from docutils import nodes
from docutils.parsers.rst import Directive, directives
+from docutils.statemachine import ViewList
from six import string_types
@@ -47,7 +48,6 @@ class Highlight(Directive):
linenothreshold=linenothreshold)]
-
def dedent_lines(lines, dedent):
if not dedent:
return lines
@@ -62,6 +62,21 @@ def dedent_lines(lines, dedent):
return new_lines
+def container_wrapper(directive, literal_node, caption):
+ container_node = nodes.container('', literal_block=True)
+
+ parsed = nodes.Element()
+ directive.state.nested_parse(ViewList([caption], source=''),
+ directive.content_offset, parsed)
+ caption_node = nodes.caption(parsed[0].rawsource, '',
+ *parsed[0].children)
+ caption_node.source = parsed[0].source
+ caption_node.line = parsed[0].line
+ container_node += caption_node
+ container_node += literal_node
+ return container_node
+
+
class CodeBlock(Directive):
"""
Directive for a code block with special highlighting or line numbering
@@ -101,9 +116,6 @@ class CodeBlock(Directive):
literal = nodes.literal_block(code, code)
literal['language'] = self.arguments[0]
- caption = self.options.get('caption')
- if caption:
- literal['caption'] = caption
literal['linenos'] = 'linenos' in self.options or \
'lineno-start' in self.options
extra_args = literal['highlight_args'] = {}
@@ -112,6 +124,11 @@ class CodeBlock(Directive):
if 'lineno-start' in self.options:
extra_args['linenostart'] = self.options['lineno-start']
set_source_info(self, literal)
+
+ caption = self.options.get('caption')
+ if caption:
+ literal = container_wrapper(self, literal, caption)
+
return [literal]
@@ -269,17 +286,20 @@ class LiteralInclude(Directive):
retnode['language'] = self.options['language']
retnode['linenos'] = 'linenos' in self.options or \
'lineno-start' in self.options
- caption = self.options.get('caption')
- if caption is not None:
- if not caption:
- caption = self.arguments[0]
- retnode['caption'] = caption
extra_args = retnode['highlight_args'] = {}
if hl_lines is not None:
extra_args['hl_lines'] = hl_lines
if 'lineno-start' in self.options:
extra_args['linenostart'] = self.options['lineno-start']
env.note_dependency(rel_filename)
+
+ caption = self.options.get('caption')
+ if caption is not None:
+ if caption:
+ retnode = container_wrapper(self, retnode, caption)
+ else:
+ retnode = container_wrapper(self, retnode, self.arguments[0])
+
return [retnode]