diff options
author | Jean-François B <jfbu@free.fr> | 2017-02-13 19:38:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-13 19:38:22 +0100 |
commit | a7becf30ded7472c4ef8fbce632d4de46c416e66 (patch) | |
tree | 68197fa93d9a52cfb19828ff2f42cfa23295f99c /sphinx/directives/code.py | |
parent | f7e5043949c2118d3bbb918ebeb6b273594c0339 (diff) | |
parent | 3e6eba790012ee3c919302fc05bb0c186105aece (diff) | |
download | sphinx-git-a7becf30ded7472c4ef8fbce632d4de46c416e66.tar.gz |
Merge branch 'master' into literalincludelines
Diffstat (limited to 'sphinx/directives/code.py')
-rw-r--r-- | sphinx/directives/code.py | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 85fc28941..2a263220f 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -11,17 +11,22 @@ import sys import codecs from difflib import unified_diff +from six import string_types + from docutils import nodes from docutils.parsers.rst import Directive, directives 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 +if False: + # For type annotation + from typing import Any # NOQA + from sphinx.application import Sphinx # NOQA + class Highlight(Directive): """ @@ -38,6 +43,7 @@ class Highlight(Directive): } def run(self): + # type: () -> List[nodes.Node] if 'linenothreshold' in self.options: try: linenothreshold = int(self.options['linenothreshold']) @@ -50,6 +56,7 @@ class Highlight(Directive): def dedent_lines(lines, dedent): + # type: (List[unicode], int) -> List[unicode] if not dedent: return lines @@ -64,6 +71,7 @@ def dedent_lines(lines, dedent): def container_wrapper(directive, literal_node, caption): + # type: (Directive, nodes.Node, unicode) -> nodes.container container_node = nodes.container('', literal_block=True, classes=['literal-block-wrapper']) parsed = nodes.Element() @@ -101,6 +109,7 @@ class CodeBlock(Directive): } def run(self): + # type: () -> List[nodes.Node] code = u'\n'.join(self.content) linespec = self.options.get('emphasize-lines') @@ -137,7 +146,7 @@ class CodeBlock(Directive): literal = container_wrapper(self, literal, caption) except ValueError as exc: document = self.state.document - errmsg = _('Invalid caption: %s' % exc[0][0].astext()) + errmsg = _('Invalid caption: %s' % exc[0][0].astext()) # type: ignore return [document.reporter.warning(errmsg, line=self.lineno)] # literal will be note_implicit_target that is linked from caption and numref. @@ -182,11 +191,12 @@ class LiteralInclude(Directive): } def read_with_encoding(self, filename, document, codec_info, encoding): + # type: (unicode, nodes.Node, Any, unicode) -> List try: with codecs.StreamReaderWriter(open(filename, 'rb'), codec_info[2], codec_info[3], 'strict') as f: lines = f.readlines() - lines = dedent_lines(lines, self.options.get('dedent')) + lines = dedent_lines(lines, self.options.get('dedent')) # type: ignore return lines except (IOError, OSError): return [document.reporter.warning( @@ -199,6 +209,7 @@ class LiteralInclude(Directive): (encoding, filename))] def run(self): + # type: () -> List[nodes.Node] document = self.state.document if not document.settings.file_insertion_enabled: return [document.reporter.warning('File insertion disabled', @@ -371,7 +382,7 @@ class LiteralInclude(Directive): retnode = container_wrapper(self, retnode, caption) except ValueError as exc: document = self.state.document - errmsg = _('Invalid caption: %s' % exc[0][0].astext()) + errmsg = _('Invalid caption: %s' % exc[0][0].astext()) # type: ignore return [document.reporter.warning(errmsg, line=self.lineno)] # retnode will be note_implicit_target that is linked from caption and numref. @@ -382,6 +393,7 @@ class LiteralInclude(Directive): def setup(app): + # type: (Sphinx) -> Dict[unicode, Any] directives.register_directive('highlight', Highlight) directives.register_directive('highlightlang', Highlight) # old directives.register_directive('code-block', CodeBlock) |