const std = @import("std"); const fern = @import("fern"); const md4c = @import("md4c"); const zlua = @import("zlua"); const Lua = zlua.Lua; const maivi = @import("../root.zig"); pub fn lua_openmd4c(l: *Lua) !i32 { l.createTable(0, 1); l.pushFunction(zlua.wrap(tohtml)); l.setField(-2, "tohtml"); return 1; } const FLAGS: c_int = 0 | // md4c.MD_FLAG_HIGHLIGHT | // md4c.MD_FLAG_INSERT | md4c.MD_FLAG_NOINDENTEDCODEBLOCKS | // md4c.MD_FLAG_SPOILERS | md4c.MD_FLAG_STRIKETHROUGH | // md4c.MD_FLAG_SUBSCRIPTS | // md4c.MD_FLAG_SUPERSCRIPTS | md4c.MD_FLAG_TABLES | md4c.MD_FLAG_TASKLISTS; pub fn tohtml(l: *Lua) !i32 { l.argCheck(l.isString(1), 1, "string"); const Output = struct { var buf: std.ArrayList(u8) = .empty; pub fn process_output(chunk: [*c]const md4c.MD_CHAR, len: md4c.MD_SIZE, _: ?*anyopaque) callconv(.c) void { buf.appendSlice(maivi.allocator, chunk[0..len]) catch |err| fern.err().err(err).log(); } }; Output.buf = try .initCapacity(maivi.allocator, 0x400); defer Output.buf.deinit(maivi.allocator); const input = try l.toString(1); const ret = md4c.md_html( input, @intCast(input.len), &Output.process_output, null, FLAGS, 0, ); if (ret == -1) l.raiseErrorStr("error while parsing markdown input", .{}); const buf = try Output.buf.toOwnedSlice(maivi.allocator); defer maivi.allocator.free(buf); _ = l.pushString(buf); return 1; }