diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-20 14:07:29 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-20 14:07:29 +0200 |
| commit | 87046ba0fc5b9c3be61d23bd8cc2bc6fa84577ba (patch) | |
| tree | c53336a0dcc223819986ddba016b31cc32e73de9 /go | |
| parent | db2f49115713c47b48587022511b3654a7042493 (diff) | |
feat(go): bindings
Diffstat (limited to 'go')
| -rw-r--r-- | go/.gitignore | 22 | ||||
| -rw-r--r-- | go/README.md | 6 | ||||
| -rw-r--r-- | go/build.zig | 31 | ||||
| -rw-r--r-- | go/build.zig.zon | 13 | ||||
| -rw-r--r-- | go/go.mod | 3 | ||||
| -rw-r--r-- | go/typdown.go | 45 | ||||
| l--------- | go/typdown.h | 1 |
7 files changed, 121 insertions, 0 deletions
diff --git a/go/.gitignore b/go/.gitignore new file mode 100644 index 0000000..a285bfa --- /dev/null +++ b/go/.gitignore @@ -0,0 +1,22 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work* + +# Zig +.zig-cache +zig-out diff --git a/go/README.md b/go/README.md new file mode 100644 index 0000000..27b03ad --- /dev/null +++ b/go/README.md @@ -0,0 +1,6 @@ +# typdown-go + +Go package containing CGO bindings for typdown. + +The application including this package must be statically linked. +An example `build.zig` and `build.zig.zon` is provided to build it with Zig's build. diff --git a/go/build.zig b/go/build.zig new file mode 100644 index 0000000..5e6b59d --- /dev/null +++ b/go/build.zig @@ -0,0 +1,31 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) !void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const typdown = b.dependency("typdown", .{ + .optimize = optimize, + .target = target, + }).module("typdown"); + const lib = b.addLibrary(.{ + .name = "typdown", + .root_module = typdown, + .linkage = .static, + }); + const install = b.addInstallArtifact(lib, .{}); + // when emitting headers will be fixed + // currently, we have to use a symlink/copy to get it + //installed.emitted_h = lib.getEmittedH(); + b.getInstallStep().dependOn(&install.step); + + var flags = try std.ArrayList(u8).initCapacity(b.allocator, 2); + try flags.appendSlice(b.allocator, "-linkmode external -extldflags -static"); + if (optimize != .Debug) try flags.appendSlice(b.allocator, " -s"); + const go_build = b.addSystemCommand(&[_][]const u8{ + "go", "build", + "-ldflags", try flags.toOwnedSlice(b.allocator), + ".", + }); + b.getInstallStep().dependOn(&go_build.step); +} diff --git a/go/build.zig.zon b/go/build.zig.zon new file mode 100644 index 0000000..b9c4c83 --- /dev/null +++ b/go/build.zig.zon @@ -0,0 +1,13 @@ +.{ + .name = .go, + .version = "0.0.1", + .dependencies = .{ + .typdown = .{ + .url = "../", + .hash = "typdown-0.0.0-W_41P72ZAADdFceFUk8ED63kaCObehOJxT9HpRTabF_k", + }, + }, + .minimum_zig_version = "0.15.2", + .paths = .{""}, + .fingerprint = 0xb6689356e0fb1477, +} diff --git a/go/go.mod b/go/go.mod new file mode 100644 index 0000000..90ee119 --- /dev/null +++ b/go/go.mod @@ -0,0 +1,3 @@ +module github.com/anhgelus/typdown/go + +go 1.26.2 diff --git a/go/typdown.go b/go/typdown.go new file mode 100644 index 0000000..8d8fff7 --- /dev/null +++ b/go/typdown.go @@ -0,0 +1,45 @@ +package typdown + +// #cgo LDFLAGS: -L${SRCDIR}/zig-out/lib/ -ltypdown +// #include <stdlib.h> +// #include "typdown.h" +import "C" +import ( + "errors" + "html/template" + "unsafe" +) + +var ( + codeErrors = map[uint8]error{ + 1: errors.New("out of memory"), + 2: ErrInvalidUtf8, + 3: ErrNotSupported, + 4: ErrModifierNotClosed, + 5: ErrInvalidTitleContent, + 6: ErrIllegalPlacement, + 7: ErrInvalidLink, + } + ErrInvalidUtf8 = errors.New("invalid UTF-8") + ErrNotSupported = errors.New("feature not supported") + ErrModifierNotClosed = errors.New("modifier not closed") + ErrInvalidTitleContent = errors.New("invalid title content") + ErrIllegalPlacement = errors.New("illegal placement") + ErrInvalidLink = errors.New("invalid link") +) + +func Parse(content string) (template.HTML, error) { + code := C.uchar(0) + conv := C.CString(content) + raw := C.parse(conv, &code) + defer C.free(unsafe.Pointer(conv)) + if code > 0 { + err := codeErrors[uint8(code)] + if code == 1 { + panic(err) + } + return "", err + } + defer C.free(unsafe.Pointer(raw)) + return template.HTML(C.GoString(raw)), nil +} diff --git a/go/typdown.h b/go/typdown.h new file mode 120000 index 0000000..de5b0ee --- /dev/null +++ b/go/typdown.h @@ -0,0 +1 @@ +../include/typdown.h
\ No newline at end of file |
