当前位置: 首页>>代码示例>>Python>>正文


Python lexer.RegexLexer方法代码示例

本文整理汇总了Python中pygments.lexer.RegexLexer方法的典型用法代码示例。如果您正苦于以下问题:Python lexer.RegexLexer方法的具体用法?Python lexer.RegexLexer怎么用?Python lexer.RegexLexer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pygments.lexer的用法示例。


在下文中一共展示了lexer.RegexLexer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_lexer_classes

# 需要导入模块: from pygments import lexer [as 别名]
# 或者: from pygments.lexer import RegexLexer [as 别名]
def test_lexer_classes(cls):
    # test that every lexer class has the correct public API
    assert type(cls.name) is str
    for attr in 'aliases', 'filenames', 'alias_filenames', 'mimetypes':
        assert hasattr(cls, attr)
        assert type(getattr(cls, attr)) is list, \
            "%s: %s attribute wrong" % (cls, attr)
    result = cls.analyse_text("abc")
    assert isinstance(result, float) and 0.0 <= result <= 1.0
    result = cls.analyse_text(".abc")
    assert isinstance(result, float) and 0.0 <= result <= 1.0

    assert all(al.lower() == al for al in cls.aliases)

    if issubclass(cls, RegexLexer):
        inst = cls(opt1="val1", opt2="val2")
        if not hasattr(cls, '_tokens'):
            # if there's no "_tokens", the lexer has to be one with
            # multiple tokendef variants
            assert cls.token_variants
            for variant in cls.tokens:
                assert 'root' in cls.tokens[variant]
        else:
            assert 'root' in cls._tokens, \
                   '%s has no root state' % cls 
开发者ID:pygments,项目名称:pygments,代码行数:27,代码来源:test_basic_api.py

示例2: fill_read_cache

# 需要导入模块: from pygments import lexer [as 别名]
# 或者: from pygments.lexer import RegexLexer [as 别名]
def fill_read_cache(self, filename: str, line: int) -> None:
        try:
            lexer = pygments.lexers.get_lexer_for_filename(
                str(filename)
            )  # type: RegexLexer
            formatter_opts = dict(linenos="inline", linespans="line", hl_lines=[line])
            html_formatter = pygments.formatters.get_formatter_by_name(
                "html", **formatter_opts
            )
            css = html_formatter.get_style_defs(".highlight")
            with open(str(filename)) as f:
                lines = f.readlines()
            if len(lines) < 1000:
                content = "".join(lines)
                tokens = lexer.get_tokens(content)
                source = pygments.format(tokens, html_formatter)
                self.file_cache[filename][line] = (css, source)
                self.file_read_cache[filename] = (lexer, content, False)
            else:
                minl = max(0, line - 30)
                maxl = min(len(lines), line + 30)
                formatter_opts = dict(
                    linenos="inline", linespans="line", hl_lines=[line]
                )
                html_formatter = pygments.formatters.get_formatter_by_name(
                    "html", **formatter_opts
                )
                css = html_formatter.get_style_defs(".highlight")
                source = pygments.format(
                    lexer.get_tokens("".join(lines[minl:maxl])), html_formatter
                )
                self.file_cache[filename][line] = (css, source)
                self.file_read_cache[filename] = (lexer, lines, True)
        except Exception as e:
            l.exception(e)
            self.file_cache[filename][line] = (None, None)
            self.file_read_cache[filename] = (None, None, False) 
开发者ID:hase-project,项目名称:hase,代码行数:39,代码来源:__init__.py


注:本文中的pygments.lexer.RegexLexer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。