fixed #7, javascript filter for pongo2 with context in this variable
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Sebastian Frank 2019-02-28 18:36:36 +01:00
parent 9e2f16dde9
commit 079534ab71
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
9 changed files with 102 additions and 6 deletions

6
.gitmodules vendored
View File

@ -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

View File

@ -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)
}
}
}
}
}

View File

@ -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()

1
vendor/github.com/robertkrimen/otto generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 15f95af6e78dcd2030d8195a138bd88d4f403546

1
vendor/gopkg.in/sourcemap.v1 generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 6e83acea0053641eff084973fee085f0c193c61a

View File

@ -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;
}

View File

@ -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 %}
<h2>
<small>{{ e.date }}</small>
{{ e.title }}
<div class="datum">{{ e.date|datum }}</div>
</h2>
{{ 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 %}
<h2>
<small>{{ e.date }}</small>
{{ e.title }}
<div class="datum">{{ e.date|datum }}</div>
</h2>
{{ e.teaser|markdown }}
{% if e.body %}

View File

@ -2,8 +2,8 @@
{% block part0 %}
<h1>
<small>{{ Data.details.date }}</small>
{{ Data.details.title }}
<div class="datum">{{ Data.details.date|datum }}</div>
</h1>
{{ Data.details.teaser|markdown }}
{% endblock part0 %}

View File

@ -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;