本文整理汇总了Python中pygments.modeline.get_filetype_from_buffer方法的典型用法代码示例。如果您正苦于以下问题:Python modeline.get_filetype_from_buffer方法的具体用法?Python modeline.get_filetype_from_buffer怎么用?Python modeline.get_filetype_from_buffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygments.modeline
的用法示例。
在下文中一共展示了modeline.get_filetype_from_buffer方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: guess_lexer
# 需要导入模块: from pygments import modeline [as 别名]
# 或者: from pygments.modeline import get_filetype_from_buffer [as 别名]
def guess_lexer(_text, **options):
"""Guess a lexer by strong distinctions in the text (eg, shebang)."""
# try to get a vim modeline first
ft = get_filetype_from_buffer(_text)
if ft is not None:
try:
return get_lexer_by_name(ft, **options)
except ClassNotFound:
pass
best_lexer = [0.0, None]
for lexer in _iter_lexerclasses():
rv = lexer.analyse_text(_text)
if rv == 1.0:
return lexer(**options)
if rv > best_lexer[0]:
best_lexer[:] = (rv, lexer)
if not best_lexer[0] or best_lexer[1] is None:
raise ClassNotFound('no lexer matching the text found')
return best_lexer[1](**options)
示例2: guess_lexer
# 需要导入模块: from pygments import modeline [as 别名]
# 或者: from pygments.modeline import get_filetype_from_buffer [as 别名]
def guess_lexer(_text, **options):
"""Guess a lexer by strong distinctions in the text (eg, shebang)."""
if not isinstance(_text, str):
inencoding = options.get('inencoding', options.get('encoding'))
if inencoding:
_text = _text.decode(inencoding or 'utf8')
else:
_text, _ = guess_decode(_text)
# try to get a vim modeline first
ft = get_filetype_from_buffer(_text)
if ft is not None:
try:
return get_lexer_by_name(ft, **options)
except ClassNotFound:
pass
best_lexer = [0.0, None]
for lexer in _iter_lexerclasses():
rv = lexer.analyse_text(_text)
if rv == 1.0:
return lexer(**options)
if rv > best_lexer[0]:
best_lexer[:] = (rv, lexer)
if not best_lexer[0] or best_lexer[1] is None:
raise ClassNotFound('no lexer matching the text found')
return best_lexer[1](**options)
示例3: test_modelines
# 需要导入模块: from pygments import modeline [as 别名]
# 或者: from pygments.modeline import get_filetype_from_buffer [as 别名]
def test_modelines():
for buf in [
'vi: ft=python' + '\n' * 8,
'vi: ft=python' + '\n' * 8,
'\n\n\n\nvi=8: syntax=python' + '\n' * 8,
'\n' * 8 + 'ex: filetype=python',
'\n' * 8 + 'vim: some,other,syn=python\n\n\n\n'
]:
assert modeline.get_filetype_from_buffer(buf) == 'python'