gitmyhub

zig-codeblocks

Rust ★ 2 updated 24d ago

Zig ANSI syntax highlighting library for Python

zig-codeblocks Explained

This library lets Python developers add colorful syntax highlighting to Zig code snippets. When you write Zig code in a Markdown file and want to display it somewhere like Discord (which doesn't natively support Zig syntax highlighting), this tool converts your code into a formatted version with ANSI color codes—the standard way to add colors to text in terminals and chat applications.

The library provides a few main functions. The simplest is highlight_zig_code, which takes raw Zig source code and returns the same code with colors injected. If you're working with Markdown files that contain multiple code blocks in different languages, process_markdown finds all the Zig code blocks and highlights only those while leaving the rest of the document untouched. There's also extract_codeblocks if you just want to pull out and inspect the code blocks themselves without highlighting them yet.

You can customize how the code looks through a "theme"—essentially a list of color and style choices for different parts of Zig syntax, like keywords, strings, and numbers. The library comes with a default theme that's been tuned to look good, but you can tweak any color or add bold or underline formatting if you want something different. The README includes examples showing how to modify colors for specific token types.

The library is built in Rust but packaged as a Python extension, which means it runs quickly without requiring users to install Rust themselves. It's installable from PyPI with a single pip command and even comes with an optional command-line tool if you want to process files directly from the terminal without writing Python code.