當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。