예제 #1
0
     def read(self):
         if self.source_path.endswith(".rst"):
             return FileInput.read(self)

         data = FileInput.read(self).split("\n")
         newdata = []

         line = data.pop(0)
         while data:
             if len(line.strip()) == 0:
                 newdata.append('')
                 line = data.pop(0)

             elif line.startswith("#"):
                 while data and line.startswith('#'):
                     newdata.append(line[2:])
                     line = data.pop(0)

             else:
                 newdata.extend(['', '::', ''])

                 while data and not line.startswith('#'):
                     newdata.append('    ' + line)
                     line = data.pop(0)

                 newdata.append('')

         return "\n".join(newdata)
예제 #2
0
파일: io.py 프로젝트: ezc/sphinx-1
    def read(self):
        # type: () -> unicode
        def get_parser_type(source_path):
            # type: (unicode) -> Tuple[unicode]
            for suffix in self.env.config.source_parsers:
                if source_path.endswith(suffix):
                    parser_class = self.env.config.source_parsers[suffix]
                    if isinstance(parser_class, string_types):
                        parser_class = import_object(
                            parser_class,
                            'source parser')  # type: ignore  # NOQA
                    return parser_class.supported
            else:
                return ('restructuredtext', )

        data = FileInput.read(self)
        if self.app:
            arg = [data]
            self.app.emit('source-read', self.env.docname, arg)
            data = arg[0]
        docinfo, data = split_docinfo(data)
        if 'restructuredtext' in get_parser_type(self.source_path):
            if self.env.config.rst_epilog:
                data = data + '\n' + self.env.config.rst_epilog + '\n'
            if self.env.config.rst_prolog:
                data = self.env.config.rst_prolog + '\n' + data
        return docinfo + data
예제 #3
0
 def read(self):
     data = FileInput.read(self)
     if self.app:
         arg = [data]
         self.app.emit('source-read', self.env.docname, arg)
         data = arg[0]
     if self.env.config.rst_epilog:
         data = data + '\n' + self.env.config.rst_epilog + '\n'
     if self.env.config.rst_prolog:
         data = self.env.config.rst_prolog + '\n' + data
     return data
예제 #4
0
    def read(self):
        # type: () -> unicode
        """Reads the contents from file.

        After reading, it emits Sphinx event ``source-read``.
        """
        data = FileInput.read(self)

        # emit source-read event
        arg = [data]
        self.app.emit('source-read', self.env.docname, arg)
        return arg[0]
예제 #5
0
    def read(self):
        # type: () -> unicode
        """Reads the contents from file.

        After reading, it emits Sphinx event ``source-read``.
        """
        data = FileInput.read(self)

        # emit source-read event
        arg = [data]
        self.app.emit('source-read', self.env.docname, arg)
        return arg[0]
예제 #6
0
파일: io.py 프로젝트: evhub/sphinx
 def read(self):
     data = FileInput.read(self)
     if self.app:
         arg = [data]
         self.app.emit('source-read', self.env.docname, arg)
         data = arg[0]
     docinfo, data = split_docinfo(data)
     if self.env.config.rst_epilog:
         data = data + '\n' + self.env.config.rst_epilog + '\n'
     if self.env.config.rst_prolog:
         data = self.env.config.rst_prolog + '\n' + data
     return docinfo + data
예제 #7
0
    def patch(self, out_filename=None):
        """Apply patches and write changes to specified file.

        Args:
            out_filename: Output filename. If not set then input one is used.
        """
        in_ = FileInput(source_path=self._filename, encoding=self._encoding)
        content = in_.read()
        content = self._patcher.patch(content)
        content = '\n'.join(content)

        out = FileOutput(destination_path=out_filename or self._filename,
                         encoding=self._encoding)
        out.write(content)
예제 #8
0
    def run(self):
        if not self.state.document.settings.file_insertion_enabled:
            raise self.warning('"%s" directive disabled.' % self.name)

        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        source_dir = os.path.dirname(os.path.abspath(source))
        file_path = rst.directives.path(self.arguments[0])
        absolute_path = os.path.normpath(os.path.join(source_dir, file_path))
        path = relative_path(None, absolute_path)

        settings = self.state.document.settings

        try:
            settings.record_dependencies.add(path)
            include_file = FileInput(
                source_path=path,
                encoding=self.options.get("encoding", settings.input_encoding),
                error_handler=settings.input_encoding_error_handler,
            )
        except UnicodeEncodeError as error:
            raise self.severe(f'Problems with "{self.name}" directive path:\n'
                              f'Cannot encode input file path "{path}" '
                              "(wrong locale?).")
        except IOError as error:
            raise self.severe(
                f'Problems with "{self.name}" directive path:\n{ErrorString(error)}.'
            )

        startline = self.options.get("start-line", None)
        endline = self.options.get("end-line", None)

        try:
            if startline or endline is not None:
                lines = include_file.readlines()
                rawtext = "".join(lines[startline:endline])
            else:
                rawtext = include_file.read()
        except UnicodeError as error:
            raise self.severe(
                f'Problem with "{self.name}" directive:\n{ErrorString(error)}')

        document = new_document(absolute_path, settings)
        MystParser().parse(rawtext, document)

        return document.children
예제 #9
0
파일: io.py 프로젝트: Anlim/sphinx
    def read(self):
        def get_parser_type(source_path):
            for suffix in self.env.config.source_parsers:
                if source_path.endswith(suffix):
                    parser_class = self.env.config.source_parsers[suffix]
                    if isinstance(parser_class, string_types):
                        parser_class = import_object(parser_class, 'source parser')
                    return parser_class.supported
            else:
                return ('restructuredtext',)

        data = FileInput.read(self)
        if self.app:
            arg = [data]
            self.app.emit('source-read', self.env.docname, arg)
            data = arg[0]
        docinfo, data = split_docinfo(data)
        if 'restructuredtext' in get_parser_type(self.source_path):
            if self.env.config.rst_epilog:
                data = data + '\n' + self.env.config.rst_epilog + '\n'
            if self.env.config.rst_prolog:
                data = self.env.config.rst_prolog + '\n' + data
        return docinfo + data
예제 #10
0
파일: io.py 프로젝트: hagenw/sphinx
    def read(self):
        # type: () -> unicode
        def get_parser_type(source_path):
            # type: (unicode) -> Tuple[unicode]
            for suffix, parser_class in iteritems(self.app.registry.get_source_parsers()):
                if source_path.endswith(suffix):
                    if isinstance(parser_class, string_types):
                        parser_class = import_object(parser_class, 'source parser')  # type: ignore  # NOQA
                    return parser_class.supported
            return ('restructuredtext',)

        data = FileInput.read(self)
        if self.app:
            arg = [data]
            self.app.emit('source-read', self.env.docname, arg)
            data = arg[0]
        docinfo, data = split_docinfo(data)
        if 'restructuredtext' in get_parser_type(self.source_path):
            if self.env.config.rst_epilog:
                data = data + '\n' + self.env.config.rst_epilog + '\n'
            if self.env.config.rst_prolog:
                data = self.env.config.rst_prolog + '\n' + data
        return docinfo + data
예제 #11
0
 def run(self):
     if not self.state.document.settings.file_insertion_enabled:
         raise self.warning('"%s" directive disabled.' % self.name)
     path = directives.path(self.arguments[0])
     # ALL the included files are relative to the repository root.
     # we need to remove absolute paths
     if path.startswith('/'):
         raise self.severe('Problem with "%s" directive path:\npath ' \
                           'should be relative' % self.name)
     encoding = self.options.get(
         'encoding', self.state.document.settings.input_encoding)
     tab_width = self.options.get('tab-width',
                                  self.state.document.settings.tab_width)
     try:
         self.state.document.settings.record_dependencies.add(path)
         include_file = FileInput(
             source=BlohgFile(path), encoding=encoding,
             error_handler=(self.state.document.settings.\
                            input_encoding_error_handler))
     except IOError as error:
         raise self.severe('Problems with "%s" directive path:\n%s.' %
                           (self.name, ErrorString(error)))
     startline = self.options.get('start-line', None)
     endline = self.options.get('end-line', None)
     try:
         if startline or (endline is not None):
             lines = include_file.readlines()
             rawtext = ''.join(lines[startline:endline])
         else:
             rawtext = include_file.read()
     except UnicodeError as error:
         raise self.severe('Problem with "%s" directive:\n%s' %
                           (self.name, ErrorString(error)))
     # start-after/end-before: no restrictions on newlines in match-text,
     # and no restrictions on matching inside lines vs. line boundaries
     after_text = self.options.get('start-after', None)
     if after_text:
         # skip content in rawtext before *and incl.* a matching text
         after_index = rawtext.find(after_text)
         if after_index < 0:
             raise self.severe('Problem with "start-after" option of "%s" '
                               'directive:\nText not found.' % self.name)
         rawtext = rawtext[after_index + len(after_text):]
     before_text = self.options.get('end-before', None)
     if before_text:
         # skip content in rawtext after *and incl.* a matching text
         before_index = rawtext.find(before_text)
         if before_index < 0:
             raise self.severe('Problem with "end-before" option of "%s" '
                               'directive:\nText not found.' % self.name)
         rawtext = rawtext[:before_index]
     if 'literal' in self.options:
         # Convert tabs to spaces, if `tab_width` is positive.
         if tab_width >= 0:
             text = rawtext.expandtabs(tab_width)
         else:
             text = rawtext
         literal_block = nodes.literal_block(rawtext, text, source=path)
         literal_block.line = 1
         return [literal_block]
     else:
         include_lines = statemachine.string2lines(rawtext,
                                                   tab_width,
                                                   convert_whitespace=1)
         self.state_machine.insert_input(include_lines, path)
         return []