Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fjebaker/md4zig
Zig wrapper around md4c for parsing Markdown.
https://github.com/fjebaker/md4zig
markdown md4c parser zig
Last synced: about 1 month ago
JSON representation
Zig wrapper around md4c for parsing Markdown.
- Host: GitHub
- URL: https://github.com/fjebaker/md4zig
- Owner: fjebaker
- License: mit
- Created: 2024-05-06T18:46:27.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-11-24T22:26:05.000Z (2 months ago)
- Last Synced: 2024-12-21T18:42:40.143Z (about 1 month ago)
- Topics: markdown, md4c, parser, zig
- Language: Zig
- Homepage:
- Size: 17.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# md4zig
A Zig wrapper around [md4c](https://github.com/mity/md4c) for parsing Markdown.
Exposes a single function that puts a parser type together from comptime-known Zig functions. The parses itself also exposes only a single `parse` function. The generalisation to using vtables is left as an exercise to the user.
## Usage
```zig
const std = @import("std");const md4zig = @import("md4zig");
const MD4CParser = md4zig.MD4CParser;
const BlockInfo = md4zig.BlockInfo;
const SpanInfo = md4zig.SpanInfo;
const Text = md4zig.Text;const Impl = struct {
pub fn enterBlock(_: *Impl, block: BlockInfo) !void {
std.debug.print(">> {any}\n", .{block});
}
pub fn leaveBlock(_: *Impl, block: BlockInfo) !void {
std.debug.print("<< {any}\n", .{block});
}
pub fn enterSpan(_: *Impl, span: SpanInfo) !void {
std.debug.print(">> {any}\n", .{span});
}
pub fn leaveSpan(_: *Impl, span: SpanInfo) !void {
std.debug.print("<< {any}\n", .{span});
}
pub fn textCallback(_: *Impl, text: Text) !void {
std.debug.print(" {any}\n", .{text});
}
};pub fn main() !void {
var impl: TestImpl = .{};
var parser = MD4CParser(TestImpl).init(.{});
try parser.parse(&impl, "# Hello World\nHow are *you*!");
}
```This will then output something like:
```
>> main.BlockInfo{ .doc = void }
>> main.BlockInfo{ .h = main.BlockInfo.BlockInfo__struct_7319{ .level = 1 } }
main.Text{ .text = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 }, .text_type = main.TextType.normal }
<< main.BlockInfo{ .h = main.BlockInfo.BlockInfo__struct_7319{ .level = 1 } }
>> main.BlockInfo{ .p = void }
main.Text{ .text = { 72, 111, 119, 32, 97, 114, 101, 32 }, .text_type = main.TextType.normal }
>> main.SpanInfo{ .em = void }
main.Text{ .text = { 121, 111, 117 }, .text_type = main.TextType.normal }
<< main.SpanInfo{ .em = void }
main.Text{ .text = { 33 }, .text_type = main.TextType.normal }
<< main.BlockInfo{ .p = void }
<< main.BlockInfo{ .doc = void }
```## Including in a project
Use the Zig package manager
```bash
zig fetch --save=md4zig https://github.com/fjebaker/md4zig/archive/master.tar.gz
```Then include the module in your `build.zig` for your target:
```zig
const md4zig = b.dependency(
"md4zig",
.{ .optimize = optimize, .target = target },
).module("md4zig");// ...
exe.root_module.addImport("md4zig", md4zig);
```