aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.zig2
-rw-r--r--go/.gitignore22
-rw-r--r--go/README.md6
-rw-r--r--go/build.zig31
-rw-r--r--go/build.zig.zon13
-rw-r--r--go/go.mod3
-rw-r--r--go/typdown.go45
l---------go/typdown.h1
8 files changed, 123 insertions, 0 deletions
diff --git a/build.zig b/build.zig
index d00aedd..03c07d7 100644
--- a/build.zig
+++ b/build.zig
@@ -33,6 +33,8 @@ pub fn build(b: *std.Build) void {
example.root_module.linkLibrary(lib);
example.root_module.addIncludePath(b.path("include"));
+ b.getInstallStep().dependOn(&installed_lib.step);
+
const mod_tests = b.addTest(.{
.root_module = mod,
});
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