aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-05-01 18:33:16 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2026-05-01 18:33:21 +0200
commit3e00c224007db19cdc7869435967c5decea570a5 (patch)
tree2e08f1a91e5930c1cc6509707581c83b8508c128 /src
parenta9e8d0e9c929bec830b086e473ef1362e1f873d9 (diff)
feat(lib): introduce document notion in C ABI
Diffstat (limited to 'src')
-rw-r--r--src/eval/math.zig2
-rw-r--r--src/root.zig30
2 files changed, 26 insertions, 6 deletions
diff --git a/src/eval/math.zig b/src/eval/math.zig
index fa95918..071287e 100644
--- a/src/eval/math.zig
+++ b/src/eval/math.zig
@@ -1,5 +1,5 @@
const std = @import("std");
-const typst = @cImport(@cInclude("typdown_typst.h"));
+const typst = @import("typst");
const Allocator = std.mem.Allocator;
const HTML = Element.HTML;
const Element = @import("Element.zig");
diff --git a/src/root.zig b/src/root.zig
index f7899a7..3a7c134 100644
--- a/src/root.zig
+++ b/src/root.zig
@@ -54,16 +54,28 @@ var default_alloc: std.mem.Allocator =
/// Parse the content.
/// Code is a pointer to an u8 populated with an error code > 0.
///
-/// Returns a not null strings and set the code to 0 if everything is fine.
+/// Returns a non-null void pointer containing the document and set the code to 0 if everything is fine.
/// Else, it returns null and set an error code above 0.
/// Use typdown_getErrorString to retrieve the string linked with the error code.
/// Use parse if you are in Zig.
-export fn typdown_parse(content: [*:0]const u8, code: *u8) ?[*:0]const u8 {
- const doc = parse(default_alloc, std.mem.span(content)) catch |err| {
+///
+/// You must free the document with typdown_free.
+export fn typdown_parse(content: [*:0]const u8, code: *u8) ?*anyopaque {
+ return parse(default_alloc, std.mem.span(content)) catch |err| {
code.* = getErrorCode(err);
return null;
};
- defer doc.deinit();
+}
+
+/// Free the document.
+export fn typdown_free(document: *anyopaque) void {
+ const doc: *Document = @ptrCast(@alignCast(document));
+ doc.deinit();
+}
+
+/// Render an HTML from the document.
+export fn typdown_renderHTML(document: *anyopaque, code: *u8) ?[*:0]const u8 {
+ const doc: *Document = @ptrCast(@alignCast(document));
const res = doc.renderHTML(default_alloc) catch |err| {
code.* = getErrorCode(err);
return null;
@@ -88,7 +100,15 @@ fn doTest(content: [*:0]const u8, exp: []const u8, comptime exp_code: u8) !void
const expect = std.testing.expect;
var code: u8 = undefined;
- const raw = typdown_parse(content, &code) orelse {
+ const doc = typdown_parse(content, &code) orelse {
+ expect(code == exp_code) catch |err| {
+ std.debug.print("{}\n", .{code});
+ return err;
+ };
+ return;
+ };
+ defer typdown_free(doc);
+ const raw = typdown_renderHTML(doc, &code) orelse {
expect(code == exp_code) catch |err| {
std.debug.print("{}\n", .{code});
return err;