Accessible Pygments Styles

0.0.5 · active · verified Thu Apr 09

A collection of accessible Pygments styles, currently at version 0.0.5. It provides themes that meet WCAG 2.1 color contrast requirements, aiming to make syntax highlighting more inclusive for users with low vision or color blindness. The library is actively maintained with regular minor releases adding new themes and improvements.

Warnings

Install

Imports

Quickstart

This example demonstrates how to highlight Python code using the `a11y-light` style from `accessible-pygments`. The key is to import `accessible_pygments` to register its styles, then refer to the style by its string name when creating a Pygments formatter. The `full=True` option generates a complete HTML document including the stylesheet.

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
import accessible_pygments # This registers the styles

code_to_highlight = """
def greet(name):
    print(f"Hello, {name}!")

greet("World")
"""

# Choose an accessible style, e.g., 'a11y-light', 'github-dark-colorblind'
style_name = 'a11y-light'

# Create an HTML formatter with the chosen accessible style
formatter = HtmlFormatter(style=style_name, full=True, encoding='utf-8')

# Highlight the code
highlighted_code = highlight(
    code_to_highlight,
    PythonLexer(),
    formatter
)

# The highlighted_code is now a bytes object (due to encoding='utf-8' in full=True mode)
# Decode to string if you want to print or save to a text file
print(highlighted_code.decode('utf-8'))

# To save to an HTML file (example)
# with open("highlighted_code.html", "wb") as f:
#     f.write(highlighted_code)

view raw JSON →