Skip to main content

Zig ANSI syntax highlighting library

Project description

uv Ruff

zig-codeblocks

zig-codeblocks is a CPython 3.10+ library for adding syntax highlighting to Zig code blocks in Markdown files through ANSI escape codes. Originally intended for patching the lack of syntax highlighting for Zig on Discord.

Installation

zig-codeblocks is available on PyPI:

pip install zig-codeblocks

You can also install it from source:

pip install git+https://github.com/trag1c/zig-codeblocks.git

API Reference

extract_codeblocks

def extract_codeblocks(source: str | bytes) -> Iterator[CodeBlock]

Yields CodeBlocks from a Markdown source. Assumes UTF-8 if source is bytes.

Example usage:

from pathlib import Path

from zig_codeblocks import extract_codeblocks

source = Path("examples/riiz.md").read_text()
for codeblock in extract_codeblocks(source):
    print(f"Language: {codeblock.lang}")
    print(f"Body:\n{codeblock.body}")
Language: py
Body:
print("Hello, World!")

Language: zig
Body:
const std = @import("std");
pub fn main() !void {
    std.debug.print("Hello, World!\n", .{});
}

highlight_zig_code

def highlight_zig_code(source: str | bytes, theme: Theme = DEFAULT_THEME) -> str

Returns an ANSI syntax-highlighted version of the given Zig source code. Assumes UTF-8 if source is bytes. An optional Theme can be supplied (defaults to DEFAULT_THEME).

Example usage:

from pathlib import Path

from zig_codeblocks import DEFAULT_THEME, Color, Style, highlight_zig_code

source = Path("examples/hello_world.zig").read_text()

theme = DEFAULT_THEME.copy()
theme.builtin_identifiers = Style(Color.ORANGE, underline=True)
theme.strings = Style(Color.CYAN)
theme.types = None

print(
    highlight_zig_code(source),
    highlight_zig_code(source, theme),
    sep="\n\n",
)

process_markdown

def process_markdown(
    source: str | bytes,
    theme: Theme = DEFAULT_THEME,
    *,
    only_code: bool = False,
) -> str

Returns a Markdown source with Zig code blocks syntax-highlighted. Assumes UTF-8 if source is bytes. An optional Theme can be supplied (defaults to DEFAULT_THEME). If only_code is True, only processed Zig code blocks will be returned.

Example usage:

from pathlib import Path

from zig_codeblocks import process_markdown

source = Path("examples/riiz.md").read_text()
print(process_markdown(source))

CodeBlock

class CodeBlock(NamedTuple):
    lang: str
    body: str

A code block extracted from a Markdown source.

Color

class Color(Enum):
    GRAY = "30"
    RED = "31"
    GREEN = "32"
    ORANGE = "33"
    BLUE = "34"
    MAGENTA = "35"
    CYAN = "36"
    WHITE = "37"  # Black for light mode

An enumeration of 3-bit ANSI colors. Some names were adjusted to match Discord's style.

Style

@dataclass(slots=True, frozen=True)
class Style:
    color: Color
    _: KW_ONLY
    bold: bool = False
    underline: bool = False

A style for syntax highlighting. Takes a Color and can optionally be bold and/or underlined. Immutable. Produces an SGR sequence when converted to a string.

Theme

@dataclass(slots=True, kw_only=True)
class Theme:
    builtin_identifiers: Style | None = None
    calls: Style | None = None
    comments: Style | None = None
    identifiers: Style | None = None
    keywords: Style | None = None
    numeric: Style | None = None
    strings: Style | None = None
    primitive_values: Style | None = None
    types: Style | None = None

A theme for syntax highlighting Zig code. Each field is optional and can be provided a Style to apply to the corresponding token type.

Theme.copy

def copy(self) -> Theme

Returns a copy of the theme.

DEFAULT_THEME

The default theme used for highlighting, defined as follows:

DEFAULT_THEME = Theme(
    builtin_identifiers=Style(Color.BLUE, bold=True),
    calls=Style(Color.BLUE),
    comments=Style(Color.GRAY),
    identifiers=None,
    keywords=Style(Color.MAGENTA),
    numeric=Style(Color.CYAN),
    primitive_values=Style(Color.CYAN),
    strings=Style(Color.GREEN),
    types=Style(Color.ORANGE),
)

License

zig-codeblocks is licensed under the MIT License.
© trag1c, 2025

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zig_codeblocks-0.2.0.tar.gz (524.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zig_codeblocks-0.2.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file zig_codeblocks-0.2.0.tar.gz.

File metadata

  • Download URL: zig_codeblocks-0.2.0.tar.gz
  • Upload date:
  • Size: 524.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.27

File hashes

Hashes for zig_codeblocks-0.2.0.tar.gz
Algorithm Hash digest
SHA256 995e61eae6681a64ed5d8dde066e88b45c8c987386aec2e4657aae0c09ca1e73
MD5 0220c2addf91657b8725b8c006bf0c4b
BLAKE2b-256 d1b85e97186e877fc301933d2132562de84d147ff9e3f6e71bb2136b996c545c

See more details on using hashes here.

File details

Details for the file zig_codeblocks-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for zig_codeblocks-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea1534a0d63635f4e57d79ce911d458f754cb333d9545716fb3b9ef23631c4fe
MD5 cd6cc9e8d3871e54779a190035ecfe86
BLAKE2b-256 4183eda6581f6042ec43883a5d99f2cd8e2ad0ceeb649badf17e095944ce9443

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page