我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用pygments.formatters.get_formatter_by_name()。
def parse_command_line(self): """Handle command-line parsing and internal configuration.""" self.main.parse_args() self.formatter = get_formatter_by_name( 'terminal256' if self.main.options.colorize else 'null', style=GNAThubOutputStyle, encoding='utf-8') self.testcases = Testsuite.compute_testcases_list(self.main.args) self.testcases = sorted(self.testcases, key=lambda s: s.lower()) if self.main.options.discs: self.discs.extend(self.main.options.discs.split(',')) setup_result_dir(self.main.options)
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 hljsx(code): lexer = get_lexer_by_name('jsx') formatter = get_formatter_by_name('html') hl = highlight(code, lexer, formatter) if isinstance(hl, str): return hl return hl.encode('utf-8')
def render(self, value): src = value['code'].strip('\n') lang = value['language'] lexer = get_lexer_by_name(lang) formatter = get_formatter_by_name( 'html', linenos='table', cssclass='code-highlight', style='default', noclasses=False, ) return mark_safe(highlight(src, 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 [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)