Ejemplo n.º 1
0
    def lines_filter(self, lines, location=None):
        # type: (List[unicode], Any) -> List[unicode]
        linespec = self.options.get("lines")
        if linespec:
            linelist = parselinenos(linespec, len(lines))
            if any(i >= len(lines) for i in linelist):
                raise ValueError("Line number spec is out of range (1 - %s)" %
                                 len(lines))

            if "lineno-match" in self.options:
                # make sure the line list is not "disjoint".
                first = linelist[0]
                if all(first + i == n for i, n in enumerate(linelist)):
                    self.lineno_start += linelist[0]
                else:
                    raise ValueError(
                        __('Cannot use "lineno-match" with a disjoint '
                           'set of "lines"'))

            lines = [lines[n] for n in linelist if n < len(lines)]
            if lines == []:
                raise ValueError(
                    __("Line spec %r: no lines pulled from include file %r") %
                    (linespec, self.url))

        return lines
Ejemplo n.º 2
0
    def get_codeblock_node(self, code, language):
        """this is copied from sphinx.directives.code.CodeBlock.run

        it has been changed to accept code and language as an arguments instead
        of reading from self

        """

        document = self.state.document
        location = self.state_machine.get_source_and_line(self.lineno)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                nlines = len(code.split('\n'))
                hl_lines = parselinenos(linespec, nlines)
                if any(i >= nlines for i in hl_lines):
                    log.warning(
                        __('line number spec is out of range(1-%d): %r') %
                        (nlines, self.options['emphasize-lines']),
                        location=location)

                hl_lines = [x + 1 for x in hl_lines if x < nlines]
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        if 'dedent' in self.options:
            location = self.state_machine.get_source_and_line(self.lineno)
            lines = code.split('\n')
            lines = dedent_lines(lines,
                                 self.options['dedent'],
                                 location=location)
            code = '\n'.join(lines)

        literal = nodes.literal_block(code, code)
        literal['language'] = language
        literal['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options
        literal['classes'] += self.options.get('class', [])
        extra_args = literal['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']
        set_source_info(self, literal)

        caption = self.options.get('caption')
        if caption:
            try:
                literal = container_wrapper(self, literal, caption)
            except ValueError as exc:
                return [document.reporter.warning(str(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.
        self.add_name(literal)

        return [literal]
Ejemplo n.º 3
0
    def run(self):
        obj_name = self.arguments[0]

        try:
            prefixed_name, obj, parent, modname = import_by_name(obj_name)
        except ImportError:
            raise self.error(
                "Could not locate Python object {} ({} directive).".format(obj_name, self.name)
            )

        document = self.state.document
        code = str(obj)
        location = self.state_machine.get_source_and_line(self.lineno)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                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') %
                                   (nlines, self.options['emphasize-lines']),
                                   location=location)

                hl_lines = [x + 1 for x in hl_lines if x < nlines]
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        if 'dedent' in self.options:
            location = self.state_machine.get_source_and_line(self.lineno)
            lines = code.split('\n')
            lines = dedent_lines(lines, self.options['dedent'], location=location)
            code = '\n'.join(lines)

        literal = nodes.literal_block(code, code)
        literal['language'] = self.options[LANGUAGE_OPTION]
        literal['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options
        literal['classes'] += self.options.get('class', [])
        extra_args = literal['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']
        set_source_info(self, literal)

        caption = self.options.get('caption')
        if caption:
            try:
                literal = container_wrapper(self, literal, caption)
            except ValueError as exc:
                return [document.reporter.warning(str(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.
        self.add_name(literal)

        return [literal]
Ejemplo n.º 4
0
    def run(self) -> List[Node]:
        document = self.state.document
        code = '\n'.join(self.content)
        location = self.state_machine.get_source_and_line(self.lineno)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                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') %
                                   (nlines, self.options['emphasize-lines']),
                                   location=location)

                hl_lines = [x + 1 for x in hl_lines if x < nlines]
            except ValueError as err:
                return [document.reporter.warning(err, line=self.lineno)]
        else:
            hl_lines = None

        if 'dedent' in self.options:
            location = self.state_machine.get_source_and_line(self.lineno)
            lines = code.split('\n')
            lines = dedent_lines(lines, self.options['dedent'], location=location)
            code = '\n'.join(lines)

        literal = nodes.literal_block(code, code)  # type: Element
        if 'linenos' in self.options or 'lineno-start' in self.options:
            literal['linenos'] = True
        literal['classes'] += self.options.get('class', [])
        literal['force'] = 'force' in self.options
        if self.arguments:
            # highlight language specified
            literal['language'] = self.arguments[0]
        else:
            # no highlight language specified.  Then this directive refers the current
            # highlight setting via ``highlight`` directive or ``highlight_language``
            # configuration.
            literal['language'] = self.env.temp_data.get('highlight_language',
                                                         self.config.highlight_language)
        extra_args = literal['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']
        self.set_source_info(literal)

        caption = self.options.get('caption')
        if caption:
            try:
                literal = container_wrapper(self, literal, caption)
            except ValueError as exc:
                return [document.reporter.warning(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.
        self.add_name(literal)

        return [literal]
Ejemplo n.º 5
0
    def run(self):
        code = u'\n'.join(self.content)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                nlines = len(self.content)
                hl_lines = [x + 1 for x in parselinenos(linespec, nlines)]
            except ValueError as err:
                document = self.state.document
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        if 'dedent' in self.options:
            lines = code.split('\n')
            lines = dedent_lines(lines, self.options['dedent'])
            code = '\n'.join(lines)

        literal = nodes.literal_block(code, code)
        literal['language'] = self.arguments[0]
        literal['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options
        extra_args = literal['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']
        set_source_info(self, literal)

        caption = self.options.get('caption')
        if caption:
            literal = container_wrapper(self, literal, caption)

        return [literal]
Ejemplo n.º 6
0
 def get_hl(self):
     linespec = self.options.get('emphasize-lines')
     if linespec:
         nlines = len(self.content)
         hl_lines = parselinenos(linespec, nlines)
         return [x + 1 for x in hl_lines if x < nlines]
     return None
Ejemplo n.º 7
0
 def get_hl(self):
     linespec = self.options.get('emphasize-lines')
     if linespec:
         nlines = len(self.content)
         hl_lines = parselinenos(linespec, nlines)
         return [x + 1 for x in hl_lines if x < nlines]
     return None          
Ejemplo n.º 8
0
    def run(self):
        # This only works lazily because the logger is inited by Sphinx
        from . import logger
        location = self.state_machine.get_source_and_line(self.lineno)

        if self.arguments:
            # As per 'sphinx.directives.code.LiteralInclude'
            env = self.state.document.settings.env
            rel_filename, filename = env.relfn2path(self.arguments[0])
            env.note_dependency(rel_filename)
            if self.content:
                logger.warning(
                    'Ignoring inline code in Jupyter cell included from "{}"'.
                    format(rel_filename),
                    location=location,
                )
            try:
                with open(filename) as f:
                    content = [line.rstrip() for line in f.readlines()]
            except (IOError, OSError):
                raise IOError(
                    "File {} not found or reading it failed".format(filename))
        else:
            self.assert_has_content()
            content = self.content

        # The code fragment is taken from CodeBlock directive almost unchanged:
        # https://github.com/sphinx-doc/sphinx/blob/0319faf8f1503453b6ce19020819a8cf44e39f13/sphinx/directives/code.py#L134-L148
        emphasize_linespec = self.options.get("emphasize-lines")
        if emphasize_linespec:
            try:
                nlines = len(content)
                hl_lines = parselinenos(emphasize_linespec, nlines)
                if any(i >= nlines for i in hl_lines):
                    logger.warning(
                        "Line number spec is out of range(1-{}): {}".format(
                            nlines, emphasize_linespec),
                        location=location,
                    )
                hl_lines = [i + 1 for i in hl_lines if i < nlines]
            except ValueError as err:
                return [
                    self.state.document.reporter.warning(err, line=self.lineno)
                ]
        else:
            hl_lines = []

        return [
            JupyterCellNode(
                "",
                docutils.nodes.literal_block(text="\n".join(content)),
                hide_code=("hide-code" in self.options),
                hide_output=("hide-output" in self.options),
                code_below=("code-below" in self.options),
                linenos=("linenos" in self.options),
                emphasize_lines=hl_lines,
                raises=self.options.get("raises"),
                stderr=("stderr" in self.options),
            )
        ]
Ejemplo n.º 9
0
    def run(self):
        code = u'\n'.join(self.content)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                nlines = len(self.content)
                hl_lines = [x+1 for x in parselinenos(linespec, nlines)]
            except ValueError as err:
                document = self.state.document
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        literal = nodes.literal_block(code, code)
        literal['language'] = self.arguments[0]
        filename = self.options.get('filename')
        if filename:
            literal['filename'] = filename
        literal['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options
        extra_args = literal['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']
        set_source_info(self, literal)
        return [literal]
Ejemplo n.º 10
0
    def run(self):
        code = u'\n'.join(self.content)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                nlines = len(self.content)
                hl_lines = [x+1 for x in parselinenos(linespec, nlines)]
            except ValueError as err:
                document = self.state.document
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        if 'dedent' in self.options:
            lines = code.split('\n')
            lines = dedent_lines(lines, self.options['dedent'])
            code = '\n'.join(lines)

        literal = nodes.literal_block(code, code)
        literal['language'] = self.arguments[0]
        literal['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options
        extra_args = literal['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']
        set_source_info(self, literal)

        caption = self.options.get('caption')
        if caption:
            literal = container_wrapper(self, literal, caption)

        return [literal]
Ejemplo n.º 11
0
    def run(self):
        document = self.state.document
        env = document.settings.env

        pymodule = self.arguments[0]
        pyfuncion = self.arguments[1]

        text = interact(pymodule, pyfuncion).strip()

        skip = self.options.get('skip', 0)
        if skip:
            filtered_lines = []
            count = 0
            for line in text.split('\n'):
                if count >= skip:
                    filtered_lines.append(line)
                if line.startswith('>>>'):
                    count += 1
            text = '\n'.join(filtered_lines)

        hl_lines = None
        linespec = self.options.get('emphasize-lines')
        if linespec:
            lines = text.split('\n')
            try:
                hl_lines = [x+1 for x in parselinenos(linespec, len(lines))]
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]

        retnode = nodes.literal_block(text, text, source=pymodule)

        if hl_lines is not None:
            retnode['highlight_args'] = {'hl_lines': hl_lines}

        return [retnode]
Ejemplo n.º 12
0
Archivo: code.py Proyecto: zooba/sphinx
    def lines_filter(self,
                     lines: List[str],
                     location: Tuple[str, int] = None) -> List[str]:
        linespec = self.options.get('lines')
        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') %
                    (len(lines), linespec),
                    location=location)

            if 'lineno-match' in self.options:
                # make sure the line list is not "disjoint".
                first = linelist[0]
                if all(first + i == n for i, n in enumerate(linelist)):
                    self.lineno_start += linelist[0]
                else:
                    raise ValueError(
                        __('Cannot use "lineno-match" with a disjoint '
                           'set of "lines"'))

            lines = [lines[n] for n in linelist if n < len(lines)]
            if lines == []:
                raise ValueError(
                    __('Line spec %r: no lines pulled from include file %r') %
                    (linespec, self.filename))

        return lines
Ejemplo n.º 13
0
    def run(self):
        document = self.state.document
        filename = self.arguments[0]
        if not document.settings.file_insertion_enabled:
            return [document.reporter.warning('File insertion disabled',
                                              line=self.lineno)]
        env = document.settings.env
        if filename.startswith('/') or filename.startswith(os.sep):
            rel_fn = filename[1:]
        else:
            docdir = path.dirname(env.doc2path(env.docname, base=None))
            rel_fn = path.normpath(path.join(docdir, filename))
        try:
            fn = path.join(env.srcdir, rel_fn)
        except UnicodeDecodeError:
            # the source directory is a bytestring with non-ASCII characters;
            # let's try to encode the rel_fn in the file system encoding
            rel_fn = rel_fn.encode(sys.getfilesystemencoding())
            fn = path.join(env.srcdir, rel_fn)

        if 'pyobject' in self.options and 'lines' in self.options:
            return [document.reporter.warning(
                'Cannot use both "pyobject" and "lines" options',
                line=self.lineno)]

        encoding = self.options.get('encoding', env.config.source_encoding)
        try:
            f = codecs.open(fn, 'rU', encoding)
            lines = f.readlines()
            f.close()
        except (IOError, OSError):
            return [document.reporter.warning(
                'Include file %r not found or reading it failed' % filename,
                line=self.lineno)]
        except UnicodeError:
            return [document.reporter.warning(
                'Encoding %r used for reading included file %r seems to '
                'be wrong, try giving an :encoding: option' %
                (encoding, filename))]

        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(fn, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [document.reporter.warning(
                    'Object named %r not found in include file %r' %
                    (objectname, filename), line=self.lineno)]
            else:
                lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError, err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            lines = [lines[i] for i in linelist]
Ejemplo n.º 14
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [document.reporter.warning('File insertion disabled',
                                              line=self.lineno)]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(self.arguments[0])

        if 'pyobject' in self.options and 'lines' in self.options:
            return [document.reporter.warning(
                'Cannot use both "pyobject" and "lines" options',
                line=self.lineno)]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        f = None
        try:
            f = codecs.StreamReaderWriter(open(filename, 'rb'),
                    codec_info[2], codec_info[3], 'strict')
            lines = f.readlines()
        except (IOError, OSError):
            return [document.reporter.warning(
                'Include file %r not found or reading it failed' % filename,
                line=self.lineno)]
        except UnicodeError:
            return [document.reporter.warning(
                'Encoding %r used for reading included file %r seems to '
                'be wrong, try giving an :encoding: option' %
                (encoding, filename))]
        finally:
            if f is not None:
                f.close()

        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(filename, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [document.reporter.warning(
                    'Object named %r not found in include file %r' %
                    (objectname, filename), line=self.lineno)]
            else:
                lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError, err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            # just ignore nonexisting lines
            nlines = len(lines)
            lines = [lines[i] for i in linelist if i < nlines]
            if not lines:
                return [document.reporter.warning(
                    'Line spec %r: no lines pulled from include file %r' %
                    (linespec, filename), line=self.lineno)]
Ejemplo n.º 15
0
    def run(self):
        # type: () -> List[nodes.Node]
        document = self.state.document
        code = u'\n'.join(self.content)
        location = self.state_machine.get_source_and_line(self.lineno)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                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') %
                        (nlines, self.options['emphasize-lines']),
                        location=location)

                hl_lines = [x + 1 for x in hl_lines if x < nlines]
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        if 'dedent' in self.options:
            location = self.state_machine.get_source_and_line(self.lineno)
            lines = code.split('\n')
            lines = dedent_lines(lines,
                                 self.options['dedent'],
                                 location=location)
            code = '\n'.join(lines)

        literal = nodes.literal_block(code, code)
        literal['language'] = self.arguments[0]
        literal['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options
        literal['classes'] += self.options.get('class', [])
        extra_args = literal['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']
        set_source_info(self, literal)

        caption = self.options.get('caption')
        if caption:
            try:
                literal = container_wrapper(self, literal, caption)
            except ValueError as exc:
                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.
        self.add_name(literal)

        return [literal]
Ejemplo n.º 16
0
    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',
                                          line=self.lineno)
            ]
        env = document.settings.env

        # convert options['diff'] to absolute path
        if 'diff' in self.options:
            _, path = 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)

            reader = LiteralIncludeReader(filename, self.options, env.config)
            text, lines = reader.read(location=location)

            retnode = nodes.literal_block(text, text, source=filename)
            set_source_info(self, retnode)
            if self.options.get('diff'):  # if diff is set, set udiff
                retnode['language'] = 'udiff'
            elif 'language' in self.options:
                retnode['language'] = self.options['language']
            retnode['linenos'] = ('linenos' in self.options
                                  or 'lineno-start' in self.options
                                  or 'lineno-match' in self.options)
            retnode['classes'] += self.options.get('class', [])
            extra_args = retnode['highlight_args'] = {}
            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') %
                        (lines, self.options['emphasize-lines']),
                        location=location)
                extra_args['hl_lines'] = [x + 1 for x in hl_lines if x < lines]
            extra_args['linenostart'] = reader.lineno_start

            if 'caption' in self.options:
                caption = self.options['caption'] or self.arguments[0]
                retnode = container_wrapper(self, retnode, caption)

            # retnode will be note_implicit_target that is linked from caption and numref.
            # when options['name'] is provided, it should be primary ID.
            self.add_name(retnode)

            return [retnode]
        except Exception as exc:
            return [
                document.reporter.warning(text_type(exc), line=self.lineno)
            ]
Ejemplo n.º 17
0
  def create_node(self, filename, rel_filename, lang):
    document = self.state.document
    env = document.settings.env

    # Read the contents of the file to include
    encoding = self.options.get('encoding', env.config.source_encoding)
    codec_info = codecs.lookup(encoding)

    try:
      f = codecs.StreamReaderWriter(open(filename, 'rb'),
          codec_info[2], codec_info[3], 'strict')
      lines = f.readlines()
      f.close()
    except (IOError, OSError):
      print_err('Failed to read %r' % filename)
      return [document.reporter.warning(
        'Include file %r not found or reading it failed' % filename,
        line=self.lineno)]
    except UnicodeError:
      print_err('Encoding %r used for reading included file %r seems to '
        'be wrong, try giving an :encoding: option' %
        (encoding, filename))
      return [document.reporter.warning(
        'Encoding %r used for reading included file %r seems to '
        'be wrong, try giving an :encoding: option' %
        (encoding, filename))]

    objectname = self.options.get('pyobject')

    if objectname is not None:
      from sphinx.pycode import ModuleAnalyzer
      analyzer = ModuleAnalyzer.for_file(filename, '')
      tags = analyzer.find_tags()

      if objectname not in tags:
        return [document.reporter.warning(
          'Object named %r not found in include file %r' %
          (objectname, filename), line=self.lineno)]
      else:
        lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

    linespec = self.options.get('lines')
    if linespec is not None:
      try:
        linelist = parselinenos(linespec, len(lines))
      except ValueError, err:
        return [document.reporter.warning(str(err), line=self.lineno)]

      # just ignore nonexisting lines
      nlines = len(lines)
      lines = [lines[i] for i in linelist if i < nlines]

      if not lines:
        return [document.reporter.warning(
          'Line spec %r: no lines pulled from include file %r' %
          (linespec, filename), line=self.lineno)]
Ejemplo n.º 18
0
  def create_node(self, filename, rel_filename, lang):
    document = self.state.document
    env = document.settings.env

    # Read the contents of the file to include
    encoding = self.options.get('encoding', env.config.source_encoding)
    codec_info = codecs.lookup(encoding)

    try:
      f = codecs.StreamReaderWriter(open(filename, 'rb'),
          codec_info[2], codec_info[3], 'strict')
      lines = f.readlines()
      f.close()
    except (IOError, OSError):
      print_err('Failed to read %r' % filename)
      return [document.reporter.warning(
        'Include file %r not found or reading it failed' % filename,
        line=self.lineno)]
    except UnicodeError:
      print_err('Encoding %r used for reading included file %r seems to '
        'be wrong, try giving an :encoding: option' %
        (encoding, filename))
      return [document.reporter.warning(
        'Encoding %r used for reading included file %r seems to '
        'be wrong, try giving an :encoding: option' %
        (encoding, filename))]

    objectname = self.options.get('pyobject')

    if objectname is not None:
      from sphinx.pycode import ModuleAnalyzer
      analyzer = ModuleAnalyzer.for_file(filename, '')
      tags = analyzer.find_tags()

      if objectname not in tags:
        return [document.reporter.warning(
          'Object named %r not found in include file %r' %
          (objectname, filename), line=self.lineno)]
      else:
        lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

    linespec = self.options.get('lines')
    if linespec is not None:
      try:
        linelist = parselinenos(linespec, len(lines))
      except ValueError, err:
        return [document.reporter.warning(str(err), line=self.lineno)]

      # just ignore nonexisting lines
      nlines = len(lines)
      lines = [lines[i] for i in linelist if i < nlines]

      if not lines:
        return [document.reporter.warning(
          'Line spec %r: no lines pulled from include file %r' %
          (linespec, filename), line=self.lineno)]
Ejemplo n.º 19
0
 def run(self):
     code = u'\n'.join(self.content)
     linespec = self.options.get('emphasize-lines')
     if linespec:
         try:
             nlines = len(self.content)
             hl_lines = [x + 1 for x in parselinenos(linespec, nlines)]
         except ValueError, err:
             document = self.state.document
             return [document.reporter.warning(str(err), line=self.lineno)]
Ejemplo n.º 20
0
    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",
                                          line=self.lineno)
            ]
        # convert options['diff'] to absolute path
        if "diff" in self.options:
            _, path = self.env.relfn2path(self.options["diff"])
            self.options["diff"] = path

        try:
            location = self.state_machine.get_source_and_line(self.lineno)
            url = self.arguments[0]

            reader = RemoteLiteralIncludeReader(url, self.options, self.config)
            text, lines = reader.read(location=location)

            retnode = nodes.literal_block(text, text, source=url)
            set_source_info(self, retnode)
            if self.options.get("diff"):  # if diff is set, set udiff
                retnode["language"] = "udiff"
            elif "language" in self.options:
                retnode["language"] = self.options["language"]
            retnode["linenos"] = ("linenos" in self.options
                                  or "lineno-start" in self.options
                                  or "lineno-match" in self.options)
            retnode["classes"] += self.options.get("class", [])
            extra_args = retnode["highlight_args"] = {}
            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") %
                        (lines, self.options["emphasize-lines"]),
                        location=location,
                    )
                extra_args["hl_lines"] = [x + 1 for x in hl_lines if x < lines]
            extra_args["linenostart"] = reader.lineno_start

            if "caption" in self.options:
                caption = self.options["caption"] or self.arguments[0]
                retnode = container_wrapper(self, retnode, caption)

            # retnode will be note_implicit_target that is linked from caption and numref.
            # when options['name'] is provided, it should be primary ID.
            self.add_name(retnode)

            return [retnode]
        except Exception as exc:
            return [
                document.reporter.warning(text_type(exc), line=self.lineno)
            ]
Ejemplo n.º 21
0
    def run(self):
        code = u'\n'.join(self.content)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                nlines = len(self.content)
                hl_lines = [x+1 for x in parselinenos(linespec, nlines)]
            except ValueError, err:
                document = self.state.document
                return [document.reporter.warning(str(err), line=self.lineno)]
Ejemplo n.º 22
0
Archivo: code.py Proyecto: LFYG/sphinx
    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',
                                              line=self.lineno)]
        env = document.settings.env

        # convert options['diff'] to absolute path
        if 'diff' in self.options:
            _, path = 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)

            reader = LiteralIncludeReader(filename, self.options, env.config)
            text, lines = reader.read(location=location)

            retnode = nodes.literal_block(text, text, source=filename)
            set_source_info(self, retnode)
            if self.options.get('diff'):  # if diff is set, set udiff
                retnode['language'] = 'udiff'
            elif 'language' in self.options:
                retnode['language'] = self.options['language']
            retnode['linenos'] = ('linenos' in self.options or
                                  'lineno-start' in self.options or
                                  'lineno-match' in self.options)
            retnode['classes'] += self.options.get('class', [])
            extra_args = retnode['highlight_args'] = {}
            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' %
                                   (lines, self.options['emphasize-lines']),
                                   location=location)
                extra_args['hl_lines'] = [x + 1 for x in hl_lines if x < lines]
            extra_args['linenostart'] = reader.lineno_start

            if 'caption' in self.options:
                caption = self.options['caption'] or self.arguments[0]
                retnode = container_wrapper(self, retnode, caption)

            # retnode will be note_implicit_target that is linked from caption and numref.
            # when options['name'] is provided, it should be primary ID.
            self.add_name(retnode)

            return [retnode]
        except Exception as exc:
            return [document.reporter.warning(str(exc), line=self.lineno)]
Ejemplo n.º 23
0
Archivo: code.py Proyecto: LFYG/sphinx
    def run(self):
        # type: () -> List[nodes.Node]
        document = self.state.document
        code = u'\n'.join(self.content)
        location = self.state_machine.get_source_and_line(self.lineno)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                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' %
                                   (nlines, self.options['emphasize-lines']),
                                   location=location)

                hl_lines = [x + 1 for x in hl_lines if x < nlines]
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        if 'dedent' in self.options:
            location = self.state_machine.get_source_and_line(self.lineno)
            lines = code.split('\n')
            lines = dedent_lines(lines, self.options['dedent'], location=location)
            code = '\n'.join(lines)

        literal = nodes.literal_block(code, code)
        literal['language'] = self.arguments[0]
        literal['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options
        literal['classes'] += self.options.get('class', [])
        extra_args = literal['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']
        set_source_info(self, literal)

        caption = self.options.get('caption')
        if caption:
            try:
                literal = container_wrapper(self, literal, caption)
            except ValueError as exc:
                return [document.reporter.warning(str(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.
        self.add_name(literal)

        return [literal]
Ejemplo n.º 24
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [
                document.reporter.warning("File insertion disabled",
                                          line=self.lineno)
            ]
        # convert options['diff'] to absolute path
        if "diff" in self.options:
            _, 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 = self.env.relfn2path(self.arguments[0])
            self.env.note_dependency(rel_filename)

            reader = LiteralIncludeReader(filename, self.options, self.config)
            text, lines = reader.read(location=location)

            retnode = nodes.literal_block(text, text, source=filename)
            set_source_info(self, retnode)
            if self.options.get("diff"):  # if diff is set, set udiff
                retnode["language"] = "udiff"
            elif "language" in self.options:
                retnode["language"] = self.options["language"]
            retnode["linenos"] = ("linenos" in self.options
                                  or "lineno-start" in self.options
                                  or "lineno-match" in self.options)
            retnode["classes"] += self.options.get("class", [])
            extra_args = retnode["highlight_args"] = {}
            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", lines,
                        self.options["emphasize-lines"])
                extra_args["hl_lines"] = [x + 1 for x in hl_lines if x < lines]
            extra_args["linenostart"] = reader.lineno_start

            container_node = nodes.container("",
                                             literal_block=True,
                                             classes=["example-block-wrapper"])
            container_node += example_header(filename=filename)
            container_node += retnode
            retnode = container_node

            return [retnode]
        except Exception as exc:
            return [
                document.reporter.warning(text_type(exc), line=self.lineno)
            ]
Ejemplo n.º 25
0
    def run(self) -> List[Node]:
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [
                document.reporter.warning('File insertion disabled',
                                          line=self.lineno)
            ]

        try:
            uri = self.arguments[0]

            location = self.state_machine.get_source_and_line(self.lineno)

            reader = RemoteIncludeReader(uri, self.options, self.config)
            text, lines = reader.read(location=location)

            # rel_filename, filename = self.env.relfn2path(self.arguments[0])
            # self.env.note_dependency(rel_filename)

            retnode = nodes.literal_block(text, text,
                                          source=uri)  # type: Element
            retnode['force'] = 'force' in self.options
            self.set_source_info(retnode)
            if 'language' in self.options:
                retnode['language'] = self.options['language']
            if ('linenos' in self.options or 'lineno-start' in self.options
                    or 'lineno-match' in self.options):
                retnode['linenos'] = True
            retnode['classes'] += self.options.get('class', [])
            extra_args = retnode['highlight_args'] = {}
            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') %
                        (lines, self.options['emphasize-lines']),
                        location=location)
                extra_args['hl_lines'] = [x + 1 for x in hl_lines if x < lines]
            extra_args['linenostart'] = reader.lineno_start

            if 'caption' in self.options:
                caption = self.options['caption'] or self.arguments[0]
                retnode = container_wrapper(self, retnode, caption)

            # retnode will be note_implicit_target that is linked from caption and numref.
            # when options['name'] is provided, it should be primary ID.
            self.add_name(retnode)

            return [retnode]
        except Exception as exc:
            return [document.reporter.warning(exc, line=self.lineno)]
Ejemplo n.º 26
0
def literalinclude_directive(name, arguments, options, content, lineno,
                             content_offset, block_text, state, state_machine):
    """Like .. include:: :literal:, but only warns if the include file is not found."""
    if not state.document.settings.file_insertion_enabled:
        return [state.document.reporter.warning('File insertion disabled', line=lineno)]
    env = state.document.settings.env
    rel_fn = arguments[0]
    source_dir = path.dirname(path.abspath(state_machine.input_lines.source(
        lineno - state_machine.input_offset - 1)))
    fn = path.normpath(path.join(source_dir, rel_fn))

    if 'pyobject' in options and 'lines' in options:
        return [state.document.reporter.warning(
            'Cannot use both "pyobject" and "lines" options', line=lineno)]

    encoding = options.get('encoding', env.config.source_encoding)
    try:
        f = codecs.open(fn, 'r', encoding)
        #f = open(fn, 'rb')
        lines = f.readlines()
        f.close()
    except (IOError, OSError):
        return [state.document.reporter.warning(
            'Include file %r not found or reading it failed' % arguments[0],
            line=lineno)]
    except UnicodeError:
        return [state.document.reporter.warning(
            'Encoding %r used for reading included file %r seems to '
            'be wrong, try giving an :encoding: option' %
            (encoding, arguments[0]))]

    objectname = options.get('pyobject')
    if objectname is not None:
        from sphinx.pycode import ModuleAnalyzer
        analyzer = ModuleAnalyzer.for_file(fn, '')
        tags = analyzer.find_tags()
        if objectname not in tags:
            return [state.document.reporter.warning(
                'Object named %r not found in include file %r' %
                (objectname, arguments[0]), line=lineno)]
        else:
            lines = lines[tags[objectname][1] - 1 : tags[objectname][2] - 1]

    linespec = options.get('lines')
    if linespec is not None:
        try:
            linelist = parselinenos(linespec, len(lines))
        except ValueError, err:
            return [state.document.reporter.warning(str(err), line=lineno)]
        lines = [lines[i] for i in linelist]
Ejemplo n.º 27
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [
                document.reporter.warning('File insertion disabled',
                                          line=self.lineno)
            ]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(self.arguments[0])

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        try:
            f = codecs.StreamReaderWriter(open(filename, 'rb'), codec_info[2],
                                          codec_info[3], 'strict')
            lines = f.readlines()
            f.close()
        except (IOError, OSError):
            return [
                document.reporter.warning(
                    'Include file %r not found or reading it failed' %
                    filename,
                    line=self.lineno)
            ]
        except UnicodeError:
            return [
                document.reporter.warning(
                    'Encoding %r used for reading included file %r seems to '
                    'be wrong, try giving an :encoding: option' %
                    (encoding, filename))
            ]

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError, err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            # just ignore nonexisting lines
            nlines = len(lines)
            lines = [lines[i] for i in linelist if i < nlines]
            if not lines:
                return [
                    document.reporter.warning(
                        'Line spec %r: no lines pulled from include file %r' %
                        (linespec, filename),
                        line=self.lineno)
                ]
Ejemplo n.º 28
0
    def run(self):
        # type: () -> List[nodes.Node]
        code = u'\n'.join(self.content)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                nlines = len(self.content)
                hl_lines = [x + 1 for x in parselinenos(linespec, nlines)]
            except ValueError as err:
                document = self.state.document
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        if 'dedent' in self.options:
            lines = code.split('\n')
            lines = dedent_lines(lines, self.options['dedent'])
            code = '\n'.join(lines)

        literal = nodes.literal_block(code, code)
        literal['language'] = self.arguments[0]
        literal['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options
        literal['classes'] += self.options.get('class', [])
        extra_args = literal['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']
        set_source_info(self, literal)

        caption = self.options.get('caption')
        if caption:
            try:
                literal = container_wrapper(self, literal, caption)
            except ValueError as exc:
                document = self.state.document
                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.
        # when options['name'] is provided, it should be primary ID.
        self.add_name(literal)

        return [literal]
Ejemplo n.º 29
0
    def run(self):
        code = u'\n'.join(self.content)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                nlines = len(self.content)
                hl_lines = [x+1 for x in parselinenos(linespec, nlines)]
            except ValueError as err:
                document = self.state.document
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        if 'dedent' in self.options:
            lines = code.split('\n')
            lines = dedent_lines(lines, self.options['dedent'])
            code = '\n'.join(lines)

        literal = nodes.literal_block(code, code)
        literal['language'] = self.arguments[0]
        literal['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options
        literal['classes'] += self.options.get('class', [])
        extra_args = literal['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']
        set_source_info(self, literal)

        caption = self.options.get('caption')
        if caption:
            self.options.setdefault('name', nodes.fully_normalize_name(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.
        self.add_name(literal)

        return [literal]
Ejemplo n.º 30
0
def get_highlights(cell, content, location, logger):
    # The code fragment is taken from CodeBlock directive almost unchanged:
    # https://github.com/sphinx-doc/sphinx/blob/0319faf8f1503453b6ce19020819a8cf44e39f13/sphinx/directives/code.py#L134-L148

    emphasize_linespec = cell.options.get("emphasize-lines")
    if emphasize_linespec:
        nlines = len(content)
        hl_lines = parselinenos(emphasize_linespec, nlines)
        if any(i >= nlines for i in hl_lines):
            logger.warning(
                "Line number spec is out of range(1-{}): {}".format(
                    nlines, emphasize_linespec
                ),
                location=location,
            )
        hl_lines = [i + 1 for i in hl_lines if i < nlines]
    else:
        hl_lines = []
    return hl_lines
Ejemplo n.º 31
0
    def run(self):
        code = "\n".join(self.content)

        linespec = self.options.get("emphasize-lines")
        if linespec:
            try:
                nlines = len(self.content)
                hl_lines = [x + 1 for x in parselinenos(linespec, nlines)]
            except ValueError as err:
                document = self.state.document
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        literal = nodes.literal_block(code, code)
        literal["language"] = self.arguments[0]
        literal["linenos"] = "linenos" in self.options
        if hl_lines is not None:
            literal["highlight_args"] = {"hl_lines": hl_lines}
        set_source_info(self, literal)
        return [literal]
Ejemplo n.º 32
0
    def run(self) -> List[Node]:
        logger.warning(
            "DeprecationWarning: doctest directives will be deprecated due to lack of interest. Please use code-block or literalinclude instead."
        )

        node = super(c, self).run()[0]

        # This code copied from sphinx.directives.code
        linespec = self.options.get("emphasize-lines")
        if linespec:
            try:
                nlines = len(self.content)
                hl_lines = [x + 1 for x in parselinenos(linespec, nlines)]
            except ValueError as err:
                document = self.state.document
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None  # type: ignore

        node["classes"] += self.options.get("class", [])
        extra_args = node["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"]
        set_source_info(self, node)

        caption = self.options.get("caption")
        if caption:
            try:
                node = container_wrapper(self, node, caption)
            except ValueError as exc:
                document = self.state.document
                errmsg = _("Invalid caption: %s" %
                           exc[0][0].astext())  # type: ignore
                return [document.reporter.warning(errmsg, line=self.lineno)]

        self.add_name(node)

        return [node]
Ejemplo n.º 33
0
    def run(self):

        document = self.state.document
        env = document.settings.env
        rel_filename, filename = env.relfn2path(self.arguments[0])

	url = repos.repositories[self.arguments[1]]+"/"+self.arguments[2]

	session=requests.Session()
	session.headers.update({"Cache-Control": "no-cache, no-store, must-revalidate"})
        r = session.get(url)
        

	
	if r.status_code != 200:
            raise Exception("Failed to retrieve "+url)

        lines = r.text.split("\n")

	linespec = self.options.get('lines')
  		
        if linespec:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
	    lines = [lines[i] for i in linelist if i < len(lines)]
	    
            if not lines:
                return [document.reporter.warning(
                    'Line spec %r: no lines pulled from include file %r' %
                    (linespec, filename), line=self.lineno)]
	
	lines=self.custom_trim(lines)
	self.content=lines
        

        return super(RemoteCodeBlock, self).run()
Ejemplo n.º 34
0
    def run(self):
        document = self.state.document
        source_file = self.arguments[0]
        code = u'\n'.join(self.content)

        # heavily inspired by https://github.com/sphinx-doc/sphinx/blob/3.x/sphinx/directives/code.py#L134
        emphasized_lines = self.options.get('emphasize-lines')
        if emphasized_lines:
            try:
                nlines = len(self.content)
                hl_lines = parselinenos(emphasized_lines, nlines)
                if any(i >= nlines for i in hl_lines):
                    raise RuntimeError(
                        'line number spec out of range 1-%d: %r' %
                        ((nlines, self.options['emphasize-lines'])))
                hl_lines = [i + 1 for i in hl_lines]
            except ValueError as err:
                return [document.reporter.warning(err, line=self.lineno)]
        else:
            hl_lines = None

        node = SnippetSourceNode(source_file, self.lineno, code, code)
        if hl_lines is not None:
            node['highlight_args'] = {'hl_lines': hl_lines}

        node['commercial'] = ('commercial' in self.options)

        caption = self.options.get('caption')
        if caption:
            try:
                node = container_wrapper(self, node, caption)
            except ValueError as exc:
                return [document.reporter.warning(exc, line=self.lineno)]

        # See: https://github.com/sphinx-doc/sphinx/blob/3.x/sphinx/directives/code.py#L184
        self.add_name(node)

        return [node]
Ejemplo n.º 35
0
Archivo: code.py Proyecto: LFYG/sphinx
    def lines_filter(self, lines, location=None):
        # type: (List[unicode], Any) -> List[unicode]
        linespec = self.options.get('lines')
        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' %
                               (len(lines), linespec), location=location)

            if 'lineno-match' in self.options:
                # make sure the line list is not "disjoint".
                first = linelist[0]
                if all(first + i == n for i, n in enumerate(linelist)):
                    self.lineno_start += linelist[0]
                else:
                    raise ValueError(__('Cannot use "lineno-match" with a disjoint '
                                        'set of "lines"'))

            lines = [lines[n] for n in linelist if n < len(lines)]
            if lines == []:
                raise ValueError(__('Line spec %r: no lines pulled from include file %r') %
                                 (linespec, self.filename))

        return lines
Ejemplo n.º 36
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [document.reporter.warning('File insertion disabled',
                                              line=self.lineno)]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(self.arguments[0])

        if 'pyobject' in self.options and 'lines' in self.options:
            return [document.reporter.warning(
                'Cannot use both "pyobject" and "lines" options',
                line=self.lineno)]

        if 'lineno-match' in self.options and 'lineno-start' in self.options:
            return [document.reporter.warning(
                'Cannot use both "lineno-match" and "lineno-start"',
                line=self.lineno)]

        if 'lineno-match' in self.options and \
           (set(['append', 'prepend']) & set(self.options.keys())):
            return [document.reporter.warning(
                'Cannot use "lineno-match" and "append" or "prepend"',
                line=self.lineno)]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)

        lines = self.read_with_encoding(filename, document,
                                        codec_info, encoding)
        if lines and not isinstance(lines[0], string_types):
            return lines

        diffsource = self.options.get('diff')
        if diffsource is not None:
            tmp, fulldiffsource = env.relfn2path(diffsource)

            difflines = self.read_with_encoding(fulldiffsource, document,
                                                codec_info, encoding)
            if not isinstance(difflines[0], string_types):
                return difflines
            diff = unified_diff(
                difflines,
                lines,
                diffsource,
                self.arguments[0])
            lines = list(diff)

        linenostart = self.options.get('lineno-start', 1)
        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(filename, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [document.reporter.warning(
                    'Object named %r not found in include file %r' %
                    (objectname, filename), line=self.lineno)]
            else:
                lines = lines[tags[objectname][1]-1: tags[objectname][2]-1]
                if 'lineno-match' in self.options:
                    linenostart = tags[objectname][1]

        linespec = self.options.get('lines')
        if linespec:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]

            if 'lineno-match' in self.options:
                # make sure the line list is not "disjoint".
                previous = linelist[0]
                for line_number in linelist[1:]:
                    if line_number == previous + 1:
                        previous = line_number
                        continue
                    return [document.reporter.warning(
                        'Cannot use "lineno-match" with a disjoint set of '
                        '"lines"', line=self.lineno)]
                linenostart = linelist[0] + 1
            # just ignore non-existing lines
            lines = [lines[i] for i in linelist if i < len(lines)]
            if not lines:
                return [document.reporter.warning(
                    'Line spec %r: no lines pulled from include file %r' %
                    (linespec, filename), line=self.lineno)]

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                hl_lines = [x+1 for x in parselinenos(linespec, len(lines))]
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        startafter = self.options.get('start-after')
        endbefore = self.options.get('end-before')
        if startafter is not None or endbefore is not None:
            use = not startafter
            res = []
            for line_number, line in enumerate(lines):
                if not use and startafter and startafter in line:
                    if 'lineno-match' in self.options:
                        linenostart += line_number + 1
                    use = True
                elif use and endbefore and endbefore in line:
                    break
                elif use:
                    res.append(line)
            lines = res

        prepend = self.options.get('prepend')
        if prepend:
            lines.insert(0, prepend + '\n')

        append = self.options.get('append')
        if append:
            lines.append(append + '\n')

        text = ''.join(lines)
        if self.options.get('tab-width'):
            text = text.expandtabs(self.options['tab-width'])
        retnode = nodes.literal_block(text, text, source=filename)
        set_source_info(self, retnode)
        if diffsource:  # if diff is set, set udiff
            retnode['language'] = 'udiff'
        if 'language' in self.options:
            retnode['language'] = self.options['language']
        retnode['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options or \
                             'lineno-match' in self.options
        retnode['classes'] += self.options.get('class', [])
        extra_args = retnode['highlight_args'] = {}
        if hl_lines is not None:
            extra_args['hl_lines'] = hl_lines
        extra_args['linenostart'] = linenostart
        env.note_dependency(rel_filename)

        caption = self.options.get('caption')
        if caption is not None:
            if not caption:
                caption = self.arguments[0]
            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.
        self.add_name(retnode)

        return [retnode]
Ejemplo n.º 37
0
def test_parselinenos():
    assert parselinenos('1,2,3', 10) == [0, 1, 2]
    assert parselinenos('4, 5, 6', 10) == [3, 4, 5]
    assert parselinenos('-4', 10) == [0, 1, 2, 3]
    assert parselinenos('7-9', 10) == [6, 7, 8]
    assert parselinenos('7-', 10) == [6, 7, 8, 9]
    assert parselinenos('1,7-', 10) == [0, 6, 7, 8, 9]
    assert parselinenos('7-7', 10) == [6]
    assert parselinenos('11-', 10) == [10]
    with pytest.raises(ValueError):
        parselinenos('1-2-3', 10)
    with pytest.raises(ValueError):
        parselinenos('abc-def', 10)
    with pytest.raises(ValueError):
        parselinenos('-', 10)
    with pytest.raises(ValueError):
        parselinenos('3-1', 10)
Ejemplo n.º 38
0
Archivo: code.py Proyecto: xcore/xdoc
    def run(self):        
        document = self.state.document
        filename = self.arguments[0]
        if not document.settings.file_insertion_enabled:
            return [document.reporter.warning('File insertion disabled',
                                              line=self.lineno)]
        env = document.settings.env
        fns = []
 
        docdir = path.dirname(env.doc2path(env.docname, base=None))
        dirs = [docdir,os.path.join(docdir,'_build','.sources')]
        if os.path.exists(os.path.join('_build','.sources')):
            dirs += [os.path.join(docdir,'_build','.sources',x) for x in os.listdir(os.path.join('_build','.sources'))]

        #self.state.document.settings.env.config.include_search_dirs
       
        fns = [path.join(x, filename) for x in dirs]


#        print fns
#        if filename.startswith('/') or filename.startswith(os.sep):
#            rel_fn = filename[1:]
#        else:
#            docdir = path.dirname(env.doc2path(env.docname, base=None))
#            rel_fn = path.join(docdir, filename)

#        try:
#            fn = path.join(env.srcdir, rel_fn)
#        except UnicodeDecodeError:
            # the source directory is a bytestring with non-ASCII characters;
            # let's try to encode the rel_fn in the file system encoding
#            rel_fn = rel_fn.encode(sys.getfilesystemencoding())
#            fn = path.join(env.srcdir, rel_fn)

        if 'pyobject' in self.options and 'lines' in self.options:
            return [document.reporter.warning(
                'Cannot use both "pyobject" and "lines" options',
                line=self.lineno)]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        lines = None
        for fn in fns:
            if lines == None:
                try:
                    f = codecs.StreamReaderWriter(open(fn, 'U'),
                                                  codec_info[2], 
                                                  codec_info[3], 'strict')
                    lines = f.readlines()
                    f.close()
                except (IOError, OSError):
                    pass
                except UnicodeError:
                    return [document.reporter.warning(
                            'Encoding %r used for reading included file %r'
                            'seems to '
                            'be wrong, try giving an :encoding: option' %
                            (encoding, filename))]
                
        if lines == None:
                return [document.reporter.warning(
                'Include file %r not found or reading it failed' % filename,
                line=self.lineno)]

        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(fn, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [document.reporter.warning(
                    'Object named %r not found in include file %r' %
                    (objectname, filename), line=self.lineno)]
            else:
                lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError, err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            lines = [lines[i] for i in linelist]
Ejemplo n.º 39
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [
                document.reporter.warning('File insertion disabled',
                                          line=self.lineno)
            ]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(self.arguments[0])

        if 'pyobject' in self.options and 'lines' in self.options:
            return [
                document.reporter.warning(
                    'Cannot use both "pyobject" and "lines" options',
                    line=self.lineno)
            ]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        try:
            f = codecs.StreamReaderWriter(open(filename, 'rb'), codec_info[2],
                                          codec_info[3], 'strict')
            lines = f.readlines()
            f.close()
        except (IOError, OSError):
            return [
                document.reporter.warning(
                    'Include file %r not found or reading it failed' %
                    filename,
                    line=self.lineno)
            ]
        except UnicodeError:
            return [
                document.reporter.warning(
                    'Encoding %r used for reading included file %r seems to '
                    'be wrong, try giving an :encoding: option' %
                    (encoding, filename))
            ]

        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(filename, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [
                    document.reporter.warning(
                        'Object named %r not found in include file %r' %
                        (objectname, filename),
                        line=self.lineno)
                ]
            else:
                lines = lines[tags[objectname][1] - 1:tags[objectname][2] - 1]

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            # just ignore nonexisting lines
            nlines = len(lines)
            lines = [lines[i] for i in linelist if i < nlines]
            if not lines:
                return [
                    document.reporter.warning(
                        'Line spec %r: no lines pulled from include file %r' %
                        (linespec, filename),
                        line=self.lineno)
                ]

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                hl_lines = [x + 1 for x in parselinenos(linespec, len(lines))]
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        startafter = self.options.get('start-after')
        endbefore = self.options.get('end-before')
        prepend = self.options.get('prepend')
        append = self.options.get('append')
        if startafter is not None or endbefore is not None:
            use = not startafter
            res = []
            for line in lines:
                if not use and startafter and startafter in line:
                    use = True
                elif use and endbefore and endbefore in line:
                    use = False
                    break
                elif use:
                    res.append(line)
            lines = res

        if prepend:
            lines.insert(0, prepend + '\n')
        if append:
            lines.append(append + '\n')

        text = ''.join(lines)
        if self.options.get('tab-width'):
            text = text.expandtabs(self.options['tab-width'])
        retnode = nodes.literal_block(text, text, source=filename)
        set_source_info(self, retnode)
        if self.options.get('language', ''):
            retnode['language'] = self.options['language']
        if 'linenos' in self.options:
            retnode['linenos'] = True
        if hl_lines is not None:
            retnode['highlight_args'] = {'hl_lines': hl_lines}
        env.note_dependency(rel_filename)
        return [retnode]
Ejemplo n.º 40
0
    def run(self):
        retnodes = []
        document = self.state.document
        filename = self.arguments[0]

        for lang in self.langs:
            try:
                if not document.settings.file_insertion_enabled:
                    return [document.reporter.warning('File insertion disabled',
                                                      line=self.lineno)]
                env = document.settings.env

                prefix_path = [self.client_dir, self.rev, lang]
                mid_path = ['snippets']

                if lang == 'java':
                    mid_path = ['src', 'snippets']
                elif lang == 'csharp':
                    mid_path = ['scenarios', 'snippets']

                suffix_path = [filename + '.' + self.lang_extensions[lang]]
                fn = os.path.join(*(prefix_path + mid_path + suffix_path))
                f = codecs.open(fn, 'rU')
                lines = f.readlines()
                f.close()
                linespec = self.options.get('lines')
                if linespec is not None:
                    try:
                        linelist = parselinenos(linespec, len(lines))
                    except ValueError, err:
                        return [document.reporter.warning(str(err), line=self.lineno)]
                    lines = [lines[i] for i in linelist]

                startafter = self.options.get('start-after')
                endbefore = self.options.get('end-before')
                if startafter is not None or endbefore is not None:
                    use = not startafter
                    res = []
                    for line in lines:
                        if not use and startafter and startafter in line:
                            use = True
                        elif use and endbefore and endbefore in line:
                            use = False
                            break
                        elif use:
                            res.append(line)
                    lines = res

                text = ''.join(lines)
                retnode = nodes.literal_block(text, text, source=fn)
                retnode.line = 1
                if lang == 'curl':
                    retnode['language'] = 'bash'
                else:
                    retnode['language'] = lang
                if 'linenos' in self.options:
                    retnode['linenos'] = True
                document.settings.env.note_dependency(fn)
                retnodes.append(retnode)
            except (IOError, OSError):
                document.reporter.warning(
                    'Include file {0} for {1} not found or reading it failed'.format(filename, lang),
                    line=self.lineno)
                pass
Ejemplo n.º 41
0
def test_parselinenos():
    assert parselinenos('1,2,3', 10) == [0, 1, 2]
    assert parselinenos('4, 5, 6', 10) == [3, 4, 5]
    assert parselinenos('-4', 10) == [0, 1, 2, 3]
    assert parselinenos('7-9', 10) == [6, 7, 8]
    assert parselinenos('7-', 10) == [6, 7, 8, 9]
    assert parselinenos('1,7-', 10) == [0, 6, 7, 8, 9]
    assert parselinenos('7-7', 10) == [6]
    assert parselinenos('11-', 10) == [10]
    with pytest.raises(ValueError):
        parselinenos('1-2-3', 10)
    with pytest.raises(ValueError):
        parselinenos('abc-def', 10)
    with pytest.raises(ValueError):
        parselinenos('-', 10)
    with pytest.raises(ValueError):
        parselinenos('3-1', 10)
Ejemplo n.º 42
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [
                document.reporter.warning('File insertion disabled',
                                          line=self.lineno)
            ]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(self.arguments[0])

        if 'pyobject' in self.options and 'lines' in self.options:
            return [
                document.reporter.warning(
                    'Cannot use both "pyobject" and "lines" options',
                    line=self.lineno)
            ]

        if 'lineno-match' in self.options and 'lineno-start' in self.options:
            return [
                document.reporter.warning(
                    'Cannot use both "lineno-match" and "lineno-start"',
                    line=self.lineno)
            ]

        if 'lineno-match' in self.options and \
           (set(['append', 'prepend']) & set(self.options.keys())):
            return [
                document.reporter.warning(
                    'Cannot use "lineno-match" and "append" or "prepend"',
                    line=self.lineno)
            ]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)

        lines = self.read_with_encoding(filename, document, codec_info,
                                        encoding)
        if lines and not isinstance(lines[0], string_types):
            return lines

        diffsource = self.options.get('diff')
        if diffsource is not None:
            tmp, fulldiffsource = env.relfn2path(diffsource)

            difflines = self.read_with_encoding(fulldiffsource, document,
                                                codec_info, encoding)
            if not isinstance(difflines[0], string_types):
                return difflines
            diff = unified_diff(difflines, lines, diffsource,
                                self.arguments[0])
            lines = list(diff)

        linenostart = self.options.get('lineno-start', 1)
        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(filename, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [
                    document.reporter.warning(
                        'Object named %r not found in include file %r' %
                        (objectname, filename),
                        line=self.lineno)
                ]
            else:
                lines = lines[tags[objectname][1] - 1:tags[objectname][2] - 1]
                if 'lineno-match' in self.options:
                    linenostart = tags[objectname][1]

        linespec = self.options.get('lines')
        if linespec:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]

            if 'lineno-match' in self.options:
                # make sure the line list is not "disjoint".
                previous = linelist[0]
                for line_number in linelist[1:]:
                    if line_number == previous + 1:
                        previous = line_number
                        continue
                    return [
                        document.reporter.warning(
                            'Cannot use "lineno-match" with a disjoint set of '
                            '"lines"',
                            line=self.lineno)
                    ]
                linenostart = linelist[0] + 1
            # just ignore non-existing lines
            lines = [lines[i] for i in linelist if i < len(lines)]
            if not lines:
                return [
                    document.reporter.warning(
                        'Line spec %r: no lines pulled from include file %r' %
                        (linespec, filename),
                        line=self.lineno)
                ]

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                hl_lines = [x + 1 for x in parselinenos(linespec, len(lines))]
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        startafter = self.options.get('start-after')
        endbefore = self.options.get('end-before')
        if startafter is not None or endbefore is not None:
            use = not startafter
            res = []
            for line_number, line in enumerate(lines):
                if not use and startafter and startafter in line:
                    if 'lineno-match' in self.options:
                        linenostart += line_number + 1
                    use = True
                elif use and endbefore and endbefore in line:
                    break
                elif use:
                    res.append(line)
            lines = res

        prepend = self.options.get('prepend')
        if prepend:
            lines.insert(0, prepend + '\n')

        append = self.options.get('append')
        if append:
            lines.append(append + '\n')

        text = ''.join(lines)
        if self.options.get('tab-width'):
            text = text.expandtabs(self.options['tab-width'])
        retnode = nodes.literal_block(text, text, source=filename)
        set_source_info(self, retnode)
        if diffsource:  # if diff is set, set udiff
            retnode['language'] = 'udiff'
        if 'language' in self.options:
            retnode['language'] = self.options['language']
        retnode['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options or \
                             'lineno-match' in self.options
        retnode['classes'] += self.options.get('class', [])
        extra_args = retnode['highlight_args'] = {}
        if hl_lines is not None:
            extra_args['hl_lines'] = hl_lines
        extra_args['linenostart'] = linenostart
        env.note_dependency(rel_filename)

        caption = self.options.get('caption')
        if caption is not None:
            if not caption:
                caption = self.arguments[0]
            self.options.setdefault('name',
                                    nodes.fully_normalize_name(caption))
            retnode = container_wrapper(self, retnode, caption)

        # retnode will be note_implicit_target that is linked from caption and numref.
        # when options['name'] is provided, it should be primary ID.
        self.add_name(retnode)

        return [retnode]
Ejemplo n.º 43
0
class IncludeFrags(Directive):
    """
    Like ``.. include:: :literal:``, but only warns if the include file is
    not found, and does not raise errors.  Also has several options for
    selecting what to include.
    """

    has_content = False
    required_arguments = 1
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {
        'linenos': directives.flag,
        'tab-width': int,
        'language': directives.unchanged_required,
        'encoding': directives.encoding,
        'pyobject': directives.unchanged_required,
        'lines': directives.unchanged_required,
        'start-after': directives.unchanged_required,
        'end-before': directives.unchanged_required,
        'prepend': directives.unchanged_required,
        'append': directives.unchanged_required,
        'emphasize-lines': directives.unchanged_required,
        'fragment': directives.unchanged,
    }

    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [
                document.reporter.warning('File insertion disabled',
                                          line=self.lineno)
            ]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(
            os.path.join('/' + env.config.includefrags_base_dir,
                         self.arguments[0]))  ## !!!

        if 'pyobject' in self.options and 'lines' in self.options:
            return [
                document.reporter.warning(
                    'Cannot use both "pyobject" and "lines" options',
                    line=self.lineno)
            ]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        f = None
        try:
            f = codecs.StreamReaderWriter(open(filename, 'rb'), codec_info[2],
                                          codec_info[3], 'strict')
            lines = f.readlines()
        except (IOError, OSError):
            return [
                document.reporter.warning(
                    'Include file %r not found or reading it failed' %
                    filename,
                    line=self.lineno)
            ]
        except UnicodeError:
            return [
                document.reporter.warning(
                    'Encoding %r used for reading included file %r seems to '
                    'be wrong, try giving an :encoding: option' %
                    (encoding, filename))
            ]
        finally:
            if f is not None:
                f.close()

        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(filename, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [
                    document.reporter.warning(
                        'Object named %r not found in include file %r' %
                        (objectname, filename),
                        line=self.lineno)
                ]
            else:
                lines = lines[tags[objectname][1] - 1:tags[objectname][2] - 1]

        fragment = self.options.get('fragment')
        if fragment is not None:
            result = []
            key = None
            active = False
            for line in lines:
                line = line.rstrip(
                )  # Strip line ending and trailing whitespace.
                line += '\n'  # add back line ending
                if line.strip().startswith('//![') and line.strip().endswith(
                        ']'):
                    key = line.strip()[4:-1].strip()
                    if key == fragment:
                        active = not active
                        continue
                if active:
                    result.append(line)
            while result and not result[-1].strip():
                result.pop()
            lines = result

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError, err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            # just ignore nonexisting lines
            nlines = len(lines)
            lines = [lines[i] for i in linelist if i < nlines]
            if not lines:
                return [
                    document.reporter.warning(
                        'Line spec %r: no lines pulled from include file %r' %
                        (linespec, filename),
                        line=self.lineno)
                ]

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                hl_lines = [x + 1 for x in parselinenos(linespec, len(lines))]
            except ValueError, err:
                return [document.reporter.warning(str(err), line=self.lineno)]
Ejemplo n.º 44
0
def run(self):
    document = self.state.document
    filename = self.arguments[0]
    #print filename
    if not document.settings.file_insertion_enabled:
        return [
            document.reporter.warning('File insertion disabled',
                                      line=self.lineno)
        ]
    env = document.settings.env
    if filename.startswith('/') or filename.startswith(os.sep):
        rel_fn = filename[1:]
    else:
        docdir = path.dirname(env.doc2path(env.docname, base=None))
        rel_fn = path.normpath(path.join(docdir, filename))
    fn = path.join(env.srcdir, rel_fn)

    if 'pyobject' in self.options and 'lines' in self.options:
        return [
            document.reporter.warning(
                'Cannot use both "pyobject" and "lines" options',
                line=self.lineno)
        ]

    encoding = self.options.get('encoding', env.config.source_encoding)
    try:
        f = codecs.open(fn, 'rU', encoding)
        lines = f.readlines()
        f.close()
        # 去掉编码指示
        if fn.endswith(".py") and lines[0].startswith(
                "#") and "coding" in lines[0]:
            lines = lines[1:]
        # 去掉文档说明
        if fn.endswith(".py"):
            if lines[0].startswith('"""'):
                for lineno, line in enumerate(lines[1:]):
                    if line.strip().endswith('"""'):
                        lines = lines[lineno + 2:]
                        break
        # 去掉每行末尾空格
        for i in xrange(len(lines)):
            lines[i] = lines[i].rstrip() + "\n"

    except (IOError, OSError):
        return [
            document.reporter.warning(
                'Include file %r not found or reading it failed' % filename,
                line=self.lineno)
        ]
    except UnicodeError:
        return [
            document.reporter.warning(
                'Encoding %r used for reading included file %r seems to '
                'be wrong, try giving an :encoding: option' %
                (encoding, filename))
        ]

    objectname = self.options.get('pyobject')
    if objectname is not None:
        from sphinx.pycode import ModuleAnalyzer
        analyzer = ModuleAnalyzer.for_file(fn, '')
        tags = analyzer.find_tags()
        if objectname not in tags:
            return [
                document.reporter.warning(
                    'Object named %r not found in include file %r' %
                    (objectname, filename),
                    line=self.lineno)
            ]
        else:
            lines = lines[tags[objectname][1] - 1:tags[objectname][2] - 1]

    linespec = self.options.get('lines')
    if linespec is not None:
        try:
            linelist = parselinenos(linespec, len(lines))
        except ValueError, err:
            return [document.reporter.warning(str(err), line=self.lineno)]
        lines = [lines[i] for i in linelist]
Ejemplo n.º 45
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [document.reporter.warning('File insertion disabled',
                                              line=self.lineno)]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(self.arguments[0])

        if 'pyobject' in self.options and 'lines' in self.options:
            return [document.reporter.warning(
                'Cannot use both "pyobject" and "lines" options',
                line=self.lineno)]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        f = None
        try:
            f = codecs.StreamReaderWriter(open(filename, 'rb'),
                    codec_info[2], codec_info[3], 'strict')
            lines = f.readlines()
            if 'dedent' in self.options:
                for i in range(0, len(lines)):
                    if len(lines[i]) <= self.options['dedent']:
                        lines[i] = lines[i][len(lines[i]) - 1:]
                    else:
                        lines[i] = lines[i][self.options['dedent']:]
        except (IOError, OSError):
            return [document.reporter.warning(
                'Include file %r not found or reading it failed' % filename,
                line=self.lineno)]
        except UnicodeError:
            return [document.reporter.warning(
                'Encoding %r used for reading included file %r seems to '
                'be wrong, try giving an :encoding: option' %
                (encoding, filename))]
        finally:
            if f is not None:
                f.close()

        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(filename, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [document.reporter.warning(
                    'Object named %r not found in include file %r' %
                    (objectname, filename), line=self.lineno)]
            else:
                lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            # just ignore nonexisting lines
            nlines = len(lines)
            lines = [lines[i] for i in linelist if i < nlines]
            if not lines:
                return [document.reporter.warning(
                    'Line spec %r: no lines pulled from include file %r' %
                    (linespec, filename), line=self.lineno)]

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                hl_lines = [x+1 for x in parselinenos(linespec, len(lines))]
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        startafter = self.options.get('start-after')
        endbefore  = self.options.get('end-before')
        prepend    = self.options.get('prepend')
        append     = self.options.get('append')
        if startafter is not None or endbefore is not None:
            use = not startafter
            res = []
            for line in lines:
                if not use and startafter and startafter in line:
                    use = True
                elif use and endbefore and endbefore in line:
                    use = False
                    break
                elif use:
                    res.append(line)
            lines = res

        if prepend:
           lines.insert(0, prepend + '\n')
        if append:
           lines.append(append + '\n')

        text = ''.join(lines)
        if self.options.get('tab-width'):
            text = text.expandtabs(self.options['tab-width'])
        retnode = nodes.literal_block(text, text, source=filename)
        set_source_info(self, retnode)
        if self.options.get('language', ''):
            retnode['language'] = self.options['language']
        retnode['linenos'] = 'linenos' in self.options or \
                             'lineno-start' in self.options
        filename = self.options.get('filename')
        if filename is not None:
            if not filename:
                filename = self.arguments[0]
            retnode['filename'] = filename
        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)
        return [retnode]
Ejemplo n.º 46
0
    def run(self):
        retnodes = []
        document = self.state.document
        filename = self.arguments[0]

        for lang in self.langs:
            try:
                if not document.settings.file_insertion_enabled:
                    return [
                        document.reporter.warning('File insertion disabled',
                                                  line=self.lineno)
                    ]
                env = document.settings.env

                prefix_path = [self.client_dir, self.rev, lang]
                mid_path = ['snippets']

                if lang == 'java':
                    mid_path = ['src', 'snippets']
                elif lang == 'csharp':
                    mid_path = ['scenarios', 'snippets']

                suffix_path = [filename + '.' + self.lang_extensions[lang]]
                fn = os.path.join(*(prefix_path + mid_path + suffix_path))
                f = codecs.open(fn, 'rU')
                lines = f.readlines()
                f.close()
                linespec = self.options.get('lines')
                if linespec is not None:
                    try:
                        linelist = parselinenos(linespec, len(lines))
                    except ValueError, err:
                        return [
                            document.reporter.warning(str(err),
                                                      line=self.lineno)
                        ]
                    lines = [lines[i] for i in linelist]

                startafter = self.options.get('start-after')
                endbefore = self.options.get('end-before')
                if startafter is not None or endbefore is not None:
                    use = not startafter
                    res = []
                    for line in lines:
                        if not use and startafter and startafter in line:
                            use = True
                        elif use and endbefore and endbefore in line:
                            use = False
                            break
                        elif use:
                            res.append(line)
                    lines = res

                text = ''.join(lines)
                retnode = nodes.literal_block(text, text, source=fn)
                retnode.line = 1
                if lang == 'curl':
                    retnode['language'] = 'bash'
                else:
                    retnode['language'] = lang
                if 'linenos' in self.options:
                    retnode['linenos'] = True
                document.settings.env.note_dependency(fn)
                retnodes.append(retnode)
            except (IOError, OSError):
                document.reporter.warning(
                    'Include file {0} for {1} not found or reading it failed'.
                    format(filename, lang),
                    line=self.lineno)
                pass
Ejemplo n.º 47
0
    def run(self):
        # type: () -> List[nodes.Node]
        document = self.state.document
        code = '\n'.join(self.content)
        location = self.state_machine.get_source_and_line(self.lineno)

        linespec = self.options.get('emphasize-lines')
        if linespec:
            try:
                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') %
                                   (nlines, self.options['emphasize-lines']),
                                   location=location)

                hl_lines = [x + 1 for x in hl_lines if x < nlines]
            except ValueError as err:
                return [document.reporter.warning(err, line=self.lineno)]
        else:
            hl_lines = None

        if 'dedent' in self.options:
            location = self.state_machine.get_source_and_line(self.lineno)
            lines = code.split('\n')
            lines = dedent_lines(lines, self.options['dedent'], location=location)
            code = '\n'.join(lines)

        literal = nodes.literal_block(code, code)  # type: nodes.Element
        if 'linenos' in self.options or 'lineno-start' in self.options:
            literal['linenos'] = True
        literal['classes'] += self.options.get('class', [])
        if self.arguments:
            # highlight language specified
            literal['language'] = self.arguments[0]
            literal['force_highlighting'] = True
        else:
            # no highlight language specified.  Then this directive refers the current
            # highlight setting via ``highlight`` directive or ``highlight_language``
            # configuration.
            literal['language'] = self.env.temp_data.get('highlight_language',
                                                         self.config.highlight_language)
            literal['force_highlighting'] = False
        extra_args = literal['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']
        set_source_info(self, literal)

        caption = self.options.get('caption')
        if caption:
            try:
                literal = container_wrapper(self, literal, caption)
            except ValueError as exc:
                return [document.reporter.warning(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.
        self.add_name(literal)

        return [literal]
Ejemplo n.º 48
0
def run(self):
    document = self.state.document
    filename = self.arguments[0]
    #print filename
    if not document.settings.file_insertion_enabled:
        return [document.reporter.warning('File insertion disabled',
                                          line=self.lineno)]
    env = document.settings.env
    if filename.startswith('/') or filename.startswith(os.sep):
        rel_fn = filename[1:]
    else:
        docdir = path.dirname(env.doc2path(env.docname, base=None))
        rel_fn = path.normpath(path.join(docdir, filename))
    fn = path.join(env.srcdir, rel_fn)

    if 'pyobject' in self.options and 'lines' in self.options:
        return [document.reporter.warning(
            'Cannot use both "pyobject" and "lines" options',
            line=self.lineno)]

    encoding = self.options.get('encoding', env.config.source_encoding)
    try:
        f = codecs.open(fn, 'rU', encoding)
        lines = f.readlines()
        f.close()
        # 去掉编码指示
        if fn.endswith(".py") and lines[0].startswith("#") and "coding" in lines[0]:
            lines = lines[1:]
        # 去掉文档说明
        if fn.endswith(".py"):
            if lines[0].startswith('"""'):
                for lineno, line in enumerate(lines[1:]):
                    if line.strip().endswith('"""'):
                        lines = lines[lineno+2:]
                        break
        # 去掉每行末尾空格
        for i in range(len(lines)):
            lines[i] = lines[i].rstrip() + "\n"
        
    except (IOError, OSError):
        return [document.reporter.warning(
            'Include file %r not found or reading it failed' % filename,
            line=self.lineno)]
    except UnicodeError:
        return [document.reporter.warning(
            'Encoding %r used for reading included file %r seems to '
            'be wrong, try giving an :encoding: option' %
            (encoding, filename))]

    objectname = self.options.get('pyobject')
    if objectname is not None:
        from sphinx.pycode import ModuleAnalyzer
        analyzer = ModuleAnalyzer.for_file(fn, '')
        tags = analyzer.find_tags()
        if objectname not in tags:
            return [document.reporter.warning(
                'Object named %r not found in include file %r' %
                (objectname, filename), line=self.lineno)]
        else:
            lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

    linespec = self.options.get('lines')
    if linespec is not None:
        try:
            linelist = parselinenos(linespec, len(lines))
        except (ValueError, err):
            return [document.reporter.warning(str(err), line=self.lineno)]
        lines = [lines[i] for i in linelist]

    startafter = self.options.get('start-after')
    endbefore = self.options.get('end-before')
    if startafter is not None or endbefore is not None:
        use = not startafter
        res = []
        for line in lines:
            if not use and startafter in line:
                use = True
            elif use and endbefore in line:
                use = False
                break
            elif use:
                res.append(line)
        lines = res
        
    section = self.options.get("section")
    if section is not None:
        section = "###%s###" % section
        print (section)
        use = False
        res = []
        for line in lines:
            if not use and section in line:
                use = True
                continue
            elif use and section in line:
                use = False
                break
            if use:
                res.append(line)
        lines = res
        indent = len(lines[0]) - len(lines[0].lstrip())
        for i,line in enumerate(lines):
            lines[i] = line[indent:]
      
    text = replace_number_label(''.join(lines))
    text = re.sub(r"(?s)#<\?(.*?)>.+?#<\?/>", lambda mo:u"#<?>%s" % mo.group(1), text)
    #text = (u"#程序文件:%s\n" % filename) + text
    retnode = nodes.literal_block(text, text, source=fn)
    retnode.line = 1
    if self.options.get('language', ''):
        retnode['language'] = self.options['language']
    if 'linenos' in self.options:
        retnode['linenos'] = True
    document.settings.env.note_dependency(rel_fn)
    #print "LiteralInclude hacked"
    return [retnode]    
Ejemplo n.º 49
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [document.reporter.warning("File insertion disabled", line=self.lineno)]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(
            os.path.join("/" + env.config.includefrags_base_dir, self.arguments[0])
        )  ## !!!

        if "pyobject" in self.options and "lines" in self.options:
            return [document.reporter.warning('Cannot use both "pyobject" and "lines" options', line=self.lineno)]

        encoding = self.options.get("encoding", env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        f = None
        try:
            f = codecs.StreamReaderWriter(open(filename, "rb"), codec_info[2], codec_info[3], "strict")
            lines = f.readlines()
        except (IOError, OSError):
            return [
                document.reporter.warning("Include file %r not found or reading it failed" % filename, line=self.lineno)
            ]
        except UnicodeError:
            return [
                document.reporter.warning(
                    "Encoding %r used for reading included file %r seems to "
                    "be wrong, try giving an :encoding: option" % (encoding, filename)
                )
            ]
        finally:
            if f is not None:
                f.close()

        objectname = self.options.get("pyobject")
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer

            analyzer = ModuleAnalyzer.for_file(filename, "")
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [
                    document.reporter.warning(
                        "Object named %r not found in include file %r" % (objectname, filename), line=self.lineno
                    )
                ]
            else:
                lines = lines[tags[objectname][1] - 1 : tags[objectname][2] - 1]

        fragment = self.options.get("fragment")
        if fragment is not None:
            active = False
            needle = "FRAGMENT(%s)" % fragment
            result = []
            for line in lines:
                if "FRAGMENT(" in line and needle not in line:
                    active = False
                elif needle in line:
                    active = True
                    continue
                if active:
                    result.append(line)
            while result and not result[-1].strip():
                result.pop()
            lines = result

        linespec = self.options.get("lines")
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError, err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            # just ignore nonexisting lines
            nlines = len(lines)
            lines = [lines[i] for i in linelist if i < nlines]
            if not lines:
                return [
                    document.reporter.warning(
                        "Line spec %r: no lines pulled from include file %r" % (linespec, filename), line=self.lineno
                    )
                ]
Ejemplo n.º 50
0
    def run(self):
        document = self.state.document
        filename = self.arguments[0]
        if not document.settings.file_insertion_enabled:
            return [document.reporter.warning('File insertion disabled',
                                              line=self.lineno)]
        env = document.settings.env
        if filename.startswith('/') or filename.startswith(os.sep):
            rel_fn = filename[1:]
        else:
            docdir = path.dirname(env.doc2path(env.docname, base=None))
            rel_fn = path.join(docdir, filename)
        try:
            fn = path.join(env.srcdir, rel_fn)
        except UnicodeDecodeError:
            # the source directory is a bytestring with non-ASCII characters;
            # let's try to encode the rel_fn in the file system encoding
            rel_fn = rel_fn.encode(sys.getfilesystemencoding())
            fn = path.join(env.srcdir, rel_fn)

        if 'pyobject' in self.options and 'lines' in self.options:
            return [document.reporter.warning(
                'Cannot use both "pyobject" and "lines" options',
                line=self.lineno)]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        try:
            f = codecs.StreamReaderWriter(open(fn, 'U'),
                    codec_info[2], codec_info[3], 'strict')
            lines = f.readlines()
            f.close()
        except (IOError, OSError):
            return [document.reporter.warning(
                'Include file %r not found or reading it failed' % filename,
                line=self.lineno)]
        except UnicodeError:
            return [document.reporter.warning(
                'Encoding %r used for reading included file %r seems to '
                'be wrong, try giving an :encoding: option' %
                (encoding, filename))]

        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(fn, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [document.reporter.warning(
                    'Object named %r not found in include file %r' %
                    (objectname, filename), line=self.lineno)]
            else:
                lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError, err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            lines = [lines[i] for i in linelist]
Ejemplo n.º 51
0
def run(self):
    document = self.state.document
    filename = self.arguments[0]
    #print filename
    if not document.settings.file_insertion_enabled:
        return [document.reporter.warning('File insertion disabled',
                                          line=self.lineno)]
    env = document.settings.env
    if filename.startswith('/') or filename.startswith(os.sep):
        rel_fn = filename[1:]
    else:
        docdir = path.dirname(env.doc2path(env.docname, base=None))
        rel_fn = path.normpath(path.join(docdir, filename))
    fn = path.join(env.srcdir, rel_fn)

    if 'pyobject' in self.options and 'lines' in self.options:
        return [document.reporter.warning(
            'Cannot use both "pyobject" and "lines" options',
            line=self.lineno)]

    encoding = self.options.get('encoding', env.config.source_encoding)
    try:
        f = codecs.open(fn, 'rU', encoding)
        lines = f.readlines()
        f.close()
        # 去掉编码指示
        if fn.endswith(".py") and lines[0].startswith("#") and "coding" in lines[0]:
            lines = lines[1:]
        # 去掉文档说明
        if fn.endswith(".py"):
            if lines[0].startswith('"""'):
                for lineno, line in enumerate(lines[1:]):
                    if line.strip().endswith('"""'):
                        lines = lines[lineno+2:]
                        break
        # 去掉每行末尾空格
        for i in xrange(len(lines)):
            lines[i] = lines[i].rstrip() + "\n"
        
    except (IOError, OSError):
        return [document.reporter.warning(
            'Include file %r not found or reading it failed' % filename,
            line=self.lineno)]
    except UnicodeError:
        return [document.reporter.warning(
            'Encoding %r used for reading included file %r seems to '
            'be wrong, try giving an :encoding: option' %
            (encoding, filename))]

    objectname = self.options.get('pyobject')
    if objectname is not None:
        from sphinx.pycode import ModuleAnalyzer
        analyzer = ModuleAnalyzer.for_file(fn, '')
        tags = analyzer.find_tags()
        if objectname not in tags:
            return [document.reporter.warning(
                'Object named %r not found in include file %r' %
                (objectname, filename), line=self.lineno)]
        else:
            lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

    linespec = self.options.get('lines')
    if linespec is not None:
        try:
            linelist = parselinenos(linespec, len(lines))
        except ValueError, err:
            return [document.reporter.warning(str(err), line=self.lineno)]
        lines = [lines[i] for i in linelist]
Ejemplo n.º 52
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [
                document.reporter.warning('File insertion disabled',
                                          line=self.lineno)
            ]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(
            os.path.join('/' + env.config.includefrags_base_dir,
                         self.arguments[0]))  ## !!!

        if 'pyobject' in self.options and 'lines' in self.options:
            return [
                document.reporter.warning(
                    'Cannot use both "pyobject" and "lines" options',
                    line=self.lineno)
            ]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        f = None
        try:
            f = codecs.StreamReaderWriter(open(filename, 'rb'), codec_info[2],
                                          codec_info[3], 'strict')
            lines = f.readlines()
        except (IOError, OSError):
            return [
                document.reporter.warning(
                    'Include file %r not found or reading it failed' %
                    filename,
                    line=self.lineno)
            ]
        except UnicodeError:
            return [
                document.reporter.warning(
                    'Encoding %r used for reading included file %r seems to '
                    'be wrong, try giving an :encoding: option' %
                    (encoding, filename))
            ]
        finally:
            if f is not None:
                f.close()

        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(filename, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [
                    document.reporter.warning(
                        'Object named %r not found in include file %r' %
                        (objectname, filename),
                        line=self.lineno)
                ]
            else:
                lines = lines[tags[objectname][1] - 1:tags[objectname][2] - 1]

        fragment = self.options.get('fragment')
        if fragment is not None:
            result = []
            key = None
            active = False
            for line in lines:
                line = line.rstrip(
                )  # Strip line ending and trailing whitespace.
                line += '\n'  # add back line ending
                if line.strip().startswith('//![') and line.strip().endswith(
                        ']'):
                    key = line.strip()[4:-1].strip()
                    if key == fragment:
                        active = not active
                        continue
                if active:
                    result.append(line)
            while result and not result[-1].strip():
                result.pop()
            lines = result

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError, err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            # just ignore nonexisting lines
            nlines = len(lines)
            lines = [lines[i] for i in linelist if i < nlines]
            if not lines:
                return [
                    document.reporter.warning(
                        'Line spec %r: no lines pulled from include file %r' %
                        (linespec, filename),
                        line=self.lineno)
                ]
Ejemplo n.º 53
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [document.reporter.warning("File insertion disabled", line=self.lineno)]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(self.arguments[0])

        if "pyobject" in self.options and "lines" in self.options:
            return [document.reporter.warning('Cannot use both "pyobject" and "lines" options', line=self.lineno)]

        encoding = self.options.get("encoding", env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        f = None
        try:
            f = codecs.StreamReaderWriter(open(filename, "rb"), codec_info[2], codec_info[3], "strict")
            lines = f.readlines()
        except (IOError, OSError):
            return [
                document.reporter.warning("Include file %r not found or reading it failed" % filename, line=self.lineno)
            ]
        except UnicodeError:
            return [
                document.reporter.warning(
                    "Encoding %r used for reading included file %r seems to "
                    "be wrong, try giving an :encoding: option" % (encoding, filename)
                )
            ]
        finally:
            if f is not None:
                f.close()

        objectname = self.options.get("pyobject")
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer

            analyzer = ModuleAnalyzer.for_file(filename, "")
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [
                    document.reporter.warning(
                        "Object named %r not found in include file %r" % (objectname, filename), line=self.lineno
                    )
                ]
            else:
                lines = lines[tags[objectname][1] - 1 : tags[objectname][2] - 1]

        linespec = self.options.get("lines")
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            # just ignore nonexisting lines
            nlines = len(lines)
            lines = [lines[i] for i in linelist if i < nlines]
            if not lines:
                return [
                    document.reporter.warning(
                        "Line spec %r: no lines pulled from include file %r" % (linespec, filename), line=self.lineno
                    )
                ]

        linespec = self.options.get("emphasize-lines")
        if linespec:
            try:
                hl_lines = [x + 1 for x in parselinenos(linespec, len(lines))]
            except ValueError as err:
                return [document.reporter.warning(str(err), line=self.lineno)]
        else:
            hl_lines = None

        startafter = self.options.get("start-after")
        endbefore = self.options.get("end-before")
        prepend = self.options.get("prepend")
        append = self.options.get("append")
        if startafter is not None or endbefore is not None:
            use = not startafter
            res = []
            for line in lines:
                if not use and startafter and startafter in line:
                    use = True
                elif use and endbefore and endbefore in line:
                    use = False
                    break
                elif use:
                    res.append(line)
            lines = res

        if prepend:
            lines.insert(0, prepend + "\n")
        if append:
            lines.append(append + "\n")

        text = "".join(lines)
        if self.options.get("tab-width"):
            text = text.expandtabs(self.options["tab-width"])
        retnode = nodes.literal_block(text, text, source=filename)
        set_source_info(self, retnode)
        if self.options.get("language", ""):
            retnode["language"] = self.options["language"]
        if "linenos" in self.options:
            retnode["linenos"] = True
        if hl_lines is not None:
            retnode["highlight_args"] = {"hl_lines": hl_lines}
        env.note_dependency(rel_filename)
        return [retnode]
Ejemplo n.º 54
0
    def run(self):
        document = self.state.document
        if not document.settings.file_insertion_enabled:
            return [document.reporter.warning('File insertion disabled',
                                              line=self.lineno)]
        env = document.settings.env
        rel_filename, filename = env.relfn2path(os.path.join(
            '/' + env.config.includefrags_base_dir, self.arguments[0])) ## !!!

        if 'pyobject' in self.options and 'lines' in self.options:
            return [document.reporter.warning(
                'Cannot use both "pyobject" and "lines" options',
                line=self.lineno)]

        encoding = self.options.get('encoding', env.config.source_encoding)
        codec_info = codecs.lookup(encoding)
        f = None
        try:
            f = codecs.StreamReaderWriter(open(filename, 'rb'),
                    codec_info[2], codec_info[3], 'strict')
            lines = f.readlines()
        except (IOError, OSError):
            return [document.reporter.warning(
                'Include file %r not found or reading it failed' % filename,
                line=self.lineno)]
        except UnicodeError:
            return [document.reporter.warning(
                'Encoding %r used for reading included file %r seems to '
                'be wrong, try giving an :encoding: option' %
                (encoding, filename))]
        finally:
            if f is not None:
                f.close()

        objectname = self.options.get('pyobject')
        if objectname is not None:
            from sphinx.pycode import ModuleAnalyzer
            analyzer = ModuleAnalyzer.for_file(filename, '')
            tags = analyzer.find_tags()
            if objectname not in tags:
                return [document.reporter.warning(
                    'Object named %r not found in include file %r' %
                    (objectname, filename), line=self.lineno)]
            else:
                lines = lines[tags[objectname][1]-1 : tags[objectname][2]-1]

        fragment = self.options.get('fragment')
        if fragment is not None:
            result = []
            key = None
            active = False
            for line in lines:
                line = line.rstrip()  # Strip line ending and trailing whitespace.
                line += '\n' # add back line ending
                if line.strip().startswith('//![') and line.strip().endswith(']'):
                    key = line.strip()[4:-1].strip()
                    if key == fragment:
                        active = not active
                        continue
                if active:
                    result.append(line)
            while result and not result[-1].strip():
                result.pop()
            lines = result

        linespec = self.options.get('lines')
        if linespec is not None:
            try:
                linelist = parselinenos(linespec, len(lines))
            except ValueError, err:
                return [document.reporter.warning(str(err), line=self.lineno)]
            # just ignore nonexisting lines
            nlines = len(lines)
            lines = [lines[i] for i in linelist if i < nlines]
            if not lines:
                return [document.reporter.warning(
                    'Line spec %r: no lines pulled from include file %r' %
                    (linespec, filename), line=self.lineno)]