CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pygments

A syntax highlighting package that supports over 500 programming languages and text formats with extensive output format options

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

style-management.mddocs/

Style Management

Functions for working with color schemes and visual styles that define how different token types are rendered in formatted output.

Capabilities

Style Discovery

Get a style class by name.

def get_style_by_name(name: str):
    """
    Get style class by name.
    
    Parameters:
    - name: Style name (e.g., 'default', 'github', 'monokai', 'vim')
    
    Returns:
    Style class that can be used with formatters
    
    Raises:
    ClassNotFound: If no style with that name is found
    """

Usage example:

from pygments.styles import get_style_by_name
from pygments.formatters import HtmlFormatter

# Get specific styles
default_style = get_style_by_name('default')
github_style = get_style_by_name('github')
monokai_style = get_style_by_name('monokai')

# Use with formatter
formatter = HtmlFormatter(style=github_style)
# Or by name
formatter = HtmlFormatter(style='github')

Style Enumeration

List all available styles.

def get_all_styles() -> Iterator[str]:
    """
    Return generator of all available style names.
    
    Yields:
    Style name strings for both built-in and plugin styles
    """

Usage example:

from pygments.styles import get_all_styles

# List all available styles
print("Available styles:")
for style_name in get_all_styles():
    print(f"  {style_name}")

# Find dark themes
dark_styles = [name for name in get_all_styles() 
               if 'dark' in name.lower() or name in ['monokai', 'vim', 'native']]

Built-in Styles

Pygments includes many built-in styles:

Light Themes

  • default - Default Pygments style
  • emacs - Emacs editor style
  • friendly - Friendly light style
  • friendly_grayscale - Grayscale friendly style
  • colorful - Colorful light style
  • autumn - Autumn colors
  • murphy - Murphy style
  • manni - Manni style
  • material - Material design inspired
  • perldoc - Perl documentation style
  • pastie - Pastie web service style
  • borland - Borland IDE style
  • trac - Trac style
  • igor - Igor Pro style
  • vs - Visual Studio style
  • xcode - Xcode style

Dark Themes

  • monokai - Popular dark theme
  • vim - Vim editor dark theme
  • native - Native terminal style
  • fruity - Fruity dark colors
  • github-dark - GitHub dark theme
  • dracula - Dracula theme
  • gruvbox-dark - Gruvbox dark variant
  • nord - Nord color palette
  • onedark - One Dark theme
  • zenburn - Zenburn style

Specialized Themes

  • bw - Black and white (no colors)
  • rrt - Radically Reduced Theme
  • solarized-dark / solarized-light - Solarized color scheme
  • paraiso-dark / paraiso-light - Paraiso themes
  • rainbow_dash - Colorful rainbow theme

Style Usage

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
from pygments.styles import get_style_by_name

code = '''
def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)
'''

lexer = PythonLexer()

# Different styles produce different visual output
styles_to_try = ['default', 'github', 'monokai', 'vim', 'colorful']

for style_name in styles_to_try:
    formatter = HtmlFormatter(style=style_name, cssclass=f'highlight-{style_name}')
    result = highlight(code, lexer, formatter)
    print(f"=== {style_name.upper()} STYLE ===")
    print(result[:200] + "...")

Style Properties

Each style defines colors and formatting for token types:

from pygments.styles import get_style_by_name
from pygments.token import Keyword, String, Comment

style = get_style_by_name('monokai')

# Access style definitions (internal API)
print(f"Keyword color: {style.style_for_token(Keyword)}")
print(f"String color: {style.style_for_token(String)}")  
print(f"Comment color: {style.style_for_token(Comment)}")

Custom Styles

You can create custom styles by subclassing the Style class:

from pygments.style import Style
from pygments.token import *

class MyCustomStyle(Style):
    name = 'mycustom'
    
    styles = {
        Comment:                'italic #75715e',
        Keyword:                'bold #66d9ef',
        Name:                   '#f8f8f2',
        Name.Attribute:         '#a6e22e',
        Name.Class:             'bold #a6e22e',
        Name.Function:          '#a6e22e',
        Number:                 '#ae81ff',
        Operator:               '#f92672',
        String:                 '#e6db74',
        Generic.Deleted:        '#f92672',
        Generic.Inserted:       '#a6e22e',
    }

# Use custom style
from pygments.formatters import HtmlFormatter
formatter = HtmlFormatter(style=MyCustomStyle)

Style Format

Style definitions use this format:

  • Colors: hex colors (#ff0000), ANSI colors (ansiblue), or color names
  • Modifiers: bold, italic, underline, nobold, noitalic, noinherit
  • Background: bg:#ffffff for background colors

Examples:

  • 'bold #ff0000' - Bold red text
  • 'italic #008000' - Italic green text
  • 'bg:#ffff00 #000000' - Black text on yellow background
  • 'underline #0000ff' - Underlined blue text
  • 'noinherit' - Don't inherit parent styles

Error Handling

  • ClassNotFound: No style found with the specified name
  • ImportError: Issues loading custom style modules

Install with Tessl CLI

npx tessl i tessl/pypi-pygments

docs

command-line.md

custom-components.md

filter-system.md

formatter-management.md

high-level-api.md

index.md

lexer-management.md

style-management.md

tile.json