Пример #1
0
def add_role(app, new_role_name):
    options = {
        'class': directives.class_option(new_role_name),
    }
    role = roles.CustomRole(new_role_name, roles.generic_custom_role, options,
                            "")
    app.add_role(new_role_name, role)
Пример #2
0
def add_literal_role(rolename):
    from docutils.parsers.rst import roles
    from docutils import nodes
    nodeclass = nodes.literal
    generic = roles.GenericRole(rolename, nodeclass)
    role = roles.CustomRole(rolename, generic, {'classes': [rolename]})
    roles.register_local_role(rolename, role)
Пример #3
0
    def set_site(self, site):
        self.site = site
        roles.register_local_role("pep", pep_role)
        roles.register_local_role("rfc", rfc_role)
        roles.register_local_role("term", term_role)
        roles.register_local_role("option", option_role)
        roles.register_local_role("ref", ref_role)
        roles.register_local_role("eq", eq_role)

        # This is copied almost verbatim from Sphinx
        generic_docroles = {
            "command": nodes.strong,
            "dfn": nodes.emphasis,
            "envvar": nodes.literal,
            "kbd": nodes.literal,
            "mailheader": nodes.emphasis,
            "makevar": nodes.strong,
            "manpage": nodes.emphasis,
            "mimetype": nodes.emphasis,
            "newsgroup": nodes.emphasis,
            "program": nodes.strong,
            "regexp": nodes.literal,
        }

        for rolename, nodeclass in generic_docroles.items():
            generic = roles.GenericRole(rolename, nodeclass)
            role = roles.CustomRole(rolename, generic, {"classes": [rolename]})
            roles.register_local_role(rolename, role)

        specific_docroles = {
            "guilabel": menusel_role,
            "menuselection": menusel_role,
            "file": emph_literal_role,
            "samp": emph_literal_role,
            "abbr": abbr_role,
        }

        for rolename, func in specific_docroles.items():
            roles.register_local_role(rolename, func)

        # Handle abbr title
        add_node(abbreviation, visit_abbreviation, depart_abbreviation)

        for name, (base_url, prefix) in self.site.config.get("EXTLINKS",
                                                             {}).items():
            roles.register_local_role(name, make_link_role(base_url, prefix))

        directives.register_directive("deprecated", VersionChange)
        directives.register_directive("versionadded", VersionChange)
        directives.register_directive("versionchanged", VersionChange)
        directives.register_directive("centered", Centered)
        directives.register_directive("hlist", HList)
        directives.register_directive("seealso", SeeAlso)
        directives.register_directive("glossary", Glossary)
        directives.register_directive("option", Option)
        directives.register_directive("math", Math)

        site.rst_transforms.append(Today)

        return super(Plugin, self).set_site(site)
Пример #4
0
    def set_site(self, site):
        self.site = site
        roles.register_local_role('pep', pep_role)
        roles.register_local_role('rfc', rfc_role)
        roles.register_local_role('term', term_role)
        roles.register_local_role('option', option_role)
        roles.register_local_role('ref', ref_role)
        roles.register_local_role('eq', eq_role)

        # This is copied almost verbatim from Sphinx
        generic_docroles = {
            'command': nodes.strong,
            'dfn': nodes.emphasis,
            'envvar': nodes.literal,
            'kbd': nodes.literal,
            'mailheader': nodes.emphasis,
            'makevar': nodes.strong,
            'manpage': nodes.emphasis,
            'mimetype': nodes.emphasis,
            'newsgroup': nodes.emphasis,
            'program': nodes.strong,
            'regexp': nodes.literal,
        }

        for rolename, nodeclass in generic_docroles.items():
            generic = roles.GenericRole(rolename, nodeclass)
            role = roles.CustomRole(rolename, generic, {'classes': [rolename]})
            roles.register_local_role(rolename, role)

        specific_docroles = {
            'guilabel': menusel_role,
            'menuselection': menusel_role,
            'file': emph_literal_role,
            'samp': emph_literal_role,
            'abbr': abbr_role,
        }

        for rolename, func in specific_docroles.items():
            roles.register_local_role(rolename, func)

        # Handle abbr title
        add_node(abbreviation, visit_abbreviation, depart_abbreviation)

        for name, (base_url, prefix) in self.site.config.get('EXTLINKS',
                                                             {}).items():
            roles.register_local_role(name, make_link_role(base_url, prefix))

        directives.register_directive('deprecated', VersionChange)
        directives.register_directive('versionadded', VersionChange)
        directives.register_directive('versionchanged', VersionChange)
        directives.register_directive('centered', Centered)
        directives.register_directive('hlist', HList)
        directives.register_directive('seealso', SeeAlso)
        directives.register_directive('glossary', Glossary)
        directives.register_directive('option', Option)
        directives.register_directive('math', Math)

        site.rst_transforms.append(Today)

        return super(Plugin, self).set_site(site)
Пример #5
0
def setup(app):
    from docutils.parsers.rst import roles

    for rolename, nodeclass in iteritems(generic_docroles):
        generic = roles.GenericRole(rolename, nodeclass)
        role = roles.CustomRole(rolename, generic, {'classes': [rolename]})
        roles.register_local_role(rolename, role)

    for rolename, func in iteritems(specific_docroles):
        roles.register_local_role(rolename, func)
Пример #6
0
 def run(self):
     """Dynamically create and register a custom interpreted text role."""
     if self.content_offset > self.lineno or not self.content:
         raise self.error('"%s" directive requires arguments on the first '
                          'line.' % self.name)
     args = self.content[0]
     match = self.argument_pattern.match(args)
     if not match:
         raise self.error('"%s" directive arguments not valid role names: '
                          '"%s".' % (self.name, args))
     new_role_name = match.group(1)
     base_role_name = match.group(3)
     messages = []
     if base_role_name:
         base_role, messages = roles.role(base_role_name,
                                          self.state_machine.language,
                                          self.lineno, self.state.reporter)
         if base_role is None:
             error = self.state.reporter.error(
                 'Unknown interpreted text role "%s".' % base_role_name,
                 nodes.literal_block(self.block_text, self.block_text),
                 line=self.lineno)
             return messages + [error]
     else:
         base_role = roles.generic_custom_role
     assert not hasattr(base_role, 'arguments'), (
         'Supplemental directive arguments for "%s" directive not '
         'supported (specified by "%r" role).' % (self.name, base_role))
     try:
         converted_role = convert_directive_function(base_role)
         (arguments, options, content,
          content_offset) = (self.state.parse_directive_block(
              self.content[1:],
              self.content_offset,
              converted_role,
              option_presets={}))
     except states.MarkupError as detail:
         error = self.state_machine.reporter.error(
             'Error in "%s" directive:\n%s.' % (self.name, detail),
             nodes.literal_block(self.block_text, self.block_text),
             line=self.lineno)
         return messages + [error]
     if 'class' not in options:
         try:
             options['class'] = directives.class_option(new_role_name)
         except ValueError as detail:
             error = self.state_machine.reporter.error(
                 'Invalid argument for "%s" directive:\n%s.' %
                 (self.name, SafeString(detail)),
                 nodes.literal_block(self.block_text, self.block_text),
                 line=self.lineno)
             return messages + [error]
     role = roles.CustomRole(new_role_name, base_role, options, content)
     roles.register_local_role(new_role_name, role)
     return messages
Пример #7
0
def setup(app: "Sphinx") -> Dict[str, Any]:
    from docutils.parsers.rst import roles

    for rolename, nodeclass in generic_docroles.items():
        generic = roles.GenericRole(rolename, nodeclass)
        role = roles.CustomRole(rolename, generic, {'classes': [rolename]})
        roles.register_local_role(rolename, role)

    for rolename, func in specific_docroles.items():
        roles.register_local_role(rolename, func)

    return {
        'version': 'builtin',
        'parallel_read_safe': True,
        'parallel_write_safe': True,
    }
Пример #8
0
def archive_directive(name, arguments, options, content, lineno,
                      content_offset, block_text, state, state_machine):
    """ Directive to create a archive (zip) from a sample project """
    environment = state.document.settings.env
    static_path = environment.config.html_static_path[0]

    directory = arguments[0]

    if options.has_key('file'):
        filename = options['file']
    else:
        filename = os.path.basename(directory.rstrip(os.sep)) + '.zip'

    archive_file = zipfile.ZipFile(
        os.path.dirname(os.path.abspath(__file__)) + '%s%s%s' %
        (os.sep, static_path, os.sep) + filename, "w")

    if directory.startswith(os.sep):
        dir = directory
    else:
        dir = os.path.normpath(
            os.path.join(environment.config.code_path, directory))

    for root, dirs, files in os.walk(dir, topdown=False):
        for name in files:
            file = os.path.join(root, name)
            zipfilename = string.replace(file, dir, '')
            if zipfilename[0] == os.sep:
                zipfilename = zipfilename[1:]
            archive_file.write(file, str(zipfilename), zipfile.ZIP_DEFLATED)

    archive_file.close()

    archive = util.relative_uri(state_machine.document.current_source,
                                os.path.dirname(os.path.abspath(__file__))
                        + '%s%s%s' % (os.sep, static_path, os.sep)) \
                        + filename

    role = roles.CustomRole(role_name, archive_role, {'archive': archive},
                            content)
    roles.register_local_role(role_name, role)
    return
Пример #9
0
    def set_site(self, site):
        self.site = site
        roles.register_local_role('pep', pep_role)
        roles.register_local_role('rfc', rfc_role)

        # This is copied almost verbatim from Sphinx
        generic_docroles = {
            'command': nodes.strong,
            'dfn': nodes.emphasis,
            'kbd': nodes.literal,
            'mailheader': nodes.emphasis,
            'makevar': nodes.strong,
            'manpage': nodes.emphasis,
            'mimetype': nodes.emphasis,
            'newsgroup': nodes.emphasis,
            'program': nodes.strong,
            'regexp': nodes.literal,
        }

        for rolename, nodeclass in generic_docroles.iteritems():
            generic = roles.GenericRole(rolename, nodeclass)
            role = roles.CustomRole(rolename, generic, {'classes': [rolename]})
            roles.register_local_role(rolename, role)

        specific_docroles = {
            'guilabel': menusel_role,
            'menuselection': menusel_role,
            'file': emph_literal_role,
            'samp': emph_literal_role,
        }

        for rolename, func in specific_docroles.iteritems():
            roles.register_local_role(rolename, func)

        for name, (base_url, prefix) in self.site.config.get('EXTLINKS',
                                                             {}).iteritems():
            roles.register_local_role(name, make_link_role(base_url, prefix))

        return super(Plugin, self).set_site(site)
Пример #10
0
def archive_directive(name, arguments, options, content, lineno,
                      content_offset, block_text, state, state_machine):
    """ Directive to create a archive (zip) from a sample project """
    clone_and_pull(state)
    environment = state.document.settings.env

    static_path = environment.config.html_static_path[0]

    directory = arguments[0]

    if options.has_key('file'):
        filename = options['file']
    else:
        filename = os.path.basename(directory.rstrip(os.sep)) + '.zip'

    archive_file = zipfile.ZipFile(
        os.path.dirname(os.path.abspath(__file__)) + '%s%s%s' %
        (os.sep, static_path, os.sep) + filename, "w")

    if options.has_key('root'):
        base = options['root']
    else:
        base = ''

    fnames = list_files(state, base)
    for name in fnames:
        archive_file.writestr(name, '\n'.join(get_file(state, name)))

    archive_file.close()

    archive = util.relative_uri(state_machine.document.current_source,
                                os.path.dirname(os.path.abspath(__file__))
                        + '%s%s%s' % (os.sep, static_path, os.sep)) \
                        + filename

    role = roles.CustomRole(role_name, archive_role, {'archive': archive},
                            content)
    roles.register_local_role(role_name, role)
    return []
Пример #11
0
    except states.MarkupError, detail:
        error = state_machine.reporter.error(
            'Error in "%s" directive:\n%s.' % (name, detail),
            nodes.literal_block(block_text, block_text),
            line=lineno)
        return messages + [error]
    if not options.has_key('class'):
        try:
            options['class'] = directives.class_option(new_role_name)
        except ValueError, detail:
            error = state_machine.reporter.error(
                'Invalid argument for "%s" directive:\n%s.' % (name, detail),
                nodes.literal_block(block_text, block_text),
                line=lineno)
            return messages + [error]
    role = roles.CustomRole(new_role_name, base_role, options, content)
    roles.register_local_role(new_role_name, role)
    return messages


role.content = 1


def default_role(name, arguments, options, content, lineno, content_offset,
                 block_text, state, state_machine):
    """Set the default interpreted text role."""
    if not arguments:
        if roles._roles.has_key(''):
            # restore the "default" default role
            del roles._roles['']
        return []
Пример #12
0
        final_argument_whitespace = True
        option_spec = {
            'width': directives.nonnegative_int,
            'height': directives.nonnegative_int
        }

        def run(self):
            set_classes(self.options)
            node = role_video(source=self.arguments[0], **self.options)
            return [node]

    generic_docroles = {'doc': role_doc}

    for rolename, nodeclass in generic_docroles.items():
        generic = roles.GenericRole(rolename, nodeclass)
        role = roles.CustomRole(rolename, generic, {'classes': [rolename]})
        roles.register_local_role(rolename, role)

    directives.register_directive('video', VideoDirective)

Builder.load_string('''
#:import parse_color kivy.parser.parse_color



<RstDocument>:
    content: content
    scatter: scatter
    do_scroll_x: False
    canvas.before:
        Color:
Пример #13
0
def _define_role(name):
    base_role = roles.generic_custom_role
    role = roles.CustomRole(name, base_role, {'class': [name]}, [])

    roles.register_local_role(name, role)
Пример #14
0
def _define_role(name):
#    print "sphinxcontrib_roles#_define_role name:", name
    #=> strike, red
    base_role = roles.generic_custom_role
    role = roles.CustomRole(name, base_role, {'class': [name]}, [])
    roles.register_local_role(name, role)