我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pygments.lexers.get_lexer_by_name()。
def _get_lexer(self, lang): if lang.lower() == 'sql': return get_lexer_by_name('postgresql', **self.options) tries = [lang] if lang.startswith('pl'): tries.append(lang[2:]) if lang.endswith('u'): tries.append(lang[:-1]) if lang.startswith('pl') and lang.endswith('u'): tries.append(lang[2:-1]) for l in tries: try: return get_lexer_by_name(l, **self.options) except ClassNotFound: pass else: # TODO: better logging # print >>sys.stderr, "language not found:", lang return None
def block_code(text, lang, inlinestyles=False, linenos=False): if not lang: text = text.strip() return u'<pre><code>%s</code></pre>\n' % mistune.escape(text) try: lexer = get_lexer_by_name(lang, stripall=True) formatter = html.HtmlFormatter( noclasses=inlinestyles, linenos=linenos ) code = highlight(text, lexer, formatter) if linenos: return '<div class="highlight-wrapper">%s</div>\n' % code return code except BaseException: return '<pre class="%s"><code>%s</code></pre>\n' % ( lang, mistune.escape(text) )
def block_code(self, code, lang): "Block code highlighter and formater" try: if not lang: lexer = guess_lexer(code, stripall=True) else: lexer = get_lexer_by_name(lang, stripall=True) detected = True code = highlight(code, lexer, self.code_formatter) except: code = escape(code) lang = None self.info.code.append(code) template = self.jinja2.get_template('code') rv = template.render(code=code, lang=lang, site=self.site, meta=self.meta) rv = rv.encode('utf-8') return rv
def __init__(self, code, language, tokennames='short'): """ Set up a lexical analyzer for `code` in `language`. """ self.code = code self.language = language self.tokennames = tokennames self.lexer = None # get lexical analyzer for `language`: if language in ('', 'text') or tokennames == 'none': return if not with_pygments: raise LexerError('Cannot analyze code. ' 'Pygments package not found.') try: self.lexer = get_lexer_by_name(self.language) except pygments.util.ClassNotFound: raise LexerError('Cannot analyze code. ' 'No Pygments lexer found for "%s".' % language) # Since version 1.2. (released Jan 01, 2010) Pygments has a # TokenMergeFilter. However, this requires Python >= 2.4. When Docutils # requires same minimal version, ``self.merge(tokens)`` in __iter__ can # be replaced by ``self.lexer.add_filter('tokenmerge')`` in __init__.
def _get_lexer(self, lang): if lang.lower() == 'sql': return get_lexer_by_name('postgresql', **self.options) tries = [ lang ] if lang.startswith('pl'): tries.append(lang[2:]) if lang.endswith('u'): tries.append(lang[:-1]) if lang.startswith('pl') and lang.endswith('u'): tries.append(lang[2:-1]) for l in tries: try: return get_lexer_by_name(l, **self.options) except ClassNotFound: pass else: # TODO: better logging # print >>sys.stderr, "language not found:", lang return None
def block_code(self, code, lang=None): """Rendering block level code. ``pre > code``. """ lexer = None if code.endswith('\n\n'): code = code[:-1] if HtmlFormatter and lang: try: lexer = get_lexer_by_name(lang, stripall=True) except ClassNotFound: code = lang + '\n' + code if not lexer or not HtmlFormatter: return '\n<pre><code>%s</code></pre>\n' % mistune.escape(code) formatter = HtmlFormatter() return highlight(code, lexer, formatter)
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) # limit the number of instances retained snippets = Snippet.objects.all() if len(snippets) > 100: snippets[0].delete()
def block_code(text, lang, inlinestyles=False, linenos=False): if not lang: text = text.strip() return u'<pre><code>%s</code></pre>\n' % mistune.escape(text) try: lexer = get_lexer_by_name(lang, stripall=True) formatter = html.HtmlFormatter( noclasses=inlinestyles, linenos=linenos ) code = highlight(text, lexer, formatter) if linenos: return '<div class="highlight">%s</div>\n' % code return code except: return '<pre class="%s"><code>%s</code></pre>\n' % ( lang, mistune.escape(text) )
def block_code(self, text, lang): linenos = inlinestyles = False if not lang: text = text.strip() return u'<pre><code>%s</code></pre>\n' % mistune.escape(text) try: lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter( noclasses=inlinestyles, linenos=linenos, cssclass='codehilite' ) code = highlight(text, lexer, formatter) if linenos: return '<div class="highlight-wrapper">%s</div>\n' % code return '<div class="doc doc-code">%s</div>%s' % (lang.upper(), code) except: return '<pre class="%s"><code>%s</code></pre>\n' % ( lang, mistune.escape(text) )
def code_block(self, node, entering): '''Output Pygments if required else use default html5 output''' if self.use_pygments: self.cr() info_words = node.info.split() if node.info else [] if len(info_words) > 0 and len(info_words[0]) > 0: try: lexer = get_lexer_by_name(info_words[0]) except ValueError: # no lexer found - use the text one instead of an exception lexer = TextLexer() else: lexer = TextLexer() formatter = HtmlFormatter(**self.pygments_options) parsed = highlight(node.literal, lexer, formatter) self.lit(parsed) self.cr() else: super().code_block(node, entering)
def color_stack_trace(): def excepthook(type_, value, trace): text = ''.join(traceback.format_exception(type_, value, trace)) try: from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import TerminalFormatter lexer = get_lexer_by_name('pytb', stripall=True) formatter = TerminalFormatter() sys.stderr.write(highlight(text, lexer, formatter)) except Exception: sys.stderr.write(text) sys.stderr.write('Failed to colorize the traceback.') sys.excepthook = excepthook setup_thread_excepthook()
def tokenize_file(source_file, language=None, literal_option=3): print(source_file) code = "" try: with codecs.open(source_file, "r",encoding='utf-8', errors='ignore') as f: code = f.read() except UnicodeDecodeError: return '', [] if language is None: try: lexer = get_lexer_for_filename(source_file) language = languageForLexer(lexer) except KeyError: # Not a valid extension lexer = guess_lexer(code) language = languageForLexer(lexer) else: lexer = get_lexer_by_name(language) return tokenize_code(code, lexer, language, literal_option)
def block_code(self, code, lang): guess = 'python3' if code.lstrip().startswith('<?php'): guess = 'php' elif code.lstrip().startswith(('<', '{%')): guess = 'html+jinja' elif code.lstrip().startswith(('function', 'var', '$')): guess = 'javascript' lexer = get_lexer_by_name(lang or guess, stripall=True) return highlight(code, lexer, HtmlFormatter()) # ???????md?????????????????????????
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)
def blockcode(self, text, lang): if not lang: return '\n<pre><code>{}</code></pre>\n'.format(houdini.escape_html(text.strip())) lexer = get_lexer_by_name(lang) formatter = HtmlFormatter() return highlight(text, lexer, formatter)
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() }
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
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)
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```'))
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)
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)
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()
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)
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))
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)
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')]
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()))
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)
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)