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) -> Iterator[Codeblock]

Yields CodeBlocks from a Markdown source.

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) -> str

Returns an ANSI syntax-highlighted version of the given Zig source code.

Example usage:

from pathlib import Path

from zig_codeblocks import highlight_zig_code

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

process_markdown

def process_markdown(source: str, *, only_code: bool = False) -> str

Returns a Markdown source with Zig code blocks syntax-highlighted. 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.

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.1.1.tar.gz (532.4 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.1.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for zig_codeblocks-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e039e46aa0f2e35944ef15a512a26ef8649923532c8d9f8e8def5c1e42a498d4
MD5 16a217a1711e7611e6da55ad82b0c914
BLAKE2b-256 c5145f5da9143d3e8243d277f75115224aece77728acc791faefacdf78cea0b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for zig_codeblocks-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eaecfca26757cd9794450aa59994c187b2be38cf57a58d24e37a7af173ddbbc0
MD5 ca0075fff68b2daadc2ab3d01dddccfa
BLAKE2b-256 573ea54a10f563e55291e1fac3a82595b34bee68fdb06358adc7683e6e87a47b

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