| 750 | |
| 751 | @classmethod |
| 752 | def _guess_lexer(cls, filename: str, code: str) -> str: |
| 753 | ext = os.path.splitext(filename)[-1] |
| 754 | if not ext: |
| 755 | # No extension, look at first line to see if it is a hashbang |
| 756 | # Note, this is an educated guess and not a guarantee |
| 757 | # If it fails, the only downside is that the code is highlighted strangely |
| 758 | new_line_index = code.index("\n") |
| 759 | first_line = code[:new_line_index] if new_line_index != -1 else code |
| 760 | if first_line.startswith("#!") and "python" in first_line.lower(): |
| 761 | return "python" |
| 762 | try: |
| 763 | return cls.LEXERS.get(ext) or guess_lexer_for_filename(filename, code).name |
| 764 | except ClassNotFound: |
| 765 | return "text" |
| 766 | |
| 767 | @group() |
| 768 | def _render_stack(self, stack: Stack) -> RenderResult: |