-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmrmlgo.go
More file actions
136 lines (106 loc) · 3.42 KB
/
Copy pathmrmlgo.go
File metadata and controls
136 lines (106 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package mrmlgo
/*
#cgo linux CFLAGS: -I./
#cgo linux,amd64 LDFLAGS: -L./libs/linux_x64 -lmrml_capi -lm -ldl -lpthread -lgcc_s -lc -Wl,--allow-multiple-definition
#cgo linux,386 LDFLAGS: -L./libs/linux_x86 -lmrml_capi -lm -ldl -lpthread -lgcc_s -lc -Wl,--allow-multiple-definition
#cgo linux,arm64 LDFLAGS: -L./libs/linux_arm64 -lmrml_capi -lm -ldl -lpthread -lgcc_s -lc -Wl,--allow-multiple-definition
#cgo windows,amd64 LDFLAGS: ./libs/windows_x64/libmrml_capi.a -lkernel32 -ladvapi32 -lntdll -luserenv -lws2_32 -ldbghelp
#cgo windows,386 LDFLAGS: ./libs/windows_x86/libmrml_capi.a -lkernel32 -ladvapi32 -lntdll -luserenv -lws2_32 -ldbghelp
#cgo darwin,arm64 LDFLAGS: -L./libs/darwin_arm64 -lmrml_capi
#cgo darwin,amd64 LDFLAGS: -L./libs/darwin_x64 -lmrml_capi
#include "./mrml-capi/include/mrml-capi.h"
*/
import "C"
import (
"errors"
"runtime"
"unsafe"
)
type ParserOptions struct {
cParserOptions *C.ParserOptions
includeLoaders map[string]string
}
type ParserOutput struct {
cParserOutput *C.ParserOutput
}
func NewParseOptions(includeLoaders map[string]string) (*ParserOptions, error) {
c := &ParserOptions{
includeLoaders: includeLoaders,
}
C.new_parser_options(&c.cParserOptions)
// Add Loaders
for key, value := range includeLoaders {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
C.add_memory_loader(c.cParserOptions, cKey, cValue)
}
runtime.SetFinalizer(c, (*ParserOptions).Destory)
return c, nil
}
func (c *ParserOptions) ParseJSON(input string) (*ParserOutput, error) {
cGoInput := C.CString(input)
defer C.free(unsafe.Pointer(cGoInput))
runtime.KeepAlive(c)
ret := &ParserOutput{}
results := C.parse_json(c.cParserOptions, cGoInput, &ret.cParserOutput)
if results != C.ParserSuccess {
return nil, errors.New(C.GoString(C.mrml_last_error()))
}
runtime.SetFinalizer(ret, (*ParserOutput).Destory)
return ret, nil
}
func (c *ParserOptions) ParseMJML(input string) (*ParserOutput, error) {
cGoInput := C.CString(input)
defer C.free(unsafe.Pointer(cGoInput))
runtime.KeepAlive(c)
ret := &ParserOutput{}
results := C.parse_mjml(c.cParserOptions, cGoInput, &ret.cParserOutput)
if results != C.ParserSuccess {
return nil, errors.New(C.GoString(C.mrml_last_error()))
}
runtime.SetFinalizer(ret, (*ParserOutput).Destory)
return ret, nil
}
func (c *ParserOutput) ToHTML() (string, error) {
var ret *C.char
defer C.free(unsafe.Pointer(ret))
results := C.to_html(c.cParserOutput, &ret)
if results != C.RenderSuccess {
return "", errors.New("rendering error")
}
return C.GoString(ret), nil
}
func (c *ParserOutput) ToJSON() (string, error) {
var ret *C.char
defer C.free(unsafe.Pointer(ret))
results := C.to_json(c.cParserOutput, &ret)
if results != C.RenderSuccess {
return "", errors.New("rendering error")
}
return C.GoString(ret), nil
}
func (c *ParserOutput) ToMJML() (string, error) {
var ret *C.char
defer C.free(unsafe.Pointer(ret))
results := C.to_mjml(c.cParserOutput, &ret)
if results != C.RenderSuccess {
return "", errors.New("rendering error")
}
return C.GoString(ret), nil
}
func (c *ParserOutput) Destory() {
if c.cParserOutput != nil {
C.destroy_parser_output(c.cParserOutput)
c.cParserOutput = nil
}
runtime.SetFinalizer(c, nil)
}
func (c *ParserOptions) Destory() {
if c.cParserOptions != nil {
C.destroy_parser_options(c.cParserOptions)
c.cParserOptions = nil
}
runtime.SetFinalizer(c, nil)
}