python类get_lexer_by_name()的实例源码

processors.py 文件源码 项目:structlog-pretty 作者: underyx 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def __init__(self, field_map):
        """Create a processor that syntax highlights code in the event values

        The syntax highlighting will use with ANSI terminal color codes.

        :param field_map: A mapping with field names mapped to languages, e.g.
                          ``{'body': 'json': 'soap_response': 'xml'}``
        """
        self.lexers = {
            field: get_lexer_by_name(language)
            for field, language in field_map.items()
        }
markdown2.py 文件源码 项目:awesome-python3-webapp 作者: syusonn 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def _get_pygments_lexer(self, lexer_name):
        try:
            from pygments import lexers, util
        except ImportError:
            return None
        try:
            return lexers.get_lexer_by_name(lexer_name)
        except util.ClassNotFound:
            return None
util.py 文件源码 项目:akamatsu 作者: rmed 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def blockcode(self, text, lang):
        if not lang:
            return '\n<pre><code>{}</code></pre>\n'.format(text.strip())

        lexer = get_lexer_by_name(lang, stripall=True)
        formatter = HtmlFormatter()

        return highlight(code=text, lexer=lexer, formatter=formatter)
mdconverter.py 文件源码 项目:CustomContextMenu 作者: bigsinger 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def block_code(self, code, lang):
        lang = CODE_LANG
        if not lang:
            return '\n<pre><code>%s</code></pre>\n' % \
                mistune.escape(code)
        lexer = get_lexer_by_name(lang, stripall=True)
        formatter = html.HtmlFormatter()
        return highlight(code, lexer, formatter)

# renderer = HighlightRenderer()
# markdown = mistune.Markdown(renderer=renderer)
# print(markdown('```python\nassert 1 == 1\n```'))
markdown2.py 文件源码 项目:touch-pay-client 作者: HackPucBemobi 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def _get_pygments_lexer(self, lexer_name):
        try:
            from pygments import lexers, util
        except ImportError:
            return None
        try:
            return lexers.get_lexer_by_name(lexer_name)
        except util.ClassNotFound:
            return None
markdown_simple.py 文件源码 项目:wdom 作者: miyakogi 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def blockcode(self, text, lang):
        if not lang:
            return '\n<pre><code>{}</code></pre>\n'.format(text.strip())
        lexer = get_lexer_by_name(lang)
        formatter = HtmlFormatter()
        return highlight(text, lexer, formatter)
markdown2.py 文件源码 项目:python-awesome-web 作者: tianzhenyun 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _get_pygments_lexer(self, lexer_name):
        try:
            from pygments import lexers, util
        except ImportError:
            return None
        try:
            return lexers.get_lexer_by_name(lexer_name)
        except util.ClassNotFound:
            return None
markup.py 文件源码 项目:pyt 作者: python-security 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def block_code(self, code, lang):
        if not lang:
            return '\n<pre><code>%s</code></pre>\n' % \
                mistune.escape(code)
        lexer = get_lexer_by_name(lang, stripall=True)
        formatter = HtmlFormatter()
        return highlight(code, lexer, formatter)
output.py 文件源码 项目:flasky 作者: RoseOu 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def process_body(self, content, content_type, subtype, encoding):
        try:
            lexer = self.lexers_by_type.get(content_type)
            if not lexer:
                try:
                    lexer = get_lexer_for_mimetype(content_type)
                except ClassNotFound:
                    lexer = get_lexer_by_name(subtype)
                self.lexers_by_type[content_type] = lexer
        except ClassNotFound:
            pass
        else:
            content = pygments.highlight(content, lexer, self.formatter)
        return content.strip()
compat.py 文件源码 项目:sdining 作者: Lurance 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def pygments_highlight(text, lang, style):
        lexer = get_lexer_by_name(lang, stripall=False)
        formatter = HtmlFormatter(nowrap=True, style=style)
        return pygments.highlight(text, lexer, formatter)
st_pygments_highlight.py 文件源码 项目:macos-st-packages 作者: zce 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def syntax_hl(src, lang=None, guess_lang=False, inline=False):
    """Highlight."""

    css_class = 'inline-highlight' if inline else 'highlight'

    src = src.strip('\n')

    try:
        lexer = get_lexer_by_name(lang)
    except ValueError:
        try:
            if guess_lang:
                lexer = guess_lexer(src)
            else:
                lexer = get_lexer_by_name('text')
        except ValueError:
            lexer = get_lexer_by_name('text')
    if inline:
        formatter = SublimeInlineHtmlFormatter(
            cssclass=css_class,
            classprefix=css_class + ' '
        )
    else:
        formatter = SublimeBlockFormatter(
            cssclass=css_class
        )
    return highlight(src, lexer, formatter)
fields.py 文件源码 项目:evonove.it 作者: evonove 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def render(self, value):
        src = value['code_text'].strip('\n')
        lang = value['language']

        lexer = get_lexer_by_name(lang)
        formatter = get_formatter_by_name(
            'html',
            linenos='table',
            noclasses=True,
            style='monokai',
        )
        return mark_safe(highlight(src, lexer, formatter))
markdown2.py 文件源码 项目:true_review_web2py 作者: lucadealfaro 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _get_pygments_lexer(self, lexer_name):
        try:
            from pygments import lexers, util
        except ImportError:
            return None
        try:
            return lexers.get_lexer_by_name(lexer_name)
        except util.ClassNotFound:
            return None
markdown2.py 文件源码 项目:python-webapp-blog 作者: tzshlyt 项目源码 文件源码 阅读 60 收藏 0 点赞 0 评论 0
def _get_pygments_lexer(self, lexer_name):
        try:
            from pygments import lexers, util
        except ImportError:
            return None
        try:
            return lexers.get_lexer_by_name(lexer_name)
        except util.ClassNotFound:
            return None
models.py 文件源码 项目:Code_python 作者: RoJoHub 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def save(self, *args, **kwargs):
        """
        Use the `pygments` library to create a highlighted HTML
        representation of the code snippet.
        """
        lexer = get_lexer_by_name(self.language)
        linenos = self.linenos and 'table' or False
        options = self.title and {'title': self.title} or {}
        formatter = HtmlFormatter(style=self.style, linenos=linenos,
                                  full=True, **options)
        self.highlighted = highlight(self.code, lexer, formatter)
        super(Snippet, self).save(*args, **kwargs)
markdown2.py 文件源码 项目:spc 作者: whbrewer 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def _get_pygments_lexer(self, lexer_name):
        try:
            from pygments import lexers, util
        except ImportError:
            return None
        try:
            return lexers.get_lexer_by_name(lexer_name)
        except util.ClassNotFound:
            return None
directives.py 文件源码 项目:prestashop-sync 作者: dragoon 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def pygments_directive(name, arguments, options, content, lineno,
                        content_offset, block_text, state, state_machine):
        try:
            lexer = get_lexer_by_name(arguments[0])
        except ValueError:
            # no lexer found - use the text one instead of an exception
            lexer = TextLexer()
        # take an arbitrary option if more than one is given
        formatter = options and VARIANTS[options.keys()[0]] or DEFAULT
        parsed = highlight(u'\n'.join(content), lexer, formatter)
        parsed = '<div class="codeblock">%s</div>' % parsed
        return [nodes.raw('', parsed, format='html')]
markdown.py 文件源码 项目:hacksoft.io 作者: HackSoftware 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def blockcode(self, text, lang):
        try:
            lexer = get_lexer_by_name(lang, stripall=True)
        except ClassNotFound:
            lexer = None

        if lexer:
            formatter = HtmlFormatter(noclasses=True)
            return highlight(text, lexer, formatter)
        # default
        return '\n<pre><code>{}</code></pre>\n'.format(
                            h.escape_html(text.strip()))
markup.py 文件源码 项目:leetcode 作者: thomasyimgit 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def _handle_codeblock(self, match):
        """
        match args: 1:backticks, 2:lang_name, 3:newline, 4:code, 5:backticks
        """
        from pygments.lexers import get_lexer_by_name

        # section header
        yield match.start(1), String        , match.group(1)
        yield match.start(2), String        , match.group(2)
        yield match.start(3), Text          , match.group(3)

        # lookup lexer if wanted and existing
        lexer = None
        if self.handlecodeblocks:
            try:
                lexer = get_lexer_by_name( match.group(2).strip() )
            except ClassNotFound:
                pass
        code = match.group(4)

        # no lexer for this language. handle it like it was a code block
        if lexer is None:
            yield match.start(4), String, code
            return

        for item in do_insertions([], lexer.get_tokens_unprocessed(code)):
            yield item

        yield match.start(5), String        , match.group(5)
formatter.py 文件源码 项目:microProxy 作者: mike820324 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def colorize(lexer_name, raw_text):  # pragma: no cover
    lexer = get_lexer_by_name(lexer_name, stripall=True)
    formatter = Terminal256Formatter()
    return highlight(raw_text, lexer, formatter)


问题


面经


文章

微信
公众号

扫码关注公众号