diff --git a/cat.webp b/cat.webp new file mode 100644 index 0000000..6b225c7 Binary files /dev/null and b/cat.webp differ diff --git a/tengo.go b/tengo.go index b343476..9f56723 100644 --- a/tengo.go +++ b/tengo.go @@ -43,13 +43,6 @@ func (tengo *Tengo) SetPreCompile(fn func(*Script)) *Tengo { return tengo } -func (pp *Tengo) Tags() [2]string { - return [2]string{ - "{{", - "}}", - } -} - // Simple Evaler implementation for the Tengo language func (pp *Tengo) Eval( ctx context.Context, @@ -59,22 +52,27 @@ func (pp *Tengo) Eval( ) ([][]byte, error) { var fullCodeBuf bytes.Buffer const retHead = ` - __ret_one__ := "" - printf := func(format, ...vals) { - __ret_one__ += sprintf(format, vals...) + __ret_one__ := bytes("") + pp := { + printf : func(format, ...vals) { + __ret_one__ += __sprintf__(format, vals...) + }, + + print : func(...vals) { + __ret_one__ += __sprint__(vals...) + }, + println : func(...vals) { + __ret_one__ += __sprintln__(vals...) + }, + write_raw : func(bts) { + __ret_one__ += bytes(bts) + } } - print := func(...vals) { - __ret_one__ += sprint(vals...) - } - - println := func(...vals) { - __ret_one__ += sprintln(vals...) - } ` const retSeparator = ` - __separate(__ret_one__) + __separate__(__ret_one__) __ret_one__ = "" ` rets := [][]byte{} @@ -96,7 +94,7 @@ func (pp *Tengo) Eval( pp.preCompile(script) } - err := script.Add("sprintf", &tengo.UserFunction{ + err := script.Add("__sprintf__", &tengo.UserFunction{ Value: func(args ...tengo.Object) (tengo.Object, error){ if len(args) < 1 { return nil, tengo.ErrWrongNumArguments @@ -113,52 +111,52 @@ func (pp *Tengo) Eval( //fmt.Printf("shit: %q\n", gargs[i]) } str := fmt.Sprintf(format, gargs...) - return tengo.FromInterface(str) + return tengo.FromInterface([]byte(str)) }, }, ) if err != nil { return nil, err } - err = script.Add("sprint", &tengo.UserFunction{ + err = script.Add("__sprint__", &tengo.UserFunction{ Value: func(args ...tengo.Object) (tengo.Object, error){ gargs := make([]any, len(args)) for i := range gargs { gargs[i] = tengo.ToInterface(args[i]) } str := fmt.Sprint(gargs...) - return tengo.FromInterface(str) + return tengo.FromInterface([]byte(str)) }, }, ) if err != nil { return nil, err } - err = script.Add("sprintln", &tengo.UserFunction{ + err = script.Add("__sprintln__", &tengo.UserFunction{ Value: func(args ...tengo.Object) (tengo.Object, error){ gargs := make([]any, len(args)) for i := range gargs { gargs[i] = tengo.ToInterface(args[i]) } str := fmt.Sprintln(gargs...) - return tengo.FromInterface(str) + return tengo.FromInterface([]byte(str)) }, }, ) if err != nil { return nil, err } - err = script.Add("__separate", &tengo.UserFunction{ + err = script.Add("__separate__", &tengo.UserFunction{ Value: func(args ...tengo.Object) (tengo.Object, error){ if len(args) < 1 { return nil, tengo.ErrWrongNumArguments } - str, ok := tengo.ToByteSlice(args[0]) + bts, ok := tengo.ToByteSlice(args[0]) if !ok { return nil, tengo.ErrInvalidArgumentType{ } } - rets = append(rets, str) + rets = append(rets, bts) return nil, nil }, }, diff --git a/tests/cat.jpg.tpp b/tests/cat.jpg.tpp new file mode 100644 index 0000000..aa58a8e --- /dev/null +++ b/tests/cat.jpg.tpp @@ -0,0 +1,9 @@ +{{ + // The example shows the way to write + // binary files + // so it makes possible to make templates + // for them or for dynamic content. + os := import("os") + bts := os.read_file("tests/cat.webp") + pp.write_raw(bts) +}} diff --git a/tests/cat.webp b/tests/cat.webp new file mode 100644 index 0000000..bf2d014 Binary files /dev/null and b/tests/cat.webp differ diff --git a/tests/index.md.pp b/tests/index.md.pp index aa1a413..aef233e 100644 --- a/tests/index.md.pp +++ b/tests/index.md.pp @@ -2,19 +2,19 @@ os := import("os") shit := import("./tests/shit.tengo") newVar := "'this is gen shita'" - println(shit.some_func()) + pp.println(shit.some_func()) }}# The index testing - 1 + 1 = {{ print(1+1) }} - cock {{ print(newVar, "and cock") }} + 1 + 1 = {{ pp.print(1+1) }} + cock {{ pp.print(newVar, "and cock") }} ## The shit after -checking {{ printf(newVar) }} +checking {{ pp.printf(newVar) }} ## File contents {{ bts := os.read_file("tests/somefile") - printf("%v\n%s", bts, bts) + pp.printf("%v\n%s", bts, bts) }}