summaryrefslogtreecommitdiff
path: root/sphinx/directives/code.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2016-08-12 21:14:10 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2016-08-12 21:14:10 +0900
commitb69d37375e583872eaa680906cba60c272e99f40 (patch)
treea7d45b4615b359455be2c059ed8bd9247a00a8d7 /sphinx/directives/code.py
parentbfcc2a159bc6786aad2c433c14437a0c4cc38ffa (diff)
downloadsphinx-git-b69d37375e583872eaa680906cba60c272e99f40.tar.gz
Fix parsing errors for caption of code-blocks are displayed in document (ref: #2845)
Diffstat (limited to 'sphinx/directives/code.py')
-rw-r--r--sphinx/directives/code.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py
index fac8f6419..8edfd61d7 100644
--- a/sphinx/directives/code.py
+++ b/sphinx/directives/code.py
@@ -18,6 +18,7 @@ from docutils.statemachine import ViewList
from six import string_types
from sphinx import addnodes
+from sphinx.locale import _
from sphinx.util import parselinenos
from sphinx.util.nodes import set_source_info
@@ -68,6 +69,8 @@ def container_wrapper(directive, literal_node, caption):
parsed = nodes.Element()
directive.state.nested_parse(ViewList([caption], source=''),
directive.content_offset, parsed)
+ if isinstance(parsed[0], nodes.system_message):
+ raise ValueError(parsed[0])
caption_node = nodes.caption(parsed[0].rawsource, '',
*parsed[0].children)
caption_node.source = parsed[0].source
@@ -131,7 +134,12 @@ class CodeBlock(Directive):
caption = self.options.get('caption')
if caption:
self.options.setdefault('name', nodes.fully_normalize_name(caption))
- literal = container_wrapper(self, literal, caption)
+ try:
+ literal = container_wrapper(self, literal, caption)
+ except ValueError as exc:
+ document = self.state.document
+ errmsg = _('Invalid caption: %s' % exc[0][0].astext())
+ return [document.reporter.warning(errmsg, line=self.lineno)]
# literal will be note_implicit_target that is linked from caption and numref.
# when options['name'] is provided, it should be primary ID.
@@ -337,7 +345,12 @@ class LiteralInclude(Directive):
if not caption:
caption = self.arguments[0]
self.options.setdefault('name', nodes.fully_normalize_name(caption))
- retnode = container_wrapper(self, retnode, caption)
+ try:
+ retnode = container_wrapper(self, retnode, caption)
+ except ValueError as exc:
+ document = self.state.document
+ errmsg = _('Invalid caption: %s' % exc[0][0].astext())
+ return [document.reporter.warning(errmsg, line=self.lineno)]
# retnode will be note_implicit_target that is linked from caption and numref.
# when options['name'] is provided, it should be primary ID.