diff options
Diffstat (limited to 'sphinx/directives/code.py')
-rw-r--r-- | sphinx/directives/code.py | 58 |
1 files changed, 33 insertions, 25 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 2f232a666..a98ab5883 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -9,16 +9,20 @@ import codecs import sys +import warnings from difflib import unified_diff from docutils import nodes -from docutils.parsers.rst import Directive, directives +from docutils.parsers.rst import directives from docutils.statemachine import ViewList +from six import text_type from sphinx import addnodes +from sphinx.deprecation import RemovedInSphinx40Warning from sphinx.locale import __ from sphinx.util import logging from sphinx.util import parselinenos +from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import set_source_info if False: @@ -30,7 +34,7 @@ if False: logger = logging.getLogger(__name__) -class Highlight(Directive): +class Highlight(SphinxDirective): """ Directive to set the highlighting language for code blocks, as well as the threshold for line numbers. @@ -41,22 +45,27 @@ class Highlight(Directive): optional_arguments = 0 final_argument_whitespace = False option_spec = { - 'linenothreshold': directives.unchanged, + 'linenothreshold': directives.positive_int, } def run(self): # type: () -> List[nodes.Node] - if 'linenothreshold' in self.options: - try: - linenothreshold = int(self.options['linenothreshold']) - except Exception: - linenothreshold = 10 - else: - linenothreshold = sys.maxsize + linenothreshold = self.options.get('linenothreshold', sys.maxsize) return [addnodes.highlightlang(lang=self.arguments[0].strip(), linenothreshold=linenothreshold)] +class HighlightLang(Highlight): + """highlightlang directive (deprecated)""" + + def run(self): + # type: () -> List[nodes.Node] + warnings.warn('highlightlang directive is deprecated. ' + 'Please use highlight directive instead.', + RemovedInSphinx40Warning) + return Highlight.run(self) + + def dedent_lines(lines, dedent, location=None): # type: (List[unicode], int, Any) -> List[unicode] if not dedent: @@ -76,7 +85,7 @@ def dedent_lines(lines, dedent, location=None): def container_wrapper(directive, literal_node, caption): - # type: (Directive, nodes.Node, unicode) -> nodes.container + # type: (SphinxDirective, nodes.Node, unicode) -> nodes.container container_node = nodes.container('', literal_block=True, classes=['literal-block-wrapper']) parsed = nodes.Element() @@ -94,7 +103,7 @@ def container_wrapper(directive, literal_node, caption): return container_node -class CodeBlock(Directive): +class CodeBlock(SphinxDirective): """ Directive for a code block with special highlighting or line numbering settings. @@ -126,7 +135,7 @@ class CodeBlock(Directive): nlines = len(self.content) hl_lines = parselinenos(linespec, nlines) if any(i >= nlines for i in hl_lines): - logger.warning('line number spec is out of range(1-%d): %r' % + logger.warning(__('line number spec is out of range(1-%d): %r') % (nlines, self.options['emphasize-lines']), location=location) @@ -159,7 +168,7 @@ class CodeBlock(Directive): try: literal = container_wrapper(self, literal, caption) except ValueError as exc: - return [document.reporter.warning(str(exc), line=self.lineno)] + return [document.reporter.warning(text_type(exc), 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. @@ -268,7 +277,7 @@ class LiteralIncludeReader(object): if linespec: linelist = parselinenos(linespec, len(lines)) if any(i >= len(lines) for i in linelist): - logger.warning('line number spec is out of range(1-%d): %r' % + logger.warning(__('line number spec is out of range(1-%d): %r') % (len(lines), linespec), location=location) if 'lineno-match' in self.options: @@ -364,13 +373,14 @@ class LiteralIncludeReader(object): return lines def dedent_filter(self, lines, location=None): + # type: (List[unicode], Any) -> List[unicode] if 'dedent' in self.options: return dedent_lines(lines, self.options.get('dedent'), location=location) else: return lines -class LiteralInclude(Directive): +class LiteralInclude(SphinxDirective): """ Like ``.. include:: :literal:``, but only warns if the include file is not found, and does not raise errors. Also has several options for @@ -410,19 +420,17 @@ class LiteralInclude(Directive): if not document.settings.file_insertion_enabled: return [document.reporter.warning('File insertion disabled', line=self.lineno)] - env = document.settings.env - # convert options['diff'] to absolute path if 'diff' in self.options: - _, path = env.relfn2path(self.options['diff']) + _, path = self.env.relfn2path(self.options['diff']) self.options['diff'] = path try: location = self.state_machine.get_source_and_line(self.lineno) - rel_filename, filename = env.relfn2path(self.arguments[0]) - env.note_dependency(rel_filename) + rel_filename, filename = self.env.relfn2path(self.arguments[0]) + self.env.note_dependency(rel_filename) - reader = LiteralIncludeReader(filename, self.options, env.config) + reader = LiteralIncludeReader(filename, self.options, self.config) text, lines = reader.read(location=location) retnode = nodes.literal_block(text, text, source=filename) @@ -439,7 +447,7 @@ class LiteralInclude(Directive): if 'emphasize-lines' in self.options: hl_lines = parselinenos(self.options['emphasize-lines'], lines) if any(i >= lines for i in hl_lines): - logger.warning('line number spec is out of range(1-%d): %r' % + logger.warning(__('line number spec is out of range(1-%d): %r') % (lines, self.options['emphasize-lines']), location=location) extra_args['hl_lines'] = [x + 1 for x in hl_lines if x < lines] @@ -455,13 +463,13 @@ class LiteralInclude(Directive): return [retnode] except Exception as exc: - return [document.reporter.warning(str(exc), line=self.lineno)] + return [document.reporter.warning(text_type(exc), line=self.lineno)] def setup(app): # type: (Sphinx) -> Dict[unicode, Any] directives.register_directive('highlight', Highlight) - directives.register_directive('highlightlang', Highlight) # old + directives.register_directive('highlightlang', HighlightLang) directives.register_directive('code-block', CodeBlock) directives.register_directive('sourcecode', CodeBlock) directives.register_directive('literalinclude', LiteralInclude) |