Python pygments.lexers.guess_lexer_for_filename() Examples
The following are 8
code examples of pygments.lexers.guess_lexer_for_filename().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pygments.lexers
, or try the search function
.
Example #1
Source File: snippets.py From diff_cover with Apache License 2.0 | 6 votes |
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)
Example #2
Source File: utility.py From pinnwand with MIT License | 5 votes |
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)
Example #3
Source File: test_basic_api.py From pygments with BSD 2-Clause "Simplified" License | 5 votes |
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
Example #4
Source File: GeneratePatchesAction.py From coala with GNU Affero General Public License v3.0 | 5 votes |
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
Example #5
Source File: test_lexer.py From jsx-lexer with MIT License | 5 votes |
def test_guess_lexer_for_filename(self): guessed_lexer = lexers.guess_lexer_for_filename("test.jsx", text) self.assertEqual(guessed_lexer.name, JsxLexer.name)
Example #6
Source File: syntax.py From rich with MIT License | 4 votes |
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, )
Example #7
Source File: syntax_highlighting.py From cauldron with MIT License | 4 votes |
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()
Example #8
Source File: diff2HtmlCompare.py From diff2HtmlCompare with MIT License | 4 votes |
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