Beispiel #1
0
    def parse(self, inputstring, document):
        """Parse the nblink file.

        Adds the linked file as a dependency, read the file, and
        pass the content to the nbshpinx.NotebookParser.
        """
        link = json.loads(inputstring)
        env = document.settings.env
        source_dir = os.path.dirname(env.doc2path(env.docname))

        abs_path = os.path.normpath(os.path.join(source_dir, link['path']))
        path = utils.relative_path(None, abs_path)
        path = nodes.reprunicode(path)

        extra_media = link.get('extra-media', None)
        if extra_media:
            source_file = env.doc2path(env.docname)
            collect_extra_media(extra_media, source_file, path, document)

        register_dependency(path, document)

        target_root = env.config.nbsphinx_link_target_root
        target = utils.relative_path(target_root, abs_path)
        target = nodes.reprunicode(target).replace(os.path.sep, '/')
        env.metadata[env.docname]['nbsphinx-link-target'] = target

        # Copy parser from nbsphinx for our cutom format
        try:
            formats = env.config.nbsphinx_custom_formats
        except AttributeError:
            pass
        else:
            formats.setdefault(
                '.nblink',
                lambda s: nbformat.reads(s, as_version=_ipynbversion))

        try:
            include_file = io.FileInput(source_path=path, encoding='utf8')
        except UnicodeEncodeError as error:
            raise NotebookError(u'Problems with linked notebook "%s" path:\n'
                                'Cannot encode input file path "%s" '
                                '(wrong locale?).' %
                                (env.docname, SafeString(path)))
        except IOError as error:
            raise NotebookError(
                u'Problems with linked notebook "%s" path:\n%s.' %
                (env.docname, ErrorString(error)))

        try:
            rawtext = include_file.read()
        except UnicodeError as error:
            raise NotebookError(u'Problem with linked notebook "%s":\n%s' %
                                (env.docname, ErrorString(error)))
        return super(LinkedNotebookParser, self).parse(rawtext, document)
Beispiel #2
0
    def __parser_helper(self, link, source_dir, document, env):
        """Helper method for adding a single notebook as a linked dependency.

        Adds the linked file as a dependency, read the file, and pass the content to
        the nbshpinx.NotebookParser.
        """

        abs_path = os.path.normpath(os.path.join(source_dir, link["path"]))
        path = utils.relative_path(None, abs_path)
        path = nodes.reprunicode(path)

        extra_media = link.get("extra-media", None)
        if extra_media:
            source_file = env.doc2path(env.docname)
            collect_extra_media(extra_media, source_file, path, document)

        register_dependency(path, document)

        target_root = env.config.nbsphinx_link_target_root
        target = utils.relative_path(target_root, abs_path)
        target = nodes.reprunicode(target).replace(os.path.sep, "/")
        env.metadata[env.docname]["nbsphinx-link-target"] = target

        # Copy parser from nbsphinx for our cutom format
        try:
            formats = env.config.nbsphinx_custom_formats
        except AttributeError:
            pass
        else:
            formats.setdefault(
                ".nblink",
                lambda s: nbformat.reads(s, as_version=_ipynbversion))

        try:
            include_file = io.FileInput(source_path=path, encoding="utf8")
        except UnicodeEncodeError as error:
            raise NotebookError(u'Problems with linked notebook "%s" path:\n'
                                'Cannot encode input file path "%s" '
                                "(wrong locale?)." %
                                (env.docname, SafeString(path)))
        except IOError as error:
            raise NotebookError(
                u'Problems with linked notebook "%s" path:\n%s.' %
                (env.docname, ErrorString(error)))

        try:
            rawtext = include_file.read()
        except UnicodeError as error:
            raise NotebookError(u'Problem with linked notebook "%s":\n%s' %
                                (env.docname, ErrorString(error)))

        super(LinkedNotebookParser, self).parse(rawtext, document)
Beispiel #3
0
    def run(self):
        """Most of this method is from ``docutils.parser.rst.Directive``.

        docutils version: 0.12
        """
        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))
        path = rst.directives.path(self.arguments[0])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)

        # get options (currently not use directive-specific options)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)

        # open the including file
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        except IOError as error:
            raise self.severe('Problems with "%s" directive path:\n%s.' %
                              (self.name, ErrorString(error)))

        # read from the file
        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)))

        config = self.state.document.settings.env.config
        converter = M2R(no_underscore_emphasis=config.no_underscore_emphasis,
                        parse_relative_links=config.m2r_parse_relative_links,
                        anonymous_references=config.m2r_anonymous_references,
                        disable_inline_math=config.m2r_disable_inline_math)
        include_lines = statemachine.string2lines(converter(rawtext),
                                                  tab_width,
                                                  convert_whitespace=True)
        self.state_machine.insert_input(include_lines, path)
        return []
    def run(self):
        env = self.state.document.settings.env

        openapi_path = self.arguments[0]
        _, openapi_path = env.relfn2path(openapi_path)
        openapi_path = utils.relative_path(None, openapi_path)
        openapi_path = nodes.reprunicode(openapi_path)

        self.state.document.settings.record_dependencies.add(openapi_path)
        with open(openapi_path, 'r') as f:
            openapi = OpenAPI.load(f)

        try:
            rendered = OPENAPI_TEMPLATE.render({
                'tags': openapi.tags.values(),
                'servers': openapi.data['servers']
            })
        except Exception as error:
            raise self.severe('Failed to render template: {}'.format(ErrorString(error)))

        rendered_lines = statemachine.string2lines(rendered, 4, convert_whitespace=1)
        self.state_machine.insert_input(rendered_lines, '')

        # Allow people to use :ref: to link to resources. Sphinx offers two ways
        # of doing this: (1) lots of arcane boilerplate, or (2) hacking our way through.
        # Let's hope this doesn't break...
        stddomain = env.get_domain('std')
        labels = stddomain.data['labels']
        for method, path, methods in openapi.resources():
            method_hash = methods[method]['hash']
            if method_hash not in labels:
                labels[method_hash] = (env.docname, method_hash, '{} {}'.format(method.upper(), path))

        return []
Beispiel #5
0
 def run(self):
     """Include a file as part of the content of this reST file."""
     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))
     path = directives.path(self.arguments[0])
     if path.startswith('<') and path.endswith('>'):
         path = os.path.join(self.standard_include_path, path[1:-1])
     path = os.path.normpath(os.path.join(source_dir, path))
     path = utils.relative_path(None, path)
     path = nodes.reprunicode(path)
     encoding = self.options.get(
         'encoding', self.state.document.settings.input_encoding)
     e_handler=self.state.document.settings.input_encoding_error_handler
     tab_width = self.options.get(
         'tab-width', self.state.document.settings.tab_width)
     try:
         self.state.document.settings.record_dependencies.add(path)
         include_file = io.FileInput(source_path=path,
                                     encoding=encoding,
                                     error_handler=e_handler)
     except UnicodeEncodeError, error:
         raise self.severe(u'Problems with "%s" directive path:\n'
                           'Cannot encode input file path "%s" '
                           '(wrong locale?).' %
                           (self.name, SafeString(path)))
Beispiel #6
0
 def read(self, path):
     """Read the file."""
     if not self.state.document.settings.file_insertion_enabled:
         raise self.warning('"%s" directive disabled.' % self.name)
     
     path = directives.path(path)
     if False:
         source = self.state_machine.input_lines.source(
             self.lineno - self.state_machine.input_offset - 1)
         source_dir = os.path.dirname(os.path.abspath(source))
         path = os.path.normpath(os.path.join(source_dir, path))
     path = utils.relative_path(None, path)
     path = nodes.reprunicode(path)
     encoding = self.options.get(
         'encoding', self.state.document.settings.input_encoding)
     e_handler=self.state.document.settings.input_encoding_error_handler
     try:
         self.state.document.settings.record_dependencies.add(path)
         include_file = io.FileInput(source_path=path,
                                     encoding=encoding,
                                     error_handler=e_handler)
     except UnicodeEncodeError, error:
         raise self.severe(u'Problems with "%s" directive path:\n'
                           'Cannot encode input file path "%s" '
                           '(wrong locale?).' %
                           (self.name, SafeString(path)))
Beispiel #7
0
 def open_file(self, path):
     """Open a file and register it as a dependency."""
     path = os.path.normpath(path)
     path = utils.relative_path(None, path)
     path = nodes.reprunicode(path)
     self.state.document.settings.record_dependencies.add(path)
     return open(path, 'r')
Beispiel #8
0
    def run(self):
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        source_dir = os.path.dirname(os.path.abspath(source))
        path = directives.path(self.arguments[0])
        path = os.path.normpath(os.path.join(source_dir, path))

        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        img_folder = None
        img_folder_os = None
        if "image_folder" in self.options:
            # This is extremly messy..
            # To be able to test if file exist in path we need to use img_path_os
            # But that cannot be used for the .. image:: tag, instead we need to use the raw option!
            img_folder_os = os.path.normpath(
                os.path.join(source_dir, self.options["image_folder"]))
            img_folder = self.options["image_folder"]
        rawtext = parse_examples(path, img_folder, img_folder_os)

        include_lines = statemachine.string2lines(
            rawtext,
            self.state.document.settings.tab_width,
            convert_whitespace=True)

        self.state_machine.insert_input(include_lines, path)
        return []
 def run(self):
     """Include a reST file as part of the content of this reST file."""
     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))
     path = directives.path(self.arguments[0])
     if path.startswith("<") and path.endswith(">"):
         path = os.path.join(self.standard_include_path, path[1:-1])
     path = os.path.normpath(os.path.join(source_dir, path))
     path = utils.relative_path(None, path)
     path = nodes.reprunicode(path)
     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 = io.FileInput(
             source_path=path,
             encoding=encoding,
             error_handler=(self.state.document.settings.input_encoding_error_handler),
             handle_io_errors=None,
         )
     except IOError, error:
         raise self.severe(
             'Problems with "%s" directive path:\n%s: %s.' % (self.name, error.__class__.__name__, str(error))
         )
Beispiel #10
0
   def visit_section(self,node):

       try:
         nodename = reprunicode(node['names'][-1]).lower()

         try:
               idanchor = node.__dict__['attributes']['ids'][0]
               contentlink = content_link % (idanchor, nodename )
               self.contentsbody += contentlink 
         except:
               print("cannot get content info for node "+nodename)

         if nodename.find("related")  == 0 or \
            nodename.find("other")    == 0 :
               node['classes'].append('related-section')
               self.in_related = 1
               self.related_title = nodename
         elif nodename.find("see also") == 0:
               node['classes'].append('see-also')
               self.in_related = 1
               self.related_title = nodename
       except:
          pass 

       HTMLTranslator.visit_section(self,node)
Beispiel #11
0
 def run(self):
     """Include a reST file as part of the content of this reST file."""
     #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))
     path = directives.path(self.arguments[0])
     if path.startswith('<') and path.endswith('>'):
         path = os.path.join(self.standard_include_path, path[1:-1])
     path = os.path.normpath(os.path.join(source_dir, path))
     path = utils.relative_path(None, path)
     path = nodes.reprunicode(path)
     encoding = self.options.get(
         'encoding', self.state.document.settings.input_encoding)
     tab_width = self.options.get(
         'tab-width', self.state.document.settings.tab_width)
     startline = self.options.get('start-line', None)
     endline = self.options.get('end-line', None)
     after_text = self.options.get('start-after', None)
     before_text = self.options.get('end-before', None)
     self.state.document.settings.record_dependencies.add(path)
     return [include('', refuri=path, encoding=encoding, tab_width=tab_width, 
             startline=startline, endline=endline,
             start_after=after_text, end_before=before_text)]
Beispiel #12
0
 def run(self):
     """Include a file as part of the content of this reST file."""
     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))
     path = directives.path(self.arguments[0])
     if path.startswith('<') and path.endswith('>'):
         path = os.path.join(self.standard_include_path, path[1:-1])
     path = os.path.normpath(os.path.join(source_dir, path))
     path = utils.relative_path(None, path)
     path = nodes.reprunicode(path)
     encoding = self.options.get(
         'encoding', self.state.document.settings.input_encoding)
     e_handler = self.state.document.settings.input_encoding_error_handler
     tab_width = self.options.get('tab-width',
                                  self.state.document.settings.tab_width)
     try:
         self.state.document.settings.record_dependencies.add(path)
         include_file = io.FileInput(source_path=path,
                                     encoding=encoding,
                                     error_handler=e_handler)
     except UnicodeEncodeError, error:
         raise self.severe(u'Problems with "%s" directive path:\n'
                           'Cannot encode input file path "%s" '
                           '(wrong locale?).' %
                           (self.name, SafeString(path)))
Beispiel #13
0
    def run(self):
        """Most of this method is from ``docutils.parser.rst.Directive``.
        docutils version: 0.12
        """
        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))
        path = rst.directives.path(self.arguments[0])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)

        # get options (currently not use directive-specific options)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler

        # open the including file
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        except OSError as error:
            raise self.severe('Problems with "%s" directive path:\n%s.' %
                              (self.name, ErrorString(error)))

        # read from the file
        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)))

        class CustomCommonMarkParser(CommonMarkParser):
            """Temporary workaround to remove multiple build warnings caused by upstream bug.
            See https://github.com/readthedocs/recommonmark/issues/177 for details.
            """
            def visit_document(self, node):
                pass

        doc = utils.new_document(self.arguments[0])
        md_parser = CustomCommonMarkParser()
        md_parser.parse(rawtext, doc)
        return [*doc.children]
Beispiel #14
0
def decode_path(path):
    """
    Decode file/path string. Return `nodes.reprunicode` object.

    Convert to Unicode without the UnicodeDecode error of the
    implicit 'ascii:strict' decoding.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path)
Beispiel #15
0
 def _get_source_path(self):
     # Taken from `docutils.parsers.rst.directives.misc.Include`
     source = self.state_machine.input_lines.source(
         self.lineno - self.state_machine.input_offset - 1)
     source_dir = os.path.dirname(os.path.abspath(source))
     path = directives.path(self.arguments[0])
     path = os.path.normpath(os.path.join(source_dir, path))
     path = relative_path(None, path)
     return nodes.reprunicode(path)
Beispiel #16
0
def decode_path(path):
    """
    Decode file/path string. Return `nodes.reprunicode` object.

    Convert to Unicode without the UnicodeDecode error of the
    implicit 'ascii:strict' decoding.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError:  # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path)
Beispiel #17
0
    def run(self):
        """Include a file as part of the content of this reST file."""
        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))

        path = directives.path(self.arguments[0])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)

        encoding = self.options.get("encoding", self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get("tab-width", self.state.document.settings.tab_width)

        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe(
                'Problems with "%s" directive path:\n'
                'Cannot encode input file path "%s" '
                "(wrong locale?)." % (self.name, SafeString(path))
            )
        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)))

        include_lines = statemachine.string2lines(rawtext, tab_width, convert_whitespace=True)

        # default lexer to 'text'
        lexer = self.options.get("lexer", "text")

        self.options["source"] = path
        codeblock = Pygments(
            self.name,
            [lexer],  # arguments
            {},  # no options for this directive
            include_lines,  # content
            self.lineno,
            self.content_offset,
            self.block_text,
            self.state,
            self.state_machine,
        )
        return codeblock.run()
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError:  # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path)
Beispiel #19
0
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path)
Beispiel #20
0
    def run(self):
        """Most of this method is from ``docutils.parser.rst.Directive``.

        docutils version: 0.12
        """
        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))
        path = rst.directives.path(self.arguments[0])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)

        # get options (currently not use directive-specific options)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get(
            'tab-width', self.state.document.settings.tab_width)

        # open the inclding file
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        except IOError as error:
            raise self.severe('Problems with "%s" directive path:\n%s.' %
                              (self.name, ErrorString(error)))

        # read from the file
        try:
            rawtext = include_file.read()
        except UnicodeError as error:
            raise self.severe('Problem with "%s" directive:\n%s' %
                              (self.name, ErrorString(error)))

        config = self.state.document.settings.env.config
        converter = M2R(no_underscore_emphasis=config.no_underscore_emphasis)
        include_lines = statemachine.string2lines(converter(rawtext),
                                                  tab_width,
                                                  convert_whitespace=True)
        self.state_machine.insert_input(include_lines, path)
        return []
Beispiel #21
0
 def get_path(self):
     """docstring for get_path"""
     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))
     path = directives.path(self.arguments[0])
     if not self.options.get('absolute', True):
         path = os.path.normpath(os.path.join(source_dir, path))
     path = utils.relative_path(None, path)
     path = nodes.reprunicode(path)
     encoding = self.options.get(
         'encoding', self.state.document.settings.input_encoding)
     globpath = self.options.get("glob", "*.pro")
     return os.path.join(path, globpath)
Beispiel #22
0
 def get_pelican_path(self, path):
     """ determine actual absolute path in Pelican context """
     if path.startswith('/') or path.startswith(path_separator):
         # absolute path => relative to Pelican working directory
         path = join(self.generator.path, path[1:])
     else:
         # relative path => relative to directory of
         # source file using the directive
         source, line = self.state_machine.get_source_and_line(
             self.lineno
         )
         source_dir = dirname(abspath(source))
         path = join(source_dir, path)
     path = nodes.reprunicode(path)
     path = abspath(path)
     return path
def hdl_diagram_name(srcpath, srclineno, hdl_path):
    srcdir, srcfile = path.split(srcpath)
    srcbase, srcext = path.splitext(srcfile)

    hdl_path = path.normpath(path.join(srcdir, hdl_path))
    hdl_path = utils.relative_path(None, hdl_path)
    hdl_path = nodes.reprunicode(hdl_path)

    hdl_path_segments = [hdl_path]
    while hdl_path_segments[0]:
        a, b = path.split(hdl_path_segments[0])
        hdl_path_segments[0:1] = [a, b]

    hdl_file, hdl_ext = path.splitext(hdl_path_segments[-1])

    return '-'.join([srcbase, str(srclineno)] + hdl_path_segments[1:-1] +
                    [hdl_file], )
Beispiel #24
0
def decode_path(path):
    """
    Decode file/path string. Return `nodes.reprunicode` object.

    Provides a conversion to unicode without the UnicodeDecode error of the
    implicit 'ascii:strict' decoding.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except UnicodeDecodeError:
        path = path.decode('utf-8', 'strict')
        try:
            path = path.decode(sys.getfilesystemencoding(), 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path)
def verilog_diagram_name(srcpath, srclineno, verilog_path):
    srcdir, srcfile = path.split(srcpath)
    srcbase, srcext = path.splitext(srcfile)

    verilog_path = path.normpath(path.join(srcdir, verilog_path))
    verilog_path = utils.relative_path(None, verilog_path)
    verilog_path = nodes.reprunicode(verilog_path)

    verilog_path_segments = [verilog_path]
    while verilog_path_segments[0]:
        a, b = path.split(verilog_path_segments[0])
        verilog_path_segments[0:1] = [a, b]

    verilog_file, verilog_ext = path.splitext(verilog_path_segments[-1])

    return '-'.join([srcbase, str(srclineno)] + verilog_path_segments[1:-1] +
                    [verilog_file], )
Beispiel #26
0
def decode_path(path):
    """
    Decode file/path string. Return `nodes.reprunicode` object.

    Provides a conversion to unicode without the UnicodeDecode error of the
    implicit 'ascii:strict' decoding.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except UnicodeDecodeError:
        path = path.decode('utf-8', 'strict')
        try:
            path = path.decode(sys.getfilesystemencoding(), 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path)
Beispiel #27
0
    def run(self):
        env = self.state.document.settings.env

        openapi_path = self.arguments[0]
        _, openapi_path = env.relfn2path(openapi_path)
        openapi_path = utils.relative_path(None, openapi_path)
        openapi_path = nodes.reprunicode(openapi_path)

        self.state.document.settings.record_dependencies.add(openapi_path)
        with open(openapi_path, 'r') as f:
            openapi = OpenAPI.load(f)

        try:
            rendered = OPENAPI_TEMPLATE.render({
                'tags':
                openapi.tags.values(),
                'servers':
                openapi.data['servers']
            })
        except Exception as error:
            raise self.severe('Failed to render template: {}'.format(
                ErrorString(error)))

        rendered_lines = statemachine.string2lines(rendered,
                                                   4,
                                                   convert_whitespace=1)
        self.state_machine.insert_input(rendered_lines, '')

        # Allow people to use :ref: to link to resources. Sphinx offers two ways
        # of doing this: (1) lots of arcane boilerplate, or (2) hacking our way through.
        # Let's hope this doesn't break...
        stddomain = env.get_domain('std')
        labels = stddomain.data['labels']
        for method, path, methods in openapi.resources():
            method_hash = methods[method]['hash']
            if method_hash not in labels:
                labels[method_hash] = (env.docname, method_hash,
                                       '{} {}'.format(method.upper(), path))

        return []
Beispiel #28
0
    def run(self):
        """Include a file as part of the content of this reST file."""
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        source_dir = os.path.dirname(os.path.abspath(source))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' % (self.name, path))
        except IOError as error:
            raise self.severe('Problems with "%s" directive path:\n%s.' %
                              (self.name, error))
        path = self.options.get('path')
        try:
            data = json.loads(include_file.read())
            for item in path.split('/'):
                data = data[item]
            assert isinstance(data, str)
        except UnicodeError as error:
            raise self.severe('Problem with "%s" directive:\n%s' %
                              (self.name, error))

        if self.options.get('prepend'):
            data = f'{self.options.get("prepend")} {data}'
        self.state_machine.insert_input(data.split('\n'), path)
        return []
    def run(self):
        source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1)
        source_dir = os.path.dirname(os.path.abspath(source))
        path = directives.path(self.arguments[0])
        path = os.path.normpath(os.path.join(source_dir, path))
        
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        img_folder = None
        img_folder_os = None
        if "image_folder" in self.options:
            # This is extremly messy.. 
            # To be able to test if file exist in path we need to use img_path_os
            # But that cannot be used for the .. image:: tag, instead we need to use the raw option!
            img_folder_os = os.path.normpath(os.path.join(source_dir, self.options["image_folder"]))
            img_folder = self.options["image_folder"]
        rawtext = parse_examples(path, img_folder, img_folder_os)

        include_lines = statemachine.string2lines(rawtext, self.state.document.settings.tab_width,
                                                  convert_whitespace=True)
        
        self.state_machine.insert_input(include_lines, path)
        return []
    def run(self):
        env = self.state.document.settings.env

        if self.arguments[0].startswith('<') and \
           self.arguments[0].endswith('>'):
            # docutils "standard" includes, do not do path processing
            return BaseInclude.run(self)
        rel_filename, filename = env.relfn2path(self.arguments[0])
        self.arguments[0] = filename

        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))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        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 = io.FileInput(
                source_path=path, encoding=encoding,
                error_handler=(self.state.document.settings.\
                               input_encoding_error_handler),
                handle_io_errors=None)
        except IOError as error:
            raise self.severe('Problems with "%s" directive path:\n%s.' %
                              (self.name, 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)

            include_lines = preProcessLines(include_lines)

            self.state_machine.insert_input(include_lines, path)
            return []
Beispiel #31
0
    def run(self):
        ### Copy from include directive docutils
        """Include a file as part of the content of this reST file."""
        rel_filename, filename = self.env.relfn2path(self.arguments[0])
        self.arguments[0] = filename
        self.env.note_included(filename)
        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))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError:
            raise self.severe(u'Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        except IOError as error:
            raise self.severe(u'Problems with "%s" directive path:\n%s.' %
                              (self.name, 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:
            raise self.severe(u'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]

        include_lines = statemachine.string2lines(rawtext,
                                                  tab_width,
                                                  convert_whitespace=True)
        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,
                                                source=path,
                                                classes=self.options.get(
                                                    'class', []))
            literal_block.line = 1
            self.add_name(literal_block)
            if 'number-lines' in self.options:
                try:
                    startline = int(self.options['number-lines'] or 1)
                except ValueError:
                    raise self.error(':number-lines: with non-integer '
                                     'start value')
                endline = startline + len(include_lines)
                if text.endswith('\n'):
                    text = text[:-1]
                tokens = NumberLines([([], text)], startline, endline)
                for classes, value in tokens:
                    if classes:
                        literal_block += nodes.inline(value,
                                                      value,
                                                      classes=classes)
                    else:
                        literal_block += nodes.Text(value, value)
            else:
                literal_block += nodes.Text(text, text)
            return [literal_block]
        if 'code' in self.options:
            self.options['source'] = path
            codeblock = CodeBlock(
                self.name,
                [self.options.pop('code')],  # arguments
                self.options,
                include_lines,  # content
                self.lineno,
                self.content_offset,
                self.block_text,
                self.state,
                self.state_machine)
            return codeblock.run()

        new_include_lines = []
        for line in include_lines:
            for i in range(10):
                value = self.options.get(f'var{i}', '')
                if value == '':
                    line = re.sub('\s?{{\s?var' + str(i) + '\s?}}', value,
                                  line)
                else:
                    line = re.sub('{{\s?var' + str(i) + '\s?}}', value, line)
            new_include_lines.append(line)
        self.state_machine.insert_input(new_include_lines, path)
        return []
Beispiel #32
0
    def run(self):
        """Include a file as part of the content of this reST file."""
        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))

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

        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get(
            'tab-width', self.state.document.settings.tab_width)

        if re.match('^https*://', self.arguments[0]):
            rawtext = self.fetch_url(self.arguments[0])
            if startline or (endline is not None):
                rawtext = self.start_end_lines(rawtext, startline, endline)

        else:
            path = directives.path(self.arguments[0])
            path = os.path.normpath(os.path.join(source_dir, path))
            path = utils.relative_path(None, path)
            path = nodes.reprunicode(path)

            try:
                self.state.document.settings.record_dependencies.add(path)
                include_file = io.FileInput(source_path=path,
                                            encoding=encoding,
                                            error_handler=e_handler)
            except UnicodeEncodeError as error:
                raise self.severe('Problems with "%s" directive path:\n'
                                  'Cannot encode input file path "%s" '
                                  '(wrong locale?).' %
                                  (self.name, SafeString(path)))
            except IOError as error:
                    raise self.severe('Problems with "%s" directive '
                                      'path:\n%s.' %
                                      (self.name, ErrorString(error)))
            self.options['source'] = path

            if startline or (endline is not None):
                rawtext = include_file.read()
                rawtext = self.start_end_lines(rawtext, startline, endline)

        include_lines = statemachine.string2lines(rawtext, tab_width,
                                                  convert_whitespace=True)

        # default lexer to 'text'
        lexer = self.options.get('lexer', 'text')
        linenos = self.options.get('linenos', 'none')
        linenostart = self.options.get('linenostart', 1)

        codeblock = Pygments(self.name,
                             [lexer],  # arguments
                             { 'linenos': linenos, 'linenostart': linenostart },  # no options for this directive
                             include_lines,  # content
                             self.lineno,
                             self.content_offset,
                             self.block_text,
                             self.state,
                             self.state_machine)
        return codeblock.run()
    def run(self):

        # For code or literal include blocks we run the normal include
        if 'literal' in self.options or 'code' in self.options:
            return super(FormatedInclude, self).run()
        """Include a file as part of the content of this reST file."""
        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))

        rel_filename, filename = self.env.relfn2path(self.arguments[0])
        self.arguments[0] = filename
        self.env.note_included(filename)
        path = directives.path(self.arguments[0])

        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))

        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)

        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError:
            raise self.severe(u'Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        except IOError as error:
            raise self.severe(u'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(u'Problem with "%s" directive:\n%s' %
                              (self.name, ErrorString(error)))

        # Format input
        sub = StringSubstituter()
        config = self.state.document.settings.env.config
        sub.init_sub_strings(config)
        rawtext = sub.substitute(rawtext)

        # 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]

        include_lines = statemachine.string2lines(rawtext,
                                                  tab_width,
                                                  convert_whitespace=True)

        self.state_machine.insert_input(include_lines, path)
        return []
Beispiel #34
0
    def _run(self):  # pylint: disable=R
        """Include a file as part of the content of this reST file."""

        # HINT: I had to copy&paste the whole Include.run method. I'am not happy
        # with this, but due to security reasons, the Include.run method does
        # not allow absolute or relative pathnames pointing to locations *above*
        # the filesystem tree where the reST document is placed.

        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))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))

        # HINT: this is the only line I had to change / commented out:
        #path = utils.relative_path(None, path)

        path = nodes.reprunicode(path)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler=self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get(
            'tab-width', self.state.document.settings.tab_width)
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        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:
            rawtext = u''
            if startline or (endline is not None):
                lines = include_file.readlines()
                rawtext = u''.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]

        include_lines = statemachine.string2lines(rawtext, tab_width,
                                                  convert_whitespace=True)
        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, source=path,
                                                classes=self.options.get('class', []))
            literal_block.line = 1
            self.add_name(literal_block)
            if 'number-lines' in self.options:
                try:
                    startline = int(self.options['number-lines'] or 1)
                except ValueError:
                    raise self.error(':number-lines: with non-integer '
                                     'start value')
                endline = startline + len(include_lines)
                if text.endswith('\n'):
                    text = text[:-1]
                tokens = NumberLines([([], text)], startline, endline)
                for classes, value in tokens:
                    if classes:
                        literal_block += nodes.inline(value, value,
                                                      classes=classes)
                    else:
                        literal_block += nodes.Text(value, value)
            else:
                literal_block += nodes.Text(text, text)
            return [literal_block]
        if 'code' in self.options:
            self.options['source'] = path
            codeblock = CodeBlock(self.name,
                                  [self.options.pop('code')], # arguments
                                  self.options,
                                  include_lines, # content
                                  self.lineno,
                                  self.content_offset,
                                  self.block_text,
                                  self.state,
                                  self.state_machine)
            return codeblock.run()
        self.state_machine.insert_input(include_lines, path)
        return []
Beispiel #35
0
    def run(self):
        """Include a file as part of the content of this reST file."""
        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))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        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]

        # **Added code** from here...
        ##---------------------------
        # Only Sphinx has the ``env`` attribute.
        env = getattr(self.state.document.settings, 'env', None)

        # If the lexer is specified, include it.
        code_to_rest_options = {}
        lexer_alias = self.options.get('lexer')
        if lexer_alias:
            code_to_rest_options['alias'] = lexer_alias
        elif env:
            # If Sphinx is running, try getting a user-specified lexer from the Sphinx configuration.
            lfg = env.app.config.CodeChat_lexer_for_glob
            for glob, lexer_alias in lfg.items():
                if Path(path).match(glob):
                    code_to_rest_options['alias'] = lexer_alias

        # Translate the source code to reST.
        lexer = get_lexer(filename=path, code=rawtext, **code_to_rest_options)
        rawtext = code_to_rest_string(rawtext, lexer=lexer)

        # If Sphinx is running, insert the appropriate highlight directive.
        if env:
            rawtext = add_highlight_language(rawtext, lexer)
        ##------------
        # ... to here.

        include_lines = statemachine.string2lines(rawtext,
                                                  tab_width,
                                                  convert_whitespace=True)
        # **Deleted code**: Options for ``literal`` and ``code`` don't apply.
        self.state_machine.insert_input(include_lines, path)
        return []
Beispiel #36
0
    def run(self):
        """Include a file as part of the content of this reST file."""
        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))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler=self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get(
            'tab-width', self.state.document.settings.tab_width)
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        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]

        include_lines = statemachine.string2lines(rawtext, tab_width,
                                                  convert_whitespace=True)
        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, source=path,
                                    classes=self.options.get('class', []))
            literal_block.line = 1
            self.add_name(literal_block)
            if 'number-lines' in self.options:
                try:
                    startline = int(self.options['number-lines'] or 1)
                except ValueError:
                    raise self.error(':number-lines: with non-integer '
                                     'start value')
                endline = startline + len(include_lines)
                if text.endswith('\n'):
                    text = text[:-1]
                tokens = NumberLines([([], text)], startline, endline)
                for classes, value in tokens:
                    if classes:
                        literal_block += nodes.inline(value, value,
                                                      classes=classes)
                    else:
                        literal_block += nodes.Text(value, value)
            else:
                literal_block += nodes.Text(text, text)
            return [literal_block]
        if 'code' in self.options:
            self.options['source'] = path
            codeblock = CodeBlock(self.name,
                                  [self.options.pop('code')], # arguments
                                  self.options,
                                  include_lines, # content
                                  self.lineno,
                                  self.content_offset,
                                  self.block_text,
                                  self.state,
                                  self.state_machine)
            return codeblock.run()
        self.state_machine.insert_input(include_lines, path)
        return []
Beispiel #37
0
    def run(self):
        """
        Verbatim copy of docutils.parsers.rst.directives.misc.Include.run()
        that just calls to our Code instead of builtin CodeBlock but otherwise
        just passes it back to the parent implementation.
        """
        if not 'code' in self.options:
            return docutils.parsers.rst.directives.misc.Include.run(self)

        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        source_dir = os.path.dirname(os.path.abspath(source))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler=self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get(
            'tab-width', self.state.document.settings.tab_width)
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        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]

        include_lines = statemachine.string2lines(rawtext, tab_width,
                                                  convert_whitespace=True)

        self.options['source'] = path
        codeblock = Code(self.name,
                            [self.options.pop('code')], # arguments
                            self.options,
                            include_lines, # content
                            self.lineno,
                            self.content_offset,
                            self.block_text,
                            self.state,
                            self.state_machine)
        return codeblock.run()
Beispiel #38
0
    def run(self):
        """
        Verbatim copy of docutils.parsers.rst.directives.misc.Include.run()
        that just calls to our Code instead of builtin CodeBlock, is without
        the rarely useful :encoding:, :literal: and :name: options and adds
        support for :start-on:, empty :end-before: and :strip-prefix:.
        """
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        source_dir = os.path.dirname(os.path.abspath(source))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        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):]
        # Compared to start-after, this includes the matched line
        on_text = self.options.get('start-on', None)
        if on_text:
            on_index = rawtext.find('\n' + on_text)
            if on_index < 0:
                raise self.severe('Problem with "start-on" option of "%s" '
                                  'directive:\nText not found.' % self.name)
            rawtext = rawtext[on_index:]
        # Compared to builtin include directive, the end-before can be empty,
        # in which case it simply matches the first empty line (which is
        # usually end of the code block)
        before_text = self.options.get('end-before', None)
        if before_text is not None:
            # skip content in rawtext after *and incl.* a matching text
            if before_text == '':
                before_index = rawtext.find('\n\n')
            else:
                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]

        include_lines = statemachine.string2lines(rawtext,
                                                  tab_width,
                                                  convert_whitespace=True)

        # Strip a common prefix from all lines. Useful for example when
        # including a reST snippet that's embedded in a comment, or cutting
        # away excessive indentation. Can be wrapped in quotes in order to
        # avoid trailing whitespace in reST markup.
        if 'strip-prefix' in self.options and self.options['strip-prefix']:
            prefix = self.options['strip-prefix']
            if prefix[0] == prefix[-1] and prefix[0] in ['\'', '"']:
                prefix = prefix[1:-1]
            for i, line in enumerate(include_lines):
                if line.startswith(prefix):
                    include_lines[i] = line[len(prefix):]
                    # Strip the prefix also if the line is just the prefix alone,
                    # with trailing whitespace removed
                elif line.rstrip() == prefix.rstrip():
                    include_lines[i] = ''

        if 'code' in self.options:
            self.options['source'] = path
            # Don't convert tabs to spaces, if `tab_width` is negative:
            if tab_width < 0:
                include_lines = rawtext.splitlines()
            codeblock = Code(
                self.name,
                [self.options.pop('code')],  # arguments
                self.options,
                include_lines,  # content
                self.lineno,
                self.content_offset,
                self.block_text,
                self.state,
                self.state_machine)
            return codeblock.run()
        self.state_machine.insert_input(include_lines, path)
        return []
Beispiel #39
0
    def run(self):
        """Include a file as part of the content of this reST file."""
        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))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe(u'Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        except IOError as error:
            raise self.severe(u'Problems with "%s" directive path:\n%s.' %
                              (self.name, ErrorString(error)))

        # Get to-be-included content
        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(u'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]

        include_lines = statemachine.string2lines(rawtext,
                                                  tab_width,
                                                  convert_whitespace=True)
        for i, line in enumerate(include_lines):
            if len(line) > self.state.document.settings.line_length_limit:
                raise self.warning('"%s": line %d exceeds the'
                                   ' line-length-limit.' % (path, i + 1))

        if 'literal' in self.options:
            # Don't convert tabs to spaces, if `tab_width` is negative.
            if tab_width >= 0:
                text = rawtext.expandtabs(tab_width)
            else:
                text = rawtext
            literal_block = nodes.literal_block(rawtext,
                                                source=path,
                                                classes=self.options.get(
                                                    'class', []))
            literal_block.line = 1
            self.add_name(literal_block)
            if 'number-lines' in self.options:
                try:
                    startline = int(self.options['number-lines'] or 1)
                except ValueError:
                    raise self.error(':number-lines: with non-integer '
                                     'start value')
                endline = startline + len(include_lines)
                if text.endswith('\n'):
                    text = text[:-1]
                tokens = NumberLines([([], text)], startline, endline)
                for classes, value in tokens:
                    if classes:
                        literal_block += nodes.inline(value,
                                                      value,
                                                      classes=classes)
                    else:
                        literal_block += nodes.Text(value)
            else:
                literal_block += nodes.Text(text)
            return [literal_block]

        if 'code' in self.options:
            self.options['source'] = path
            # Don't convert tabs to spaces, if `tab_width` is negative:
            if tab_width < 0:
                include_lines = rawtext.splitlines()
            codeblock = CodeBlock(
                self.name,
                [self.options.pop('code')],  # arguments
                self.options,
                include_lines,  # content
                self.lineno,
                self.content_offset,
                self.block_text,
                self.state,
                self.state_machine)
            return codeblock.run()

        if 'parser' in self.options:
            parser = self.options['parser']()
            # parse into a new (dummy) document
            document = utils.new_document(path, self.state.document.settings)
            parser.parse('\n'.join(include_lines), document)
            return document.children

        # include as rST source
        #
        # Prevent circular inclusion:
        source = utils.relative_path(None, source)
        clip_options = (startline, endline, before_text, after_text)
        include_log = self.state.document.include_log
        if not include_log:  # new document:
            # log entries: (<source>, <clip-options>, <insertion end index>)
            include_log = [(source, (None, None, None, None), sys.maxsize / 2)]
        # cleanup: we may have passed the last inclusion(s):
        include_log = [
            entry for entry in include_log if entry[2] >= self.lineno
        ]
        if (path, clip_options) in [(pth, opt)
                                    for (pth, opt, e) in include_log]:
            raise self.warning(
                'circular inclusion in "%s" directive: %s' %
                (self.name,
                 ' < '.join([path] +
                            [pth for (pth, opt, e) in include_log[::-1]])))
        # include as input
        self.state_machine.insert_input(include_lines, path)
        # update include-log
        include_log.append((path, clip_options, self.lineno))
        self.state.document.include_log = [(pth, opt,
                                            e + len(include_lines) + 2)
                                           for (pth, opt, e) in include_log]
        return []
Beispiel #40
0
"""],
[u"""\
.. image:: picture.png
   :align: \xe4
""",
u"""\
<document source="test data">
    <system_message level="3" line="1" source="test data" type="ERROR">
        <paragraph>
            Error in "image" directive:
            invalid option value: (option: "align"; value: %s)
            "\xe4" unknown; choose from "top", "middle", "bottom", "left", "center", or "right".
        <literal_block xml:space="preserve">
            .. image:: picture.png
               :align: \xe4
""" % repr(reprunicode(u'\xe4'))],
["""
.. image:: test.png
   :target: Uppercase_

.. _Uppercase: http://docutils.sourceforge.net/
""",
"""\
<document source="test data">
    <reference name="Uppercase" refname="uppercase">
        <image uri="test.png">
    <target ids="uppercase" names="uppercase" refuri="http://docutils.sourceforge.net/">
"""],
]

Beispiel #41
0
    [
        u"""\
.. image:: picture.png
   :align: \xe4
""",
        u"""\
<document source="test data">
    <system_message level="3" line="1" source="test data" type="ERROR">
        <paragraph>
            Error in "image" directive:
            invalid option value: (option: "align"; value: %s)
            "\xe4" unknown; choose from "top", "middle", "bottom", "left", "center", or "right".
        <literal_block xml:space="preserve">
            .. image:: picture.png
               :align: \xe4
""" % repr(reprunicode(u'\xe4'))
    ],
    [
        """
.. image:: test.png
   :target: Uppercase_

.. _Uppercase: http://docutils.sourceforge.net/
""", """\
<document source="test data">
    <reference name="Uppercase" refname="uppercase">
        <image uri="test.png">
    <target ids="uppercase" names="uppercase" refuri="http://docutils.sourceforge.net/">
"""
    ],
    [
Beispiel #42
0
    def run(self):
        """Include a file as part of the content of this reST file."""

        # copied from docutils.parsers.rst.directives.misc.Include
        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))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe(u'Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        except IOError as error:
            raise self.severe(u'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(u'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]

        # copied code ends
        parser = CommonMarkParser()
        md_document = utils.new_document(path, self.state.document.settings)
        parser.parse(rawtext, md_document)
        return md_document.children
Beispiel #43
0
"""],
["""\
.. image:: picture.png
   :align: \xe4
""",
"""\
<document source="test data">
    <system_message level="3" line="1" source="test data" type="ERROR">
        <paragraph>
            Error in "image" directive:
            invalid option value: (option: "align"; value: %s)
            "\xe4" unknown; choose from "top", "middle", "bottom", "left", "center", or "right".
        <literal_block xml:space="preserve">
            .. image:: picture.png
               :align: \xe4
""" % repr(reprunicode('\xe4'))],
["""
.. image:: test.png
   :target: Uppercase_

.. _Uppercase: http://docutils.sourceforge.net/
""",
"""\
<document source="test data">
    <reference name="Uppercase" refname="uppercase">
        <image uri="test.png">
    <target ids="uppercase" names="uppercase" refuri="http://docutils.sourceforge.net/">
"""],
[r"""
.. image:: path\ with\ spaces/name\ with\ spaces.png
   :target: path\ with\ spaces/
Beispiel #44
0
        u"""\
.. image:: picture.png
   :align: \xe4
""",
        u"""\
<document source="test data">
    <system_message level="3" line="1" source="test data" type="ERROR">
        <paragraph>
            Error in "image" directive:
            invalid option value: (option: "align"; value: %s)
            "\xe4" unknown; choose from "top", "middle", "bottom", "left", "center", or "right".
        <literal_block xml:space="preserve">
            .. image:: picture.png
               :align: \xe4
"""
        % repr(reprunicode(u"\xe4")),
    ],
    [
        """
.. image:: test.png
   :target: Uppercase_

.. _Uppercase: http://docutils.sourceforge.net/
""",
        """\
<document source="test data">
    <reference name="Uppercase" refname="uppercase">
        <image uri="test.png">
    <target ids="uppercase" names="uppercase" refuri="http://docutils.sourceforge.net/">
""",
    ],
    def run(self):
        env = self.state.document.settings.env

        if self.arguments[0].startswith('<') and \
           self.arguments[0].endswith('>'):
            # docutils "standard" includes, do not do path processing
            return BaseInclude.run(self)
        rel_filename, filename = env.relfn2path(self.arguments[0])
        self.arguments[0] = filename
        
        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))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        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 = io.FileInput(
                source_path=path, encoding=encoding,
                error_handler=(self.state.document.settings.\
                               input_encoding_error_handler),
                handle_io_errors=None)
        except IOError as error:
            raise self.severe('Problems with "%s" directive path:\n%s.' %
                      (self.name, 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)
            
            include_lines = preProcessLines( include_lines )
            
            self.state_machine.insert_input(include_lines, path)
            return []
Beispiel #46
0
    def run(self):
        """Include a file as part of the content of this reST file."""
        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))
        path = directives.path(self.arguments[0])
        if path.startswith("<") and path.endswith(">"):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        encoding = self.options.get(
            "encoding", self.state.document.settings.input_encoding
        )
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get(
            "tab-width", self.state.document.settings.tab_width
        )
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(
                source_path=path, encoding=encoding, error_handler=e_handler
            )
        except UnicodeEncodeError as error:
            raise self.severe(
                u'Problems with "%s" directive path:\n'
                'Cannot encode input file path "%s" '
                "(wrong locale?)." % (self.name, SafeString(path))
            )
        except IOError as error:
            raise self.severe(
                u'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(
                u'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]

        include_lines = statemachine.string2lines(
            rawtext, tab_width, convert_whitespace=True
        )
        if "literal" in self.options:
            # Don't 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, source=path, classes=self.options.get("class", [])
            )
            literal_block.line = 1
            self.add_name(literal_block)
            if "number-lines" in self.options:
                try:
                    startline = int(self.options["number-lines"] or 1)
                except ValueError:
                    raise self.error(":number-lines: with non-integer " "start value")
                endline = startline + len(include_lines)
                if text.endswith("\n"):
                    text = text[:-1]
                tokens = NumberLines([([], text)], startline, endline)
                for classes, value in tokens:
                    if classes:
                        literal_block += nodes.inline(value, value, classes=classes)
                    else:
                        literal_block += nodes.Text(value)
            else:
                literal_block += nodes.Text(text)
            return [literal_block]
        if "code" in self.options:
            self.options["source"] = path
            # Don't convert tabs to spaces, if `tab_width` is negative:
            if tab_width < 0:
                include_lines = rawtext.splitlines()
            codeblock = CodeBlock(
                self.name,
                [self.options.pop("code")],  # arguments
                self.options,
                include_lines,  # content
                self.lineno,
                self.content_offset,
                self.block_text,
                self.state,
                self.state_machine,
            )
            return codeblock.run()
        self.state_machine.insert_input(include_lines, path)
        return []
Beispiel #47
0
  def run(self):
    """Include a file as part of the content of this reST file."""

    # Direct copy from Include with changes noted.

    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))

    # CHANGE: for some reason the arg to the include directive is expanded to
    # the full path for the docutils Include directive, but for ours, it
    # remains unchanged from the value supplied in the source, so we have to
    # expand it ourselves
    env = self.state.document.settings.env
    if not os.path.exists(self.arguments[0]):
      self.arguments[0] = env.srcdir + self.arguments[0]

    path = directives.path(self.arguments[0])
    if path.startswith('<') and path.endswith('>'):
      path = os.path.join(self.standard_include_path, path[1:-1])

    path = os.path.normpath(os.path.join(source_dir, path))
    path = utils.relative_path(None, path)
    path = nodes.reprunicode(path)
    encoding = self.options.get(
      'encoding', self.state.document.settings.input_encoding)
    e_handler=self.state.document.settings.input_encoding_error_handler
    tab_width = self.options.get(
      'tab-width', self.state.document.settings.tab_width)
    try:
      self.state.document.settings.record_dependencies.add(path)
      include_file = io.FileInput(source_path=path,
                                  encoding=encoding,
                                  error_handler=e_handler)
    except UnicodeEncodeError as error:
      raise self.severe('Problems with "%s" directive path:\n'
                        'Cannot encode input file path "%s" '
                        '(wrong locale?).' %
                        (self.name, SafeString(path)))
    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]

    include_lines = statemachine.string2lines(rawtext, tab_width,
                                              convert_whitespace=True)
    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, source=path,
                              classes=self.options.get('class', []))
      literal_block.line = 1
      self.add_name(literal_block)
      if 'number-lines' in self.options:
        try:
          startline = int(self.options['number-lines'] or 1)
        except ValueError:
          raise self.error(':number-lines: with non-integer '
                           'start value')
        endline = startline + len(include_lines)
        if text.endswith('\n'):
          text = text[:-1]
        tokens = NumberLines([([], text)], startline, endline)
        for classes, value in tokens:
          if classes:
            literal_block += nodes.inline(value, value,
                                          classes=classes)
          else:
            literal_block += nodes.Text(value, value)
      else:
        literal_block += nodes.Text(text, text)
      return [literal_block]

    if 'code' in self.options:
      self.options['source'] = path
      codeblock = CodeBlock(self.name,
                            [self.options.pop('code')], # arguments
                            self.options,
                            include_lines, # content
                            self.lineno,
                            self.content_offset,
                            self.block_text,
                            self.state,
                            self.state_machine)
      return codeblock.run()

    # CHANGE: add the suffixes to all the references (only do it for vimdocs)
    if isinstance(env.app.builder, VimdocBuilder) and 'ref-suffix' in self.options:
      suffix = self.options['ref-suffix']
      for i, line in enumerate(include_lines):
        # relying on a regex is gross, but it's easy and we just have to worry
        # about the eclim docs, so it'll do.
        match = self.REF.match(line)
        if match:
          include_lines[i] = '%s_%s%s' % (match.group(1), suffix, match.group(2))

    self.state_machine.insert_input(include_lines, path)
    return []
Beispiel #48
0
    def run(self):
        """
        Verbatim copy of docutils.parsers.rst.directives.misc.Include.run()
        that just calls to our Code instead of builtin CodeBlock but otherwise
        just passes it back to the parent implementation.
        """
        if not 'code' in self.options:
            return docutils.parsers.rst.directives.misc.Include.run(self)

        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1)
        source_dir = os.path.dirname(os.path.abspath(source))
        path = directives.path(self.arguments[0])
        if path.startswith('<') and path.endswith('>'):
            path = os.path.join(self.standard_include_path, path[1:-1])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = nodes.reprunicode(path)
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get('tab-width',
                                     self.state.document.settings.tab_width)
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(source_path=path,
                                        encoding=encoding,
                                        error_handler=e_handler)
        except UnicodeEncodeError as error:
            raise self.severe('Problems with "%s" directive path:\n'
                              'Cannot encode input file path "%s" '
                              '(wrong locale?).' %
                              (self.name, SafeString(path)))
        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]

        include_lines = statemachine.string2lines(rawtext,
                                                  tab_width,
                                                  convert_whitespace=True)

        self.options['source'] = path
        codeblock = Code(
            self.name,
            [self.options.pop('code')],  # arguments
            self.options,
            include_lines,  # content
            self.lineno,
            self.content_offset,
            self.block_text,
            self.state,
            self.state_machine)
        return codeblock.run()