我们从Python开源项目中,提取了以下25个代码示例,用于说明如何使用pygments.lexers.guess_lexer()。
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 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 lang_for_block(source,lang): if lang in ('py', 'python'): if source.startswith('>>>'): # interactive session return 'pycon' else: # maybe Python -- try parsing it if try_parse(source): return 'python' else: # Guess return lang_for_block(source,'guess') elif lang in ('python3', 'py3') and source.startswith('>>>'): # for py3, recognize interactive sessions, but do not try parsing... return 'pycon3' elif lang == 'guess': try: #return 'python' lexer=guess_lexer(source) return lexer.aliases[0] except Exception: return None else: return lang
def __init__(self, source, highlight, expiration, title, password): expiration = int(expiration) if not source: raise ValueError() self.source = source if title: self.title = title if password: self.password = password if expiration > 0: self.expire_at = datetime.now() + timedelta(minutes=expiration) if highlight == 'auto': self.lexer = guess_lexer(source).aliases[0] else: self.lexer = highlight for _ in range(5): slug = self._generate_random_slug() if not db.session.query(exists().where(Paste.slug == slug)).scalar(): self.slug = slug break else: raise RuntimeError()
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 write_content(content_type, content, formatter, fh=sys.stdout): # nlines = len(content) content = dedent(''.join(content)) # ' ' to keep pygments from removing empty lines # split, merge by \n can introduce one additional line content = [' \n' if x == '' else x + '\n' for x in content.split('\n')][:nlines] # if content_type == 'COMMENT': fh.write(highlight(''.join(content), SoS_Lexer(), formatter)) elif content_type in ('REPORT', 'report'): fh.write(highlight(''.join(content), TextLexer(), formatter)) elif content_type == 'SECTION': fh.write(highlight(''.join(content), SoS_Lexer(), formatter)) elif content_type == 'DIRECTIVE': fh.write(highlight(''.join(content), SoS_Lexer(), formatter)) elif content_type == 'STATEMENT': fh.write(highlight(''.join(content), SoS_Lexer(), formatter)) elif content_type == 'ERROR': fh.write(highlight(''.join(content), SoS_Lexer(), formatter)) else: if content_type == 'run': content_type = 'bash' elif content_type == 'node': content_type = 'JavaScript' elif content_type == 'report': content_type = 'text' try: lexer = get_lexer_by_name(content_type) except Exception: try: lexer = guess_lexer(''.join(content)) except Exception: lexer = TextLexer() fh.write(highlight((''.join(content)), lexer, formatter))
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._parseHeader() if pygments and self.use_pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = get_lexer_by_name('text') except ValueError: lexer = get_lexer_by_name('text') formatter = get_formatter_by_name('html', linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses, hl_lines=self.hl_lines) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n' % \ (self.css_class, class_str, txt)
def hilite(self): """ Pass code to the http://pygments.pocoo.org highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._parseHeader() if pygments and self.use_pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = get_lexer_by_name('text') except ValueError: lexer = get_lexer_by_name('text') formatter = SublimeBlockFormatter( linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses, hl_lines=self.hl_lines ) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = _escape(self.src) classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n' % \ (self.css_class, class_str, txt)
def codehilite(self, lang, src): """Syntax highlite the inline code block.""" process_text = self.style_plain_text or lang or self.guess_lang if not lang and self.style_plain_text and not self.guess_lang: lang = 'text' sublime_hl_enabled, sublime_hl = self.config.get("sublime_hl", None) if sublime_hl_enabled: code = sublime_hl.syntax_highlight(src, lang, inline=True) elif pygments and self.use_pygments and process_text: try: lexer = get_lexer_by_name(lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(src) else: lexer = get_lexer_by_name('text') except ValueError: lexer = get_lexer_by_name('text') formatter = SublimeInlineHtmlFormatter( style=self.style, cssclass=self.css_class, noclasses=self.noclasses, classprefix=self.css_class + ' ' # Insert class prefix for styling Sublime ) code = highlight(src, lexer, formatter) else: # Just escape and build markup usable by JS highlighting libs txt = src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') txt = multi_space.sub(replace_nbsp, txt.replace('\t', ' ' * 4)) # Special format for sublime. classes = [self.css_class] if self.css_class and process_text else [] if lang and process_text: classes.append('language-%s' % lang) class_str = '' if len(classes): class_str = ' class="%s"' % ' '.join(classes) code = '<code%s>%s</code>' % (class_str, txt) placeholder = self.markdown.htmlStash.store(code, safe=True) return placeholder
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._getLang() if pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = TextLexer() except ValueError: lexer = TextLexer() formatter = HtmlFormatter(linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n'% \ (self.css_class, class_str, txt)
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._parseHeader() if pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = TextLexer() except ValueError: lexer = TextLexer() formatter = HtmlFormatter(linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses, hl_lines=self.hl_lines) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n'% \ (self.css_class, class_str, txt)
def fetch_lexer( source: str, language: str = None, filename: str = None, mime_type: str = None ) -> Lexer: """ :param source: :param language: :param filename: :param mime_type: :return: """ environ.abort_thread() try: if language: return get_lexer_by_name(language, stripall=True) except ClassNotFound: pass if filename: try: return get_lexer_for_filename(filename, stripall=True) except ClassNotFound: pass try: return guess_lexer_for_filename(filename, source, stripall=True) except ClassNotFound: pass try: if mime_type: return get_lexer_for_mimetype(mime_type, stripall=True) except ClassNotFound: pass try: return guess_lexer(source, stripall=True) except ClassNotFound: return TextLexer()
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._parseHeader() if pygments and self.use_pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = get_lexer_by_name('text') except ValueError: lexer = get_lexer_by_name('text') formatter = get_formatter_by_name('html', linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses, hl_lines=self.hl_lines, linenostart = self.linenostart) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n' % \ (self.css_class, class_str, txt)
def write_html_content(content_type, content, formatter, html): # dedent content but still keeps empty lines old_class = formatter.cssclass nlines = len(content) content = dedent('\n'.join(content)) # ' ' to keep pygments from removing empty lines # split, merge by \n can introduce one additional line content = [' \n' if x == '' else x + '\n' for x in content.split('\n')][:nlines] # if content_type == 'COMMENT': formatter.cssclass = 'source blob-code sos-comment' html.write('\n'.join(highlight(x, SoS_Lexer(), formatter) for x in content)) elif content_type in ('REPORT', 'report'): formatter.cssclass = 'source blob-code sos-report' html.write('{}\n'.format(highlight(''.join(content), TextLexer(), formatter))) elif content_type == 'SECTION': formatter.cssclass = 'source blob-code sos-header' html.write('{}\n'.format(highlight(''.join(content), SoS_Lexer(), formatter))) elif content_type == 'DIRECTIVE': formatter.cssclass = 'source blob-code sos-directive' html.write('{}\n'.format(highlight(''.join(content), SoS_Lexer(), formatter))) elif content_type == 'STATEMENT': formatter.cssclass = 'source blob-code sos-statement' html.write('{}\n'.format(highlight(''.join(content), SoS_Lexer(), formatter))) elif content_type == 'ERROR': formatter.cssclass = 'source blob-code sos-error ' html.write('{}\n'.format(highlight(''.join(content), SoS_Lexer(), formatter))) else: formatter.cssclass = 'source blob-code sos-script ' if content_type == 'run': content_type = 'bash' elif content_type == 'node': content_type = 'JavaScript' elif content_type == 'report': content_type = 'text' try: lexer = get_lexer_by_name(content_type) except Exception: try: lexer = guess_lexer(''.join(content)) except Exception: lexer = TextLexer() html.write('{}\n'.format(highlight((''.join(content)), lexer, formatter))) formatter.cssclass = old_class # # utility function #