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


Python lexers.guess_lexer_for_filename方法代码示例

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


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

示例1: guess_language

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer_for_filename [as 别名]
def guess_language(raw: str, filename: Optional[str] = None) -> str:
    options = {'stripnl': True}

    # Guess a lexer based on filename and raw text first
    if filename:
        try:
            return guess_lexer_for_filename(filename, raw, **options).aliases[0]
        except (ValueError, IndexError):
            pass

    # If that didn't work guess lexer just by looking at the raw text
    try:
        language = guess_lexer(raw, **options).aliases[0]
    except (ValueError, IndexError):
        # If no lexer was detected, fallback to plain text.
        return 'text'

    # These are odd lexers that match far too often, so exclude them.
    if language in GUESS_LANG_IGNORES:
        return 'text'

    # Finally check for language overrides and return
    return GUESS_LANG_OVERRIDES.get(language, language) 
开发者ID:supakeen,项目名称:pinnwand,代码行数:25,代码来源:utility.py

示例2: _parse_src

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer_for_filename [as 别名]
def _parse_src(cls, src_contents, src_filename):
        """
        Return a stream of `(token_type, value)` tuples
        parsed from `src_contents` (str)

        Uses `src_filename` to guess the type of file
        so it can highlight syntax correctly.
        """

        # Parse the source into tokens
        try:
            lexer = guess_lexer_for_filename(src_filename, src_contents)
        except ClassNotFound:
            lexer = TextLexer()

        # Ensure that we don't strip newlines from
        # the source file when lexing.
        lexer.stripnl = False

        return pygments.lex(src_contents, lexer) 
开发者ID:Bachmann1234,项目名称:diff_cover,代码行数:22,代码来源:snippets.py

示例3: test_get_lexers

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer_for_filename [as 别名]
def test_get_lexers():
    # test that the lexers functions work
    for func, args in [(lexers.get_lexer_by_name, ("python",)),
                       (lexers.get_lexer_for_filename, ("test.py",)),
                       (lexers.get_lexer_for_mimetype, ("text/x-python",)),
                       (lexers.guess_lexer, ("#!/usr/bin/python3 -O\nprint",)),
                       (lexers.guess_lexer_for_filename, ("a.py", "<%= @foo %>"))
                       ]:
        x = func(opt='val', *args)
        assert isinstance(x, lexers.PythonLexer)
        assert x.options["opt"] == "val"

    for cls, (_, lname, aliases, _, mimetypes) in lexers.LEXERS.items():
        assert cls == lexers.find_lexer_class(lname).__name__

        for alias in aliases:
            assert cls == lexers.get_lexer_by_name(alias).__class__.__name__

        for mimetype in mimetypes:
            assert cls == lexers.get_lexer_for_mimetype(mimetype).__class__.__name__

    try:
        lexers.get_lexer_by_name(None)
    except ClassNotFound:
        pass
    else:
        raise Exception 
开发者ID:pygments,项目名称:pygments,代码行数:29,代码来源:test_basic_api.py

示例4: find_language

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer_for_filename [as 别名]
def find_language(filename):
    """
    Find the language used in `filename`.

    :param filename: The name of the file.
    :return:         The language used.
    """

    return guess_lexer_for_filename(filename, 'Error, no file '
                                    'found').name 
开发者ID:coala,项目名称:coala,代码行数:12,代码来源:GeneratePatchesAction.py

示例5: test_guess_lexer_for_filename

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer_for_filename [as 别名]
def test_guess_lexer_for_filename(self):
        guessed_lexer = lexers.guess_lexer_for_filename("test.jsx", text)
        self.assertEqual(guessed_lexer.name, JsxLexer.name) 
开发者ID:fcurella,项目名称:jsx-lexer,代码行数:5,代码来源:test_lexer.py

示例6: from_path

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer_for_filename [as 别名]
def from_path(
        cls,
        path: str,
        encoding: str = "utf-8",
        theme: Union[str, Type[PygmentsStyle]] = DEFAULT_THEME,
        dedent: bool = True,
        line_numbers: bool = False,
        line_range: Tuple[int, int] = None,
        start_line: int = 1,
        highlight_lines: Set[int] = None,
        code_width: Optional[int] = None,
        tab_size: int = 4,
        word_wrap: bool = False,
    ) -> "Syntax":
        """Construct a Syntax object from a file.
        
        Args:
            path (str): Path to file to highlight.
            encoding (str): Encoding of file.
            lexer_name (str): Lexer to use (see https://pygments.org/docs/lexers/)
            theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "emacs".
            dedent (bool, optional): Enable stripping of initial whitespace. Defaults to True.
            line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False.
            start_line (int, optional): Starting number for line numbers. Defaults to 1.
            line_range (Tuple[int, int], optional): If given should be a tuple of the start and end line to render.
            highlight_lines (Set[int]): A set of line numbers to highlight.
            code_width: Width of code to render (not including line numbers), or ``None`` to use all available width.
            tab_size (int, optional): Size of tabs. Defaults to 4.
            word_wrap (bool, optional): Enable word wrapping of code.

        Returns:
            [Syntax]: A Syntax object that may be printed to the console
        """
        with open(path, "rt", encoding=encoding) as code_file:
            code = code_file.read()
        try:
            lexer = guess_lexer_for_filename(path, code)
            lexer_name = lexer.name
        except ClassNotFound:
            lexer_name = "default"
        return cls(
            code,
            lexer_name,
            theme=theme,
            dedent=dedent,
            line_numbers=line_numbers,
            line_range=line_range,
            start_line=start_line,
            highlight_lines=highlight_lines,
            code_width=code_width,
            word_wrap=word_wrap,
        ) 
开发者ID:willmcgugan,项目名称:rich,代码行数:54,代码来源:syntax.py

示例7: fetch_lexer

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer_for_filename [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() 
开发者ID:sernst,项目名称:cauldron,代码行数:46,代码来源:syntax_highlighting.py

示例8: format

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer_for_filename [as 别名]
def format(self, options):
        self.diffs = self.getDiffDetails(self.fromfile, self.tofile)

        if options.verbose:
            for diff in self.diffs:
                print("%-6s %-80s %-80s" % (diff[2], diff[0], diff[1]))

        fields = ((self.leftcode, True, self.fromfile),
                  (self.rightcode, False, self.tofile))

        codeContents = []
        for (code, isLeft, filename) in fields:

            inst = DiffHtmlFormatter(isLeft,
                                     self.diffs,
                                     nobackground=False,
                                     linenos=True,
                                     style=options.syntax_css)

            try:
                self.lexer = guess_lexer_for_filename(self.filename, code)

            except pygments.util.ClassNotFound:
                if options.verbose:
                    print("No Lexer Found! Using default...")

                self.lexer = DefaultLexer()

            formatted = pygments.highlight(code, self.lexer, inst)

            codeContents.append(formatted)

        answers = {
            "html_title":     self.filename,
            "reset_css":      self.resetCssFile,
            "pygments_css":   self.pygmentsCssFile % options.syntax_css,
            "diff_css":       self.diffCssFile,
            "page_title":     self.filename,
            "original_code":  codeContents[0],
            "modified_code":  codeContents[1],
            "jquery_js":      self.jqueryJsFile,
            "diff_js":        self.diffJsFile,
            "page_width":     "page-80-width" if options.print_width else "page-full-width"
        }

        self.htmlContents = HTML_TEMPLATE % answers 
开发者ID:wagoodman,项目名称:diff2HtmlCompare,代码行数:48,代码来源:diff2HtmlCompare.py


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