本文整理汇总了Python中pygments.lexer.Lexer方法的典型用法代码示例。如果您正苦于以下问题:Python lexer.Lexer方法的具体用法?Python lexer.Lexer怎么用?Python lexer.Lexer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygments.lexer
的用法示例。
在下文中一共展示了lexer.Lexer方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print_lexer
# 需要导入模块: from pygments import lexer [as 别名]
# 或者: from pygments.lexer import Lexer [as 别名]
def print_lexer(
body: str, lexer: Lexer, label: str = None, prefix: str = None, indent: int = None
):
if COLORIZE:
prefix_str = prefix + " " if prefix else ""
if prefix_str or indent:
prefix_body = prefix_str + " " * (indent or 0)
lexer.add_filter(PrefixFilter(prefix=prefix_body))
tokens = list(pygments.lex(body, lexer=lexer))
if label:
fmt_label = [("fg:ansimagenta", label)]
if prefix_str:
fmt_label.insert(0, ("", prefix_str))
print_formatted(FormattedText(fmt_label))
print_formatted(PygmentsTokens(tokens))
else:
print_ext(body, label=label, prefix=prefix)
示例2: get_lexers
# 需要导入模块: from pygments import lexer [as 别名]
# 或者: from pygments.lexer import Lexer [as 别名]
def get_lexers(main_lex, exam_lex, tool_lex):
""" gets all the lexer wrappers """
if not main_lex:
return None, None, None
lexer = None
if issubclass(main_lex, PromptLex):
lexer = main_lex
elif issubclass(main_lex, PygLex):
lexer = PygmentsLexer(main_lex)
if exam_lex:
if issubclass(exam_lex, PygLex):
exam_lex = PygmentsLexer(exam_lex)
if tool_lex:
if issubclass(tool_lex, PygLex):
tool_lex = PygmentsLexer(tool_lex)
return lexer, exam_lex, tool_lex
示例3: from_pygments_lexer_cls
# 需要导入模块: from pygments import lexer [as 别名]
# 或者: from pygments.lexer import Lexer [as 别名]
def from_pygments_lexer_cls(cls, lexer_cls: "PygmentsLexerCls") -> "RegexSync":
"""
Create a :class:`.RegexSync` instance for this Pygments lexer class.
"""
patterns = {
# For Python, start highlighting at any class/def block.
"Python": r"^\s*(class|def)\s+",
"Python 3": r"^\s*(class|def)\s+",
# For HTML, start at any open/close tag definition.
"HTML": r"<[/a-zA-Z]",
# For javascript, start at a function.
"JavaScript": r"\bfunction\b"
# TODO: Add definitions for other languages.
# By default, we start at every possible line.
}
p = patterns.get(lexer_cls.name, "^")
return cls(p)
示例4: __init__
# 需要导入模块: from pygments import lexer [as 别名]
# 或者: from pygments.lexer import Lexer [as 别名]
def __init__(
self,
pygments_lexer_cls: Type["PygmentsLexerCls"],
sync_from_start: FilterOrBool = True,
syntax_sync: Optional[SyntaxSync] = None,
) -> None:
self.pygments_lexer_cls = pygments_lexer_cls
self.sync_from_start = to_filter(sync_from_start)
# Instantiate the Pygments lexer.
self.pygments_lexer = pygments_lexer_cls(
stripnl=False, stripall=False, ensurenl=False
)
# Create syntax sync instance.
self.syntax_sync = syntax_sync or RegexSync.from_pygments_lexer_cls(
pygments_lexer_cls
)
示例5: fetch_lexer
# 需要导入模块: from pygments import lexer [as 别名]
# 或者: from pygments.lexer import Lexer [as 别名]
def fetch_lexer(
source: str,
language: str = None,
filename: str = None,
mime_type: str = None
) -> Lexer:
"""
:param source:
:param language:
:param filename:
:param mime_type:
:return:
"""
environ.abort_thread()
try:
if language:
return get_lexer_by_name(language, stripall=True)
except ClassNotFound:
pass
if filename:
try:
return get_lexer_for_filename(filename, stripall=True)
except ClassNotFound:
pass
try:
return guess_lexer_for_filename(filename, source, stripall=True)
except ClassNotFound:
pass
try:
if mime_type:
return get_lexer_for_mimetype(mime_type, stripall=True)
except ClassNotFound:
pass
try:
return guess_lexer(source, stripall=True)
except ClassNotFound:
return TextLexer()