Claude Code for Pygments: Python Syntax Highlighting — Claude Skills 360 Blog
Blog / AI / Claude Code for Pygments: Python Syntax Highlighting
AI

Claude Code for Pygments: Python Syntax Highlighting

Published: February 13, 2028
Read time: 5 min read
By: Claude Skills 360

Pygments highlights source code in HTML and terminal output. pip install pygments. Basic: from pygments import highlight; from pygments.lexers import get_lexer_by_name; from pygments.formatters import HtmlFormatter. highlight(code, get_lexer_by_name("python"), HtmlFormatter()). Lexer: get_lexer_by_name("python", stripall=True). get_lexer_by_name("javascript"). get_lexer_by_name("sql"). guess_lexer(code) — auto-detect language. get_lexer_for_filename("app.py"). get_lexer_for_mimetype("text/x-python"). HtmlFormatter: HtmlFormatter(style="monokai", full=True, linenos=True, cssclass="highlight"). full=True produces standalone HTML with <html>. full=False (default) produces <div class="highlight">. linenos="table" — line numbers in table cells. linenos="inline". CSS: HtmlFormatter(style="monokai").get_style_defs(".highlight"). Styles: from pygments.styles import get_all_styles; list(get_all_styles()) — monokai, solarized-dark, dracula, github-dark, nord, vs, emacs, friendly. Terminal: from pygments.formatters import TerminalFormatter, Terminal256Formatter. highlight(code, lexer, TerminalFormatter()). highlight(code, lexer, Terminal256Formatter(style="monokai")). Token inspection: from pygments.formatters import RawTokenFormatter. lex(code, lexer) → token stream. list(lexer.get_tokens(code)). Custom lexer: subclass RegexLexer, define tokens dict with regex rules. Claude Code generates Pygments highlight pipelines, HTML renderers, and terminal colorizers.

CLAUDE.md for Pygments

## Pygments Stack
- Version: Pygments >= 2.17 | pip install pygments
- One-shot: highlight(code, get_lexer_by_name("python", stripall=True), HtmlFormatter(style="monokai"))
- CSS: HtmlFormatter(style="monokai").get_style_defs(".highlight") — inject into <style>
- Linenos: HtmlFormatter(linenos="table", anchorlinenos=True, lineanchors="line")
- Terminal: Terminal256Formatter(style="monokai") — needs 256-color terminal
- Guess: guess_lexer(code) for auto-detection | get_lexer_for_filename("file.py")
- Full HTML: HtmlFormatter(full=True, style="monokai") — standalone page

Pygments Syntax Highlighting Pipeline

# app/highlight.py — Pygments HTML and terminal syntax highlighting
from __future__ import annotations

import textwrap
from pathlib import Path
from typing import Any

from pygments import highlight, lex
from pygments.formatter import Formatter
from pygments.formatters import (
    HtmlFormatter,
    RawTokenFormatter,
    Terminal256Formatter,
    TerminalFormatter,
)
from pygments.lexer import RegexLexer
from pygments.lexers import (
    get_lexer_by_name,
    get_lexer_for_filename,
    get_lexer_for_mimetype,
    guess_lexer,
)
from pygments.styles import get_all_styles
from pygments.token import Comment, Keyword, Name, Number, Operator, String, Token
from pygments.util import ClassNotFound


# ─────────────────────────────────────────────────────────────────────────────
# 1. HTML highlighting
# ─────────────────────────────────────────────────────────────────────────────

def highlight_html(
    code: str,
    language: str = "python",
    style: str = "monokai",
    linenos: bool = False,
    cssclass: str = "highlight",
) -> str:
    """
    Render code as an HTML <div class="highlight"> block.
    Pair with get_css() to inject the stylesheet once per page.

    linenos=True → "table" style (line numbers in a separate column).
    """
    try:
        lexer = get_lexer_by_name(language, stripall=True)
    except ClassNotFound:
        lexer = get_lexer_by_name("text")

    formatter = HtmlFormatter(
        style=style,
        linenos="table" if linenos else False,
        cssclass=cssclass,
        nowrap=False,
    )
    return highlight(code, lexer, formatter)


def get_css(style: str = "monokai", cssclass: str = "highlight") -> str:
    """
    Return the CSS rules for the given style.
    Inject once into the page <style> tag; reuse the same cssclass across snippets.
    """
    return HtmlFormatter(style=style, cssclass=cssclass).get_style_defs(f".{cssclass}")


def highlight_html_full(
    code: str,
    language: str = "python",
    style: str = "monokai",
    title: str = "",
) -> str:
    """
    Produce a standalone HTML page (with <html>, <head>, <body>).
    HtmlFormatter(full=True) embeds all CSS inline.
    """
    lexer = get_lexer_by_name(language, stripall=True)
    formatter = HtmlFormatter(
        style=style,
        full=True,
        linenos="table",
        title=title or f"{language} code",
    )
    return highlight(code, lexer, formatter)


def highlight_with_anchors(
    code: str,
    language: str = "python",
    style: str = "friendly",
) -> str:
    """
    Line numbers with anchor links — e.g. #line-15 links to line 15.
    Useful for documentation pages where you want to deep-link to lines.
    """
    lexer = get_lexer_by_name(language, stripall=True)
    formatter = HtmlFormatter(
        style=style,
        linenos="table",
        anchorlinenos=True,
        lineanchors="line",
        cssclass="highlight",
    )
    return highlight(code, lexer, formatter)


# ─────────────────────────────────────────────────────────────────────────────
# 2. Terminal (ANSI) highlighting
# ─────────────────────────────────────────────────────────────────────────────

def highlight_terminal(code: str, language: str = "python") -> str:
    """
    Highlight for terminal output using 16-color ANSI escape codes.
    TerminalFormatter works on all UNIX terminals; no style choice.
    """
    lexer = get_lexer_by_name(language, stripall=True)
    return highlight(code, lexer, TerminalFormatter())


def highlight_terminal_256(
    code: str,
    language: str = "python",
    style: str = "monokai",
) -> str:
    """
    256-color terminal highlighting — richer palette.
    Terminal256Formatter accepts full Pygments style names.
    """
    lexer = get_lexer_by_name(language, stripall=True)
    return highlight(code, lexer, Terminal256Formatter(style=style))


# ─────────────────────────────────────────────────────────────────────────────
# 3. Language detection
# ─────────────────────────────────────────────────────────────────────────────

def detect_and_highlight(code: str, filename: str | None = None) -> dict[str, str]:
    """
    Auto-detect language from filename or source content, then highlight.
    Returns {"language", "html", "detected"}.
    """
    if filename:
        try:
            lexer = get_lexer_for_filename(filename, stripall=True)
        except ClassNotFound:
            lexer = guess_lexer(code)
    else:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            lexer = get_lexer_by_name("text")

    html = highlight(
        code,
        lexer,
        HtmlFormatter(style="github-dark", cssclass="highlight"),
    )
    return {
        "language": lexer.name,
        "html":     html,
        "aliases":  lexer.aliases[:3] if lexer.aliases else [],
    }


# ─────────────────────────────────────────────────────────────────────────────
# 4. Token stream inspection
# ─────────────────────────────────────────────────────────────────────────────

def get_token_types(code: str, language: str = "python") -> list[dict[str, str]]:
    """
    Expose the Pygments token stream — each (token_type, value) pair.
    Useful for building custom renderers or for analysis tools.
    """
    lexer = get_lexer_by_name(language, stripall=True)
    return [
        {"type": str(ttype), "value": repr(value)}
        for ttype, value in lex(code, lexer)
        if value.strip()
    ]


# ─────────────────────────────────────────────────────────────────────────────
# 5. Custom lexer — minimal example (INI-style config)
# ─────────────────────────────────────────────────────────────────────────────

class EnvFileLexer(RegexLexer):
    """
    Simple .env file lexer.
    RegexLexer maps regex patterns to token types.
    """
    name    = "Env"
    aliases = ["env", "dotenv"]
    filenames = [".env", "*.env"]

    tokens = {
        "root": [
            (r"#.*\n",                      Comment),           # # comment
            (r"export\s+",                  Keyword),           # export
            (r"[A-Z_][A-Z0-9_]*(?==)",      Name.Variable),     # KEY
            (r"=",                          Operator),          # =
            (r'"[^"]*"',                    String.Double),     # "value"
            (r"'[^']*'",                    String.Single),     # 'value'
            (r"[^\s\n]+",                   String),            # unquoted value
            (r"\n",                         Token.Text),
        ],
    }


def highlight_env(env_text: str) -> str:
    """Highlight a .env file using the custom EnvFileLexer."""
    return highlight(
        env_text,
        EnvFileLexer(stripall=True),
        HtmlFormatter(style="friendly", cssclass="highlight"),
    )


# ─────────────────────────────────────────────────────────────────────────────
# 6. Available styles
# ─────────────────────────────────────────────────────────────────────────────

def list_styles() -> list[str]:
    return sorted(get_all_styles())


# ─────────────────────────────────────────────────────────────────────────────
# 7. Jinja2 / Flask integration
# ─────────────────────────────────────────────────────────────────────────────

def register_pygments_filter(env, style: str = "monokai") -> None:
    """
    Register a Jinja2 filter so templates can do:
      {{ post.code_snippet | highlight("python") | safe }}
    """
    def _filter(code: str, language: str = "python") -> str:
        return highlight_html(code, language=language, style=style)

    env.filters["highlight"] = _filter


def highlight_file(path: Path, style: str = "monokai") -> str:
    """Highlight a source file by detecting its language from the extension."""
    code = path.read_text(encoding="utf-8", errors="replace")
    return detect_and_highlight(code, filename=path.name)["html"]


# ─────────────────────────────────────────────────────────────────────────────
# Demo
# ─────────────────────────────────────────────────────────────────────────────

PYTHON_SAMPLE = textwrap.dedent("""\
    def fibonacci(n: int) -> list[int]:
        \"\"\"Return the first n Fibonacci numbers.\"\"\"
        a, b = 0, 1
        result = []
        for _ in range(n):
            result.append(a)
            a, b = b, a + b
        return result

    if __name__ == "__main__":
        print(fibonacci(10))
""")

ENV_SAMPLE = textwrap.dedent("""\
    # Application settings
    export DEBUG=false
    DATABASE_URL="postgresql://user:pass@localhost/mydb"
    SECRET_KEY='my-secret-key-here'
    PORT=8000
""")

if __name__ == "__main__":
    print("=== Available styles (first 10) ===")
    print(list_styles()[:10])

    print("\n=== Terminal (16-color) ===")
    print(highlight_terminal(PYTHON_SAMPLE))

    print("\n=== Language detection ===")
    detected = detect_and_highlight(PYTHON_SAMPLE)
    print(f"  Detected: {detected['language']}")

    print("\n=== Token stream (first 8 tokens) ===")
    tokens = get_token_types(PYTHON_SAMPLE)[:8]
    for t in tokens:
        print(f"  {t['type']:40} {t['value']}")

    print("\n=== CSS snippet (first 3 lines) ===")
    css = get_css()
    print("\n".join(css.splitlines()[:3]))

    print("\n=== .env file (custom lexer — HTML) ===")
    env_html = highlight_env(ENV_SAMPLE)
    print(f"  HTML length: {len(env_html)} chars")

For the highlight.js (client-side) alternative — highlight.js runs in the browser, requires JavaScript, and rehighlights on every page load; Pygments runs on the server at build time, embeds static HTML with <span class="..."> tags, and the page renders pre-highlighted with no client-side JavaScript at all — the output is indexable by search engines and works without JS. For the rich.Syntax alternative — rich.Syntax(code, "python") is excellent for terminal output using Rich’s rendering pipeline (Live, Panel, Table), while Pygments is the correct tool when you need HTML output for a web application — HtmlFormatter(style="monokai").get_style_defs() produces a CSS file you inject once, and then every highlight(code, lexer, formatter) call returns a <div class="highlight"> block compatible with any HTML page. The Claude Skills 360 bundle includes Pygments skill sets covering highlight() with HtmlFormatter and TerminalFormatter, get_lexer_by_name / guess_lexer / get_lexer_for_filename, HtmlFormatter options including linenos/anchorlinenos/cssclass/full/style, get_style_defs() CSS extraction, Terminal256Formatter for 256-color ANSI output, lex() token stream inspection, RegexLexer subclassing for custom file formats, detect_and_highlight auto-detection, Jinja2 highlight filter registration, and highlight_file for per-extension language detection. Start with the free tier to try syntax highlighting pipeline code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free