本文整理汇总了Python中pygments.formatters.get_formatter_by_name函数的典型用法代码示例。如果您正苦于以下问题:Python get_formatter_by_name函数的具体用法?Python get_formatter_by_name怎么用?Python get_formatter_by_name使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_formatter_by_name函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: serve_language_css
def serve_language_css(style):
try:
fmt = formatters.get_formatter_by_name('html', style=style)
except:
util.log.warn(
'Style "%s" cannot be found, falling back to default' % (style,)
)
fmt = formatters.get_formatter_by_name('html')
bottle.response.content_type = 'text/css'
return fmt.get_style_defs(['.pygmentized'])
示例2: get_pygment_style
def get_pygment_style(style, css_class='codehilite'):
"""Get the specified pygments sytle CSS."""
try:
# Try and request pygments to generate it
text = get_formatter_by_name('html', style=style).get_style_defs('.' + css_class)
except Exception:
# Try and request pygments to generate default
text = get_formatter_by_name('html', style="default").get_style_defs('.' + css_class)
return '<style>\n%s\n</style>\n' % text if text is not None else ""
示例3: get_context
def get_context(self, value, parent_context=None):
context = super().get_context(value, parent_context=parent_context)
src = value.get('code') or ''
src = src.strip('\n')
lang = value.get('language') or ''
line_numbers = value['line_numbers']
if lang:
lexer = get_lexer_by_name(lang)
else:
lexer = guess_lexer(src)
formatter = get_formatter_by_name(
'html',
linenos=line_numbers,
cssclass='codehilite',
style='default',
noclasses=False,
)
context.update({
'filename': value.get('filename'),
'display_filename': value.get('display_filename'),
'language': value.get('lang'),
'code': mark_safe(highlight(src, lexer, formatter))
})
return context
示例4: hljsx
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')
示例5: get_printer
def get_printer(mode='auto'):
"""
Generate printer function.
:param mode: string: always, never or auto
:return:
"""
def printer(data):
print(data)
if mode in ('auto', 'always'):
try:
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import get_formatter_by_name
if mode == 'always' or sys.stdout.isatty():
lexer = get_lexer_by_name('json')
formatter = get_formatter_by_name('terminal256')
def printer(data):
print(highlight(data, lexer, formatter), end='', file=sys.stdout)
except ImportError as e:
if mode == 'always':
import warnings
warnings.warn('No pygments module available, cannot colorize output')
return printer
示例6: create_code_css
def create_code_css():
from pygments.formatters import get_formatter_by_name
formatter = get_formatter_by_name('html', style=kw["code_color_scheme"])
utils.makedirs(os.path.dirname(code_css_path))
with codecs.open(code_css_path, 'wb+', 'utf8') as outf:
outf.write(formatter.get_style_defs(kw["code.css_selectors"]))
outf.write(kw["code.css_close"])
示例7: diff_revs
def diff_revs(wiki, user, rev, raw=False):
diff = wiki.scm.diff(path=None, rev1=rev, rev2=None)
if not raw:
lexer = get_lexer_by_name('diff')
formatter = get_formatter_by_name('html')
diff = highlight(diff, lexer, formatter)
return {'diff': diff, 'disp_rev': rev}
示例8: process_request
def process_request(json_data):
log.debug('Processing: path: %s\n%s:%s' % (json_data['fullPath'], json_data['action'], json_data['language']))
try:
code = json_data['code']
fmter = get_formatter_by_name(json_data['outformat'], linenos = json_data['linenos'], lineanchors = json_data['lineanchors'])
fmter.encoding = json_data['encoding']
lexer = get_lexer_by_name(json_data['language'])
return highlight(code, lexer, fmter)
except ValueError as err:
log.error(err)
return '''
<h1><a href="http://pygments.org">Pygments</a> failed to highlight this file</h1>
<div>You should consider filing a bug report or provide a bug fix to help the community ;)</div>
<div>
The Error was:
<pre>%s</pre>
</div>
<div>
The code that was parsed is:
<pre>%s</pre>
</div>
''' % (err, code)
示例9: create_code_css
def create_code_css():
from pygments.formatters import get_formatter_by_name
formatter = get_formatter_by_name('html', style=kw["code_color_scheme"])
utils.makedirs(os.path.dirname(code_css_path))
with codecs.open(code_css_path, 'wb+', 'utf8') as outf:
outf.write(formatter.get_style_defs('pre.code'))
outf.write("table.codetable { width: 100%;} td.linenos {text-align: right; width: 4em;}")
示例10: format
def format(self, code, language):
if language == "":
lexer = guess_lexer(code)
else:
lexer = get_lexer_by_name(language)
formatter = get_formatter_by_name(self.formatname)
return highlight(code, lexer, formatter)
示例11: render
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=None, cssclass="codehilite", style="default", noclasses=False)
return mark_safe(highlight(src, lexer, formatter))
示例12: ConvertCodeToHtml
def ConvertCodeToHtml(self, code):
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import get_formatter_by_name
lexer = get_lexer_by_name('python')
formatter = get_formatter_by_name('html', noclasses=True)
return highlight(code, lexer, formatter)
示例13: highlight
def highlight(context, body, syntax, format='html'):
"""Looks up the appropriate Pygments lexer for a given syntax
and uses it to format the passed body.
"""
lexer = get_lexer_by_name(syntax)
formatter = get_formatter_by_name(format)
return pygments.highlight(body, lexer, formatter)
示例14: pygments_style
def pygments_style(style):
try:
string = get_formatter_by_name('html', style=style).get_style_defs('.highlight')
except ClassNotFound:
logger.warning('Unknown Pygments style %s', style)
string = '/* nothing */'
return string
示例15: traceback_code
def traceback_code(self, task):
formatter = get_formatter_by_name('html')
if sys.version_info.major == 3:
lexer = get_lexer_by_name('py3tb')
else:
lexer = get_lexer_by_name('pytb')
style = formatter.get_style_defs('.highlight')
return mark_safe('<style>{}</style><br/>{}'.format(style, highlight(task.traceback, lexer, formatter)))