size: 959 B

1const std = @import("std");
2const mem = std.mem;
3const t = std.testing;
4
5pub fn toHtml(arena: mem.Allocator, text: []const u8) ![]const u8 {
6 var child = std.process.Child.init(&.{"cmark"}, arena);
7 child.stdin_behavior = .Pipe;
8 child.stdout_behavior = .Pipe;
9 child.stderr_behavior = .Pipe;
10
11 var stdout: std.ArrayList(u8) = .empty;
12 var stderr: std.ArrayList(u8) = .empty;
13
14 try child.spawn();
15 errdefer {
16 _ = child.kill() catch {};
17 }
18 try child.stdin.?.writeAll(text);
19 child.stdin.?.close();
20 child.stdin = null;
21 try child.collectOutput(arena, &stdout, &stderr, 64 * 1024);
22 _ = try child.wait();
23
24 return stdout.items;
25}
26
27test "toHtml" {
28 var arena_impl = std.heap.ArenaAllocator.init(t.allocator_instance.allocator());
29 defer arena_impl.deinit();
30 const arena = arena_impl.allocator();
31
32 try t.expectEqualStrings(
33 "<h1>foo</h1>\n",
34 try toHtml(arena, "# foo"),
35 );
36}