本文整理汇总了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)