From 079534ab7167f7cf2bb4f522b31a3f330d9888e3 Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Thu, 28 Feb 2019 18:36:36 +0100 Subject: [PATCH] fixed #7, javascript filter for pongo2 with context in this variable --- .gitmodules | 6 +++ helper/template_filters.go | 67 +++++++++++++++++++++++- main.go | 4 +- vendor/github.com/robertkrimen/otto | 1 + vendor/gopkg.in/sourcemap.v1 | 1 + website/project-files/css/main.css | 10 +++- website/templates/base_blog.html | 4 +- website/templates/base_blog_details.html | 2 +- website/templates/filters/datum.js | 13 +++++ 9 files changed, 102 insertions(+), 6 deletions(-) create mode 160000 vendor/github.com/robertkrimen/otto create mode 160000 vendor/gopkg.in/sourcemap.v1 create mode 100644 website/templates/filters/datum.js diff --git a/.gitmodules b/.gitmodules index a9d1431..cc2f792 100644 --- a/.gitmodules +++ b/.gitmodules @@ -52,3 +52,9 @@ [submodule "vendor/github.com/russross/blackfriday"] path = vendor/github.com/russross/blackfriday url = https://github.com/russross/blackfriday +[submodule "vendor/github.com/robertkrimen/otto"] + path = vendor/github.com/robertkrimen/otto + url = https://github.com/robertkrimen/otto +[submodule "vendor/gopkg.in/sourcemap.v1"] + path = vendor/gopkg.in/sourcemap.v1 + url = https://gopkg.in/sourcemap.v1 diff --git a/helper/template_filters.go b/helper/template_filters.go index ddd6656..52b69a2 100644 --- a/helper/template_filters.go +++ b/helper/template_filters.go @@ -1,12 +1,21 @@ package helper import ( + "io/ioutil" + "path" + "strings" + "github.com/flosch/pongo2" _ "github.com/flosch/pongo2-addons" + "github.com/robertkrimen/otto" + _ "github.com/robertkrimen/otto/underscore" ) func init() { - pongo2.ReplaceFilter("markdown", MarkdownFilter) + err := pongo2.ReplaceFilter("markdown", MarkdownFilter) + if err != nil { + panic(err) + } } // MarkdownFilter is a pongo2 filter, which converts markdown to html @@ -19,3 +28,59 @@ func MarkdownFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pong ))), nil } + +func RegisterFilters(dir string) { + files, err := ioutil.ReadDir(dir) + if err != nil { + Log.Panicf("could not read from template filters dir '%s': %s", dir, err) + } + for _, f := range files { + if !f.IsDir() { + switch path.Ext(f.Name()) { + case ".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) + if err != nil { + Log.Panicf("error in javascript vm for '%s': %s", jsFile, err) + } + if !fn.IsFunction() { + Log.Panicf("%s does not contain a function code", jsFile) + } + + err = pongo2.RegisterFilter( + strings.TrimSuffix(f.Name(), ".js"), + func(in, param *pongo2.Value) (out *pongo2.Value, erro *pongo2.Error) { + thisObj, _ := vm.Object("({})") + if currentContext != nil { + thisObj.Set("context", *currentContext) + } + + if err != nil { + Log.Panicf("could not set context as in '%s': %s", jsFile, err) + } + ret, err := fn.Call(thisObj.Value(), in.Interface(), param.Interface()) + if err != nil { + Log.Panicf("error in javascript file '%s' while calling returned function: %s", jsFile, err) + } + retGo, err := ret.Export() + if err != nil { + Log.Panicf("export error for '%s': %s", jsFile, err) + } + return pongo2.AsValue(retGo), nil + }, + ) + if err != nil { + Log.Panicf("could not register filter from '%s': %s", jsFile, err) + } + + } + } + } + +} diff --git a/main.go b/main.go index 4d340e5..92da561 100644 --- a/main.go +++ b/main.go @@ -120,7 +120,9 @@ func main() { //spew.Dump(navMap) - helper.SetTemplateDir(*inDir + "/templates") + templateDir := *inDir + "/templates" + helper.SetTemplateDir(templateDir) + helper.RegisterFilters(templateDir + "/filters") helper.ProcessContent(contentConfig, contentConfig) helper.ProcessAssets() diff --git a/vendor/github.com/robertkrimen/otto b/vendor/github.com/robertkrimen/otto new file mode 160000 index 0000000..15f95af --- /dev/null +++ b/vendor/github.com/robertkrimen/otto @@ -0,0 +1 @@ +Subproject commit 15f95af6e78dcd2030d8195a138bd88d4f403546 diff --git a/vendor/gopkg.in/sourcemap.v1 b/vendor/gopkg.in/sourcemap.v1 new file mode 160000 index 0000000..6e83ace --- /dev/null +++ b/vendor/gopkg.in/sourcemap.v1 @@ -0,0 +1 @@ +Subproject commit 6e83acea0053641eff084973fee085f0c193c61a diff --git a/website/project-files/css/main.css b/website/project-files/css/main.css index 0de4ebf..467f21b 100755 --- a/website/project-files/css/main.css +++ b/website/project-files/css/main.css @@ -193,7 +193,6 @@ label {font-weight:600;} } -.boxen {} .boxen .col-sm-6 {padding:15px;} .box { height:100%; @@ -279,4 +278,13 @@ code.language-mermaid svg { margin-left: auto; margin-right: auto; display: block; +} + +.datum { + background-color: #2e2e2e; + padding: 5px; + font-size: 0.5em; + border-radius: 5px; + display:inline-block; + color: #fff; } \ No newline at end of file diff --git a/website/templates/base_blog.html b/website/templates/base_blog.html index ffa56fa..903f603 100644 --- a/website/templates/base_blog.html +++ b/website/templates/base_blog.html @@ -4,8 +4,8 @@ {{ Body }} {% for e in fnRequest("https://cockpit.basiscms.de/api/collections/get/mark2webBlog?filter[published]=true&sort[date]=-1&limit=1").entries %}

- {{ e.date }} {{ e.title }} +
{{ e.date|datum }}

{{ e.teaser|markdown }} {% if e.body %} @@ -21,8 +21,8 @@ {% endcomment %} {% for e in fnRequest("https://cockpit.basiscms.de/api/collections/get/mark2webBlog?filter[published]=true&sort[date]=-1&skip=1&limit=100").entries %}

- {{ e.date }} {{ e.title }} +
{{ e.date|datum }}

{{ e.teaser|markdown }} {% if e.body %} diff --git a/website/templates/base_blog_details.html b/website/templates/base_blog_details.html index 8f4ce4c..2b47008 100644 --- a/website/templates/base_blog_details.html +++ b/website/templates/base_blog_details.html @@ -2,8 +2,8 @@ {% block part0 %}

- {{ Data.details.date }} {{ Data.details.title }} +
{{ Data.details.date|datum }}

{{ Data.details.teaser|markdown }} {% endblock part0 %} diff --git a/website/templates/filters/datum.js b/website/templates/filters/datum.js new file mode 100644 index 0000000..a65957c --- /dev/null +++ b/website/templates/filters/datum.js @@ -0,0 +1,13 @@ +function datum(str, param) { + if (!param) { + param = "$3.$2.$1"; + } + + /* + this.context.Meta.Title ist hier z.B. möglich + */ + + return str.replace(/^([0-9]+)[^0-9]+([0-9]+)[^0-9]+([0-9]+).*/, param) +} + +datum; \ No newline at end of file