zig-gemtext - A new gemini text parser
2021-03-05
Introduction to a new gemini text library
Intro
In the last two days I created a new library called zig-gemtext which provides both spec-compliant parsing as well as rendering gemini text into the following formats:
- gemini text ("autoformatting")
- rich text
- html
- markdown
The library is written in Zig, but exposes both a Zig and C api, so it can be used from pretty much any programming language or platform. In theory, the library should be compilable to wasm as well and be used from there.
The parser itself is a non-blocking streaming parser, so it's perfectly suitable for any asynchronous or stream processing, also combinable with the renderer.
You can get the source of the library here:
github.com/MasterQ32/zig-gemtext
Usage
Here's a small usage example of the library:
pub fn main() !void { var document = try gemtext.Document.parse( std.heap.page_allocator, std.io.getStdIn().reader(), ); defer document.deinit(); try gemtext.renderer.html( document.fragments.items, std.io.getStdOut().writer(), ); }
Performance
I tested the library with a pretty naive benchmark and got those results:
148489113 Bytes (148 MB, 142 MiB) copied, 6,13719 s, 24,2 MB/s
- 1392348 lines in total
- 6,13719 seconds
- 226870 lines per second
- 140 MB source
- 24.2 MB/s
I think it can be faster, but it's good enough for now.