当前位置: 首页>>代码示例>>Python>>正文


Python lexers.get_lexer_for_mimetype函数代码示例

本文整理汇总了Python中pygments.lexers.get_lexer_for_mimetype函数的典型用法代码示例。如果您正苦于以下问题:Python get_lexer_for_mimetype函数的具体用法?Python get_lexer_for_mimetype怎么用?Python get_lexer_for_mimetype使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了get_lexer_for_mimetype函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: edit

def edit(db, id):
    """
    Edits the entry. If the entry is protected with a password it will display
    a simple password entry form until the password is a match in the database
    """

    paste = _get_paste(db, id)
    if not paste:
        return bottle.HTTPError(404, output='This paste does not exist')
    password = bottle.request.forms.password
    is_encrypted = bool(bottle.request.forms.is_encrypted)
    if not is_encrypted:
        match = hashlib.sha1(password).hexdigest()
    else:
        match = password
    util.log.debug(
        '%s == %s ? %s' % (
            match, paste.password,
            match == paste.password,
        )
    )
    kwargs = {
        'password': paste.password,
        'content': paste.content,
        'syntax': lexers.get_lexer_for_mimetype(paste.mimetype).aliases[0],
    }
    if paste.password:
        if not password:
            return _password_protect_form()
        if match == paste.password:
            return _edit_form('edit entry #%s' % (paste.id,), **kwargs)
        else:
            return bottle.HTTPError(401, output='Wrong password provided')
    else:
        return _edit_form('edit entry #%s' % (paste.id, ), **kwargs)
开发者ID:dcolish,项目名称:pasttle,代码行数:35,代码来源:server.py

示例2: _html_format

    def _html_format(self, content, content_types):
        try:
            from pygments import highlight
            from pygments.lexers import get_lexer_for_mimetype
            from pygments.formatters import HtmlFormatter

            lexer = None
            for ct in content_types:
                try:
                    lexer = get_lexer_for_mimetype(ct)
                    break
                except:
                    pass

            if lexer is None:
                raise ValueError("No lexer found")
            formatter = HtmlFormatter()
            return html_body % dict(
                css=formatter.get_style_defs(),
                content=highlight(content, lexer, formatter).encode('utf8'))
        except Exception as e:
            log.warning(
                "Could not pygment the content because of the following "
                "error :\n%s" % e)
            return html_body % dict(
                css='',
                content=u('<pre>%s</pre>') %
                    content.replace(b('>'), b('&gt;'))
                           .replace(b('<'), b('&lt;')))
开发者ID:LonelyWhale,项目名称:openstackDemo,代码行数:29,代码来源:root.py

示例3: test_get_lexers

def test_get_lexers():
    # test that the lexers functions work
    def verify(func, args):
        x = func(opt='val', *args)
        assert isinstance(x, lexers.PythonLexer)
        assert x.options["opt"] == "val"

    for func, args in [(lexers.get_lexer_by_name, ("python",)),
                       (lexers.get_lexer_for_filename, ("test.py",)),
                       (lexers.get_lexer_for_mimetype, ("text/x-python",)),
                       (lexers.guess_lexer, ("#!/usr/bin/python -O\nprint",)),
                       (lexers.guess_lexer_for_filename, ("a.py", "<%= @foo %>"))
                       ]:
        yield verify, func, args

    for cls, (_, lname, aliases, _, mimetypes) in lexers.LEXERS.items():
        assert cls == lexers.find_lexer_class(lname).__name__

        for alias in aliases:
            assert cls == lexers.get_lexer_by_name(alias).__class__.__name__

        for mimetype in mimetypes:
            assert cls == lexers.get_lexer_for_mimetype(mimetype).__class__.__name__

    try:
        lexers.get_lexer_by_name(None)
    except ClassNotFound:
        pass
    else:
        raise Exception
开发者ID:spencerlyon2,项目名称:pygments,代码行数:30,代码来源:test_basic_api.py

示例4: body

    def body(self, content, content_type):
        lexer = None
        content_type = content_type.split(';')[0]
        if 'json' in content_type:
            lexer = JSONLexer()
            try:
                # Indent the JSON data.
                content = json.dumps(json.loads(content),
                                    sort_keys=True, indent=4)
            except Exception:
                pass
        if not lexer:
            try:
                lexer = get_lexer_for_mimetype(content_type)
            except ClassNotFound:
                try:
                    lexer = guess_lexer(content)
                except ClassNotFound:
                    return content

        if lexer.name == 'XML':
            dom = parseString(content)
            content = dom.toprettyxml(indent='  ')

        return pygments.highlight(content, lexer, self.formatter)
开发者ID:raff,项目名称:httpie,代码行数:25,代码来源:pretty.py

示例5: _pygmentize

def _pygmentize(paste, lang):
    """
    Guess (or force if lang is given) highlight on a given paste via pygments
    """

    if lang:
        try:
            lexer = lexers.get_lexer_by_name(lang)
        except lexers.ClassNotFound:
            lexer = lexers.get_lexer_by_name('text')
    else:
        lexer = lexers.get_lexer_for_mimetype(paste.mimetype)
    a = '<small><a href="/edit/%s">edit</a></small>' % (paste.id,)
    if paste.ip:
        ip = IPy.IP(long(paste.ip, 2))
        util.log.debug('Originally pasted from %s' % (ip,))
    if paste.filename:
        title = u'%s, created on %s' % (paste.filename, paste.created, )
    else:
        title = u'created on %s' % (paste.created, )
    title = '%s %s (%s)' % (paste.mimetype, title, a,)
    util.log.debug(lexer)
    return pygments.highlight(
        paste.content, lexer, formatters.HtmlFormatter(
            full=True, linenos='table',
            encoding='utf-8', lineanchors='ln', title=title)
        )
开发者ID:barseghyanartur,项目名称:pasttle,代码行数:27,代码来源:server.py

示例6: body

    def body(self, content, content_type):
        prettyfiers_by_lexer = {
            JSONLexer: lambda x: json.dumps(json.loads(x),
                                    sort_keys=True, indent=4),
            pygments.lexers.XmlLexer: xml_prettify,
        }

        content_type = content_type.split(';')[0]
        try:
            lexer = get_lexer_for_mimetype(content_type)
        except ClassNotFound:
            if 'json' in content_type:
                # JSON lexer not found, use internal
                lexer = JSONLexer()
            else:
                # no lexer for mimetype
                return content

        prettyfier = prettyfiers_by_lexer.get(lexer.__class__)
        if prettyfier is not None:
            try:
                # prettify the data.
                content = prettyfier(content)
            except Exception:
                pass
        return pygments.highlight(content, lexer, self.formatter)
开发者ID:macro,项目名称:httpie,代码行数:26,代码来源:pretty.py

示例7: nice_body

def nice_body(body, content=None, cssclass=None, encoding='utf-8'):
    if not body:
        return None
    cssclasses = ['codehilite']
    if cssclass:
        cssclasses.append(cssclass)
    classes = ' '.join(cssclasses)
    content = get_body_content_type(body, content)
    if content is not None:
        if 'x-www-form-urlencoded' in content:
            lex = IniLexer()
        elif 'json' in content:
            lex = JsonLexer()
        else:
            try:
                lex = get_lexer_for_mimetype(content, encoding=encoding)
            except ClassNotFound as e:
                return body

        if isinstance(lex, IniLexer):
            parsedbody = urlparse.parse_qsl(body, keep_blank_values=True)
            if body and not parsedbody:
                return tornado.escape.xhtml_escape(body)
            parsedbody = [(x.strip(), y) for [x, y] in parsedbody]
            args = collections.OrderedDict(sorted(parsedbody))
            params = "\n".join([k.strip() + "=" + v for k, v in args.iteritems()])
            return highlight(params, IniLexer(), HtmlFormatter(cssclass=classes, encoding=encoding))
        elif isinstance(lex, JsonLexer):
            try:
                return highlight(json.dumps(json.loads(body), indent=4), JsonLexer(), HtmlFormatter(cssclass=classes))
            except ValueError as e:
                pass

    return highlight(body, lex, HtmlFormatter(cssclass=classes, encoding=encoding))
开发者ID:leibowitz,项目名称:gianni-proxy-ui,代码行数:34,代码来源:util.py

示例8: pygment

def pygment(content, mime=None):
    if mime:
        lexer = get_lexer_for_mimetype(mime, stripall=True)
    else:
        lexer = guess_lexer(content)
    formatter = HtmlFormatter(linenos='table')
    code = highlight(content, lexer, formatter)
    return Markup(code)
开发者ID:GaretJax,项目名称:csat,代码行数:8,代码来源:pygments.py

示例9: process_body

 def process_body(self, content, content_type):
     try:
         lexer = get_lexer_for_mimetype(content_type)
     except ClassNotFound:
         pass
     else:
         content = pygments.highlight(content, lexer, self.formatter)
     return content
开发者ID:yevgenko,项目名称:httpie,代码行数:8,代码来源:output.py

示例10: print_data

 def print_data(self, data, mimetype):
     if data:
         try:
             lexer = get_lexer_for_mimetype(mimetype)
         except ClassNotFound:
             lexer = guess_lexer(data)
         print
         print highlight(data, lexer, TerminalFormatter())
开发者ID:victoru,项目名称:HttpShell,代码行数:8,代码来源:loggers.py

示例11: set_lexer_from_mime_type

    def set_lexer_from_mime_type(self, mime, **options):
        """
        Sets the pygments lexer from mime type.

        :param mime: mime type
        :param options: optional addtional options.
        """
        self._lexer = get_lexer_for_mimetype(mime, **options)
        _logger().info('lexer for mimetype (%s): %r', mime, self._lexer)
开发者ID:dtonal,项目名称:pyqode.core,代码行数:9,代码来源:pygments_sh.py

示例12: example

def example(body):
    if body:
        example = body.raw[body.mime_type].get('example', None)

        if example is not None:
            lexer = get_lexer_for_mimetype(body.mime_type, stripall=True)
            formatter = HtmlFormatter(linenos=False, cssclass="codehilite")
            return mark_safe(highlight(example, lexer, formatter))
    return ''
开发者ID:pkucmus,项目名称:ramlgnarok,代码行数:9,代码来源:markdown.py

示例13: highlight_by_mime_type

def highlight_by_mime_type(content, mimetype):
    try:
        #if config.get('global', 'syntax') == "1":
        lexer = get_lexer_for_mimetype(mimetype, encoding='chardet')
        #else:
        #lexer = get_lexer_for_mimetype('text/plain')
        formatter = HtmlFormatter(linenos="inline", cssclass="source", outencoding="utf-8")
        content = highlight(content, lexer, formatter)
    except Exception, e:
        #log_package('hilight error in extract_code : ' + str(e), log_path)
        content = '<p>An error occurred while formatting content: '+str(e)+'</p>' + content
开发者ID:ygbourhis,项目名称:django_doc4,代码行数:11,代码来源:util.py

示例14: get_blob_text

def get_blob_text(repopath, path, branchname='master'):
    repo = Repo(repopath)
    git = repo.git
    text = None
    text = git.show('%s:%s' % (branchname, path))
    mime_type = get_mime_type(repo, branchname, path)
    try:
        lexer = get_lexer_for_mimetype(mime_type)
    except ClassNotFound:
        lexer = get_lexer_by_name('text')
    formatter = HtmlFormatter(linenos=True, lineanchors='line', anchorlinenos=True)
    result = highlight(Markup(text).unescape(), lexer, formatter)
    return result
开发者ID:Sanjiban,项目名称:kickit,代码行数:13,代码来源:utils.py

示例15: pygmentize

def pygmentize(mime, blob):
    try:
        lexer = lexers.get_lexer_for_mimetype(mime)
    except ClassNotFound:
        try:
            lexer = lexers.get_lexer_by_name(mime)
        except:
            lexer = lexers.get_lexer_by_name('text')

    pygmented_string = pygments.highlight(blob, lexer, NakedHtmlFormatter())
    pygmented_string = unescape_amp(pygmented_string)

    return mark_safe(pygmented_string)
开发者ID:EnTeQuAk,项目名称:brigitte,代码行数:13,代码来源:utils.py


注:本文中的pygments.lexers.get_lexer_for_mimetype函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。