A syntax highlighting package that supports over 500 programming languages and text formats with extensive output format options
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Functions for discovering, loading, and working with syntax lexers. Pygments includes lexers for over 500 programming languages and text formats.
Get a lexer instance by its alias or name.
def get_lexer_by_name(_alias: str, **options):
"""
Get a lexer instance by alias with options.
Parameters:
- _alias: Short identifier for the lexer (e.g., 'python', 'javascript', 'c++')
- **options: Lexer-specific options (stripnl, stripall, ensurenl, tabsize, encoding)
Returns:
Lexer instance configured with the given options
Raises:
ClassNotFound: If no lexer with that alias is found
"""Usage example:
from pygments.lexers import get_lexer_by_name
# Get Python lexer
python_lexer = get_lexer_by_name('python')
# Get JavaScript lexer with custom options
js_lexer = get_lexer_by_name('javascript', tabsize=4, stripall=True)
# Get C++ lexer
cpp_lexer = get_lexer_by_name('cpp')Get a lexer based on filename patterns, with optional code analysis.
def get_lexer_for_filename(_fn: str, code=None, **options):
"""
Get a lexer for a filename, optionally using code analysis.
Parameters:
- _fn: File name or path to determine lexer
- code: Optional code content for disambiguation
- **options: Lexer-specific options
Returns:
Lexer instance best matching the filename and code
Raises:
ClassNotFound: If no suitable lexer is found
"""Usage example:
from pygments.lexers import get_lexer_for_filename
# Get lexer based on file extension
lexer = get_lexer_for_filename('script.py') # Returns Python lexer
lexer = get_lexer_for_filename('app.js') # Returns JavaScript lexer
lexer = get_lexer_for_filename('main.cpp') # Returns C++ lexer
# Use code content for disambiguation
with open('ambiguous_file', 'r') as f:
code = f.read()
lexer = get_lexer_for_filename('ambiguous_file', code=code)Guess the appropriate lexer based on code analysis.
def guess_lexer(_text: str, **options):
"""
Guess lexer based on text analysis using analyse_text() methods.
Parameters:
- _text: Source code to analyze
- **options: Lexer-specific options
Returns:
Lexer instance with highest confidence score
Raises:
ClassNotFound: If no lexer can analyze the text
"""Usage example:
from pygments.lexers import guess_lexer
# Guess based on code content only
code = '''
function hello() {
console.log("Hello, World!");
}
'''
lexer = guess_lexer(code) # Likely returns JavaScript lexerCombined filename and content analysis for better accuracy.
def guess_lexer_for_filename(_fn: str, _text: str, **options):
"""
Guess lexer using both filename and text analysis.
Parameters:
- _fn: File name for initial lexer detection
- _text: Source code for analysis and disambiguation
- **options: Lexer-specific options
Returns:
Lexer instance with highest combined confidence score
Raises:
ClassNotFound: If no suitable lexer is found
"""def get_lexer_for_mimetype(_mime: str, **options):
"""
Get lexer instance by MIME type.
Parameters:
- _mime: MIME type (e.g., 'text/x-python', 'application/javascript')
- **options: Lexer-specific options
Returns:
Lexer instance for the given MIME type
Raises:
ClassNotFound: If no lexer handles that MIME type
"""Usage example:
from pygments.lexers import guess_lexer_for_filename, get_lexer_for_mimetype
# Guess using both filename and content (most accurate)
with open('script.js', 'r') as f:
code = f.read()
lexer = guess_lexer_for_filename('script.js', code)
# Get lexer by MIME type
lexer = get_lexer_for_mimetype('text/x-python')
lexer = get_lexer_for_mimetype('application/javascript')List all available lexers with their metadata.
def get_all_lexers(plugins: bool = True) -> Iterator[tuple[str, list[str], list[str], list[str]]]:
"""
Return generator of all available lexers.
Parameters:
- plugins: Include plugin lexers from entry points (default True)
Yields:
Tuples of (name, aliases, filenames, mimetypes) for each lexer
"""Usage example:
from pygments.lexers import get_all_lexers
# List all lexers
for name, aliases, filenames, mimetypes in get_all_lexers():
print(f"{name}: {', '.join(aliases)}")
if filenames:
print(f" Files: {', '.join(filenames)}")
if mimetypes:
print(f" MIME: {', '.join(mimetypes)}")
# Find Python-related lexers
python_lexers = [
(name, aliases) for name, aliases, _, _ in get_all_lexers()
if any('python' in alias.lower() for alias in aliases)
]Find lexer classes without instantiating them.
def find_lexer_class(name: str):
"""
Find lexer class by name without instantiation.
Parameters:
- name: Full lexer name (not alias)
Returns:
Lexer class or None if not found
"""def find_lexer_class_by_name(_alias: str):
"""
Find lexer class by alias without instantiation.
Parameters:
- _alias: Lexer alias
Returns:
Lexer class
Raises:
ClassNotFound: If no lexer with that alias is found
"""def find_lexer_class_for_filename(_fn: str, code=None):
"""
Find lexer class for filename without instantiation.
Parameters:
- _fn: File name for lexer detection
- code: Optional code for analysis
Returns:
Lexer class or None if not found
"""Load custom lexers from files.
def load_lexer_from_file(filename: str, lexername: str = "CustomLexer", **options):
"""
Load custom lexer from file.
Parameters:
- filename: Path to Python file containing lexer class
- lexername: Name of lexer class in file (default "CustomLexer")
- **options: Options passed to lexer constructor
Returns:
Custom lexer instance
Raises:
ClassNotFound: If file cannot be loaded or class not found
"""Usage example:
from pygments.lexers import load_lexer_from_file
# Load custom lexer
custom_lexer = load_lexer_from_file('my_lexer.py', 'MyLanguageLexer')All lexers accept these standard options:
stripnl (bool): Strip leading/trailing newlines (default True)stripall (bool): Strip all leading/trailing whitespace (default False)ensurenl (bool): Ensure input ends with newline (default True)tabsize (int): Tab size for expansion (default 0, no expansion)encoding (str): Input encoding for byte stringsInstall with Tessl CLI
npx tessl i tessl/pypi-pygments