motto and otto for nodejs module syntax in custom filters
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Sebastian Frank 2019-03-02 14:45:08 +01:00
parent 8e84901465
commit 6e3688d713
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
6 changed files with 27 additions and 11 deletions

3
.gitmodules vendored
View File

@ -58,3 +58,6 @@
[submodule "vendor/gopkg.in/sourcemap.v1"] [submodule "vendor/gopkg.in/sourcemap.v1"]
path = vendor/gopkg.in/sourcemap.v1 path = vendor/gopkg.in/sourcemap.v1
url = https://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

View File

@ -5,9 +5,9 @@ import (
"path" "path"
"strings" "strings"
"github.com/ddliu/motto"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
_ "github.com/flosch/pongo2-addons" _ "github.com/flosch/pongo2-addons"
"github.com/robertkrimen/otto"
_ "github.com/robertkrimen/otto/underscore" _ "github.com/robertkrimen/otto/underscore"
) )
@ -29,6 +29,7 @@ func MarkdownFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pong
nil nil
} }
// RegisterFilters reads a directory and register filters from files within it
func RegisterFilters(dir string) { func RegisterFilters(dir string) {
files, err := ioutil.ReadDir(dir) files, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
@ -38,14 +39,17 @@ func RegisterFilters(dir string) {
if !f.IsDir() { if !f.IsDir() {
switch path.Ext(f.Name()) { switch path.Ext(f.Name()) {
case ".js": case ".js":
fileBase := strings.TrimSuffix(f.Name(), ".js")
jsFile := dir + "/" + f.Name() jsFile := dir + "/" + f.Name()
Log.Debugf("trying to register filter from: %s", jsFile) Log.Debugf("trying to register filter from: %s", jsFile)
/*
jsStr, err := ioutil.ReadFile(jsFile) jsStr, err := ioutil.ReadFile(jsFile)
if err != nil { if err != nil {
Log.Panicf("could not read '%s': %s", jsFile, err) Log.Panicf("could not read '%s': %s", jsFile, err)
} }
vm := otto.New() */
fn, err := vm.Run(jsStr) vm := motto.New()
fn, err := vm.Run(jsFile)
if err != nil { if err != nil {
Log.Panicf("error in javascript vm for '%s': %s", jsFile, err) Log.Panicf("error in javascript vm for '%s': %s", jsFile, err)
} }
@ -54,7 +58,7 @@ func RegisterFilters(dir string) {
} }
err = pongo2.RegisterFilter( err = pongo2.RegisterFilter(
strings.TrimSuffix(f.Name(), ".js"), fileBase,
func(in, param *pongo2.Value) (out *pongo2.Value, erro *pongo2.Error) { func(in, param *pongo2.Value) (out *pongo2.Value, erro *pongo2.Error) {
thisObj, _ := vm.Object("({})") thisObj, _ := vm.Object("({})")
if currentContext != nil { if currentContext != nil {

View File

@ -13,7 +13,7 @@ import (
// RequestFn will make a web request and returns map[string]interface form pongo2 // RequestFn will make a web request and returns map[string]interface form pongo2
func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value { func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value {
u := url.String() u := url.String()
Log.Notice("requesting url via GET %s", u) Log.Noticef("requesting url via GET %s", u)
resp, err := http.Get(u) resp, err := http.Get(u)
if err != nil { if err != nil {

1
vendor/github.com/ddliu/motto generated vendored Submodule

@ -0,0 +1 @@
Subproject commit b73cfd26c91b45e1c1dcb9ff70634b5240e442c3

View File

@ -1,3 +1,5 @@
var strings = require('./lib/strings');
function datum(str, param) { function datum(str, param) {
if (!param) { if (!param) {
param = "$3.$2.$1"; param = "$3.$2.$1";
@ -7,7 +9,8 @@ function datum(str, param) {
this.context.Meta.Title ist hier z.B. möglich 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; module.exports = datum;

View File

@ -0,0 +1,5 @@
module.exports = {
replace: function (str, regex, replace) {
return str.replace(regex, replace);
}
}