本文整理匯總了Python中pygments.formatters.get_formatter_by_name方法的典型用法代碼示例。如果您正苦於以下問題:Python formatters.get_formatter_by_name方法的具體用法?Python formatters.get_formatter_by_name怎麽用?Python formatters.get_formatter_by_name使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pygments.formatters
的用法示例。
在下文中一共展示了formatters.get_formatter_by_name方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: formatter
# 需要導入模塊: from pygments import formatters [as 別名]
# 或者: from pygments.formatters import get_formatter_by_name [as 別名]
def formatter(self):
colors = _get_term_color_support()
if self.debug:
sys.stderr.write("Detected support for %s colors\n" % colors)
if colors == 256:
fmt_options = {'style': self.style}
elif self.style in ('light', 'dark'):
fmt_options = {'bg': self.style}
else:
fmt_options = {'bg': 'dark'}
from pygments.formatters import get_formatter_by_name
import pygments.util
fmt_alias = 'terminal256' if colors == 256 else 'terminal'
try:
return get_formatter_by_name(fmt_alias, **fmt_options)
except pygments.util.ClassNotFound as ex:
if self.debug:
sys.stderr.write(str(ex) + "\n")
return get_formatter_by_name(fmt_alias)
示例2: highlighted_operations
# 需要導入模塊: from pygments import formatters [as 別名]
# 或者: from pygments.formatters import get_formatter_by_name [as 別名]
def highlighted_operations(self):
from pygments.lexers import get_lexer_by_name
from pygments.util import ClassNotFound
from pygments import highlight
from pygments.formatters import get_formatter_by_name
from wordinserter import parse
import warnings
try:
formatter = get_formatter_by_name("html")
lexer = get_lexer_by_name(self.highlight)
except ClassNotFound:
warnings.warn("Lexer {0} or formatter html not found, not highlighting".format(self.highlight))
return None
formatter.noclasses = True
highlighted_code = highlight(self.text, lexer=lexer, formatter=formatter)
return parse(highlighted_code, parser="html")
示例3: __fancy
# 需要導入模塊: from pygments import formatters [as 別名]
# 或者: from pygments.formatters import get_formatter_by_name [as 別名]
def __fancy(text, language, fmt):
if not configs._on_rtd and __USE_PYGMENTS:
try:
lang_lex = lexers.find_lexer_class_by_name(language)
fmt = formatters.get_formatter_by_name(fmt)
highlighted = pygments.highlight(text, lang_lex(), fmt)
return highlighted
except:
return text
else:
return text
示例4: test_get_formatters
# 需要導入模塊: from pygments import formatters [as 別名]
# 或者: from pygments.formatters import get_formatter_by_name [as 別名]
def test_get_formatters():
# test that the formatters functions work
x = formatters.get_formatter_by_name("html", opt="val")
assert isinstance(x, formatters.HtmlFormatter)
assert x.options["opt"] == "val"
x = formatters.get_formatter_for_filename("a.html", opt="val")
assert isinstance(x, formatters.HtmlFormatter)
assert x.options["opt"] == "val"
示例5: hilite
# 需要導入模塊: from pygments import formatters [as 別名]
# 或者: from pygments.formatters import get_formatter_by_name [as 別名]
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)
示例6: hilite
# 需要導入模塊: from pygments import formatters [as 別名]
# 或者: from pygments.formatters import get_formatter_by_name [as 別名]
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,
wrapcode=True)
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)