From 6e3688d713cfee8edfda9f14fd2b6fa440857cfc Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Sat, 2 Mar 2019 14:45:08 +0100 Subject: [PATCH] motto and otto for nodejs module syntax in custom filters --- .gitmodules | 3 +++ helper/template_filters.go | 20 ++++++++++++-------- helper/template_functions.go | 2 +- vendor/github.com/ddliu/motto | 1 + website/templates/filters/datum.js | 7 +++++-- website/templates/filters/lib/strings.js | 5 +++++ 6 files changed, 27 insertions(+), 11 deletions(-) create mode 160000 vendor/github.com/ddliu/motto create mode 100644 website/templates/filters/lib/strings.js diff --git a/.gitmodules b/.gitmodules index cc2f792..a924ca1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -58,3 +58,6 @@ [submodule "vendor/gopkg.in/sourcemap.v1"] path = vendor/gopkg.in/sourcemap.v1 url = https://gopkg.in/sourcemap.v1 +[submodule "vendor/github.com/ddliu/motto"] + path = vendor/github.com/ddliu/motto + url = https://github.com/ddliu/motto diff --git a/helper/template_filters.go b/helper/template_filters.go index 52b69a2..f16161b 100644 --- a/helper/template_filters.go +++ b/helper/template_filters.go @@ -5,9 +5,9 @@ import ( "path" "strings" + "github.com/ddliu/motto" "github.com/flosch/pongo2" _ "github.com/flosch/pongo2-addons" - "github.com/robertkrimen/otto" _ "github.com/robertkrimen/otto/underscore" ) @@ -29,6 +29,7 @@ func MarkdownFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pong nil } +// RegisterFilters reads a directory and register filters from files within it func RegisterFilters(dir string) { files, err := ioutil.ReadDir(dir) if err != nil { @@ -38,14 +39,17 @@ func RegisterFilters(dir string) { if !f.IsDir() { switch path.Ext(f.Name()) { case ".js": + fileBase := strings.TrimSuffix(f.Name(), ".js") jsFile := dir + "/" + f.Name() Log.Debugf("trying to register filter from: %s", jsFile) - jsStr, err := ioutil.ReadFile(jsFile) - if err != nil { - Log.Panicf("could not read '%s': %s", jsFile, err) - } - vm := otto.New() - fn, err := vm.Run(jsStr) + /* + jsStr, err := ioutil.ReadFile(jsFile) + if err != nil { + Log.Panicf("could not read '%s': %s", jsFile, err) + } + */ + vm := motto.New() + fn, err := vm.Run(jsFile) if err != nil { Log.Panicf("error in javascript vm for '%s': %s", jsFile, err) } @@ -54,7 +58,7 @@ func RegisterFilters(dir string) { } err = pongo2.RegisterFilter( - strings.TrimSuffix(f.Name(), ".js"), + fileBase, func(in, param *pongo2.Value) (out *pongo2.Value, erro *pongo2.Error) { thisObj, _ := vm.Object("({})") if currentContext != nil { diff --git a/helper/template_functions.go b/helper/template_functions.go index 0ea493c..9dd56c0 100644 --- a/helper/template_functions.go +++ b/helper/template_functions.go @@ -13,7 +13,7 @@ import ( // RequestFn will make a web request and returns map[string]interface form pongo2 func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value { u := url.String() - Log.Notice("requesting url via GET %s", u) + Log.Noticef("requesting url via GET %s", u) resp, err := http.Get(u) if err != nil { diff --git a/vendor/github.com/ddliu/motto b/vendor/github.com/ddliu/motto new file mode 160000 index 0000000..b73cfd2 --- /dev/null +++ b/vendor/github.com/ddliu/motto @@ -0,0 +1 @@ +Subproject commit b73cfd26c91b45e1c1dcb9ff70634b5240e442c3 diff --git a/website/templates/filters/datum.js b/website/templates/filters/datum.js index a65957c..23d3657 100644 --- a/website/templates/filters/datum.js +++ b/website/templates/filters/datum.js @@ -1,3 +1,5 @@ +var strings = require('./lib/strings'); + function datum(str, param) { if (!param) { param = "$3.$2.$1"; @@ -7,7 +9,8 @@ function datum(str, param) { this.context.Meta.Title ist hier z.B. möglich */ - return str.replace(/^([0-9]+)[^0-9]+([0-9]+)[^0-9]+([0-9]+).*/, param) + return strings.replace(str, /^([0-9]+)[^0-9]+([0-9]+)[^0-9]+([0-9]+).*/, param) + // return str.replace(/^([0-9]+)[^0-9]+([0-9]+)[^0-9]+([0-9]+).*/, param) } -datum; \ No newline at end of file +module.exports = datum; \ No newline at end of file diff --git a/website/templates/filters/lib/strings.js b/website/templates/filters/lib/strings.js new file mode 100644 index 0000000..4f19812 --- /dev/null +++ b/website/templates/filters/lib/strings.js @@ -0,0 +1,5 @@ +module.exports = { + replace: function (str, regex, replace) { + return str.replace(regex, replace); + } +} \ No newline at end of file