json and dump filter
This commit is contained in:
@@ -19,8 +19,8 @@ import (
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func newContext() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
func newContext() pongo2.Context {
|
||||
return pongo2.Context{
|
||||
"fnRequest": RequestFn,
|
||||
"fnRender": RenderFn,
|
||||
|
||||
@@ -106,7 +106,7 @@ func fillNodeConfig(node *config.PathConfigTree, inBase, outBase, dir string, co
|
||||
}
|
||||
|
||||
if node.ColMap == nil {
|
||||
node.ColMap = make(map[string]interface{})
|
||||
node.ColMap = make(config.MapString)
|
||||
}
|
||||
ctx := newContext()
|
||||
jsonStr, err := pongo2.RenderTemplateString(*colConfig.EntriesJSON, ctx)
|
||||
@@ -114,7 +114,7 @@ func fillNodeConfig(node *config.PathConfigTree, inBase, outBase, dir string, co
|
||||
Log.Panicf("invalid template string in '%s': %s", inPath, err)
|
||||
}
|
||||
|
||||
var colSlice []map[string]interface{}
|
||||
var colSlice []config.MapString
|
||||
err = json.Unmarshal([]byte(jsonStr), &colSlice)
|
||||
if err != nil {
|
||||
Log.Panicf("invalid JSON in EntriesJSON in '%s': %s", inPath, err)
|
||||
@@ -130,12 +130,12 @@ func fillNodeConfig(node *config.PathConfigTree, inBase, outBase, dir string, co
|
||||
|
||||
// build navigation with detail sites
|
||||
for _, colEl := range colSlice {
|
||||
ctxE := make(map[string]interface{})
|
||||
ctxE := make(pongo2.Context)
|
||||
err := config.Merge(&ctxE, ctx)
|
||||
if err != nil {
|
||||
Log.Panicf("could not merge context in '%s': %s", inPath, err)
|
||||
}
|
||||
err = config.Merge(&ctxE, colEl)
|
||||
err = config.Merge(&ctxE, pongo2.Context(colEl))
|
||||
if err != nil {
|
||||
Log.Panicf("could not merge context in '%s': %s", inPath, err)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ type NavElement struct {
|
||||
GoTo string
|
||||
Active bool
|
||||
|
||||
ColMap map[string]interface{}
|
||||
ColMap config.MapString
|
||||
|
||||
Data interface{}
|
||||
|
||||
@@ -93,7 +93,7 @@ func BuildNavigation(conf *config.PathConfigTree, curNavMap *map[string]*NavElem
|
||||
navEl.GoTo = path.Clean(navEl.GoTo)
|
||||
}
|
||||
|
||||
(*curNavMap)[navEl.Navname] = &navEl
|
||||
(*curNavMap)[path.Base(el.OutputPath)] = &navEl
|
||||
if curNavSlice != nil {
|
||||
*curNavSlice = append(*curNavSlice, &navEl)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
var templateCache = make(map[string]*pongo2.Template)
|
||||
var currentContext *map[string]interface{}
|
||||
var currentContext *pongo2.Context
|
||||
var currentTreeNodeConfig *config.PathConfigTree
|
||||
var currentPathConfig *config.PathConfig
|
||||
var templateDir string
|
||||
@@ -63,7 +63,7 @@ func SetTemplateDir(dir string) {
|
||||
}
|
||||
|
||||
// RenderTemplate renders a pongo2 template with context
|
||||
func RenderTemplate(filename string, treeNodeConfig *config.PathConfigTree, pathConfig *config.PathConfig, ctx *map[string]interface{}) (string, error) {
|
||||
func RenderTemplate(filename string, treeNodeConfig *config.PathConfigTree, pathConfig *config.PathConfig, ctx *pongo2.Context) (string, error) {
|
||||
currentContext = ctx
|
||||
currentTreeNodeConfig = treeNodeConfig
|
||||
currentPathConfig = pathConfig
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"gitbase.de/apairon/mark2web/config"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/ddliu/motto"
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/flosch/pongo2"
|
||||
@@ -32,6 +33,7 @@ func init() {
|
||||
"image_process": ImageProcessFilter,
|
||||
"relative_path": RelativePathFilter,
|
||||
"json": JSONFilter,
|
||||
"dump": DumpFilter,
|
||||
}
|
||||
for name, fn := range newFilters {
|
||||
err := pongo2.RegisterFilter(name, fn)
|
||||
@@ -41,9 +43,29 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// DumpFilter is a pongo2 filter, which returns a spew.Dump of the input
|
||||
func DumpFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
||||
dumpString := spew.Sdump(in.Interface())
|
||||
return pongo2.AsValue(string(dumpString)), nil
|
||||
}
|
||||
|
||||
// JSONFilter is a pongo2 filter, which returns a json string of the input
|
||||
func JSONFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
||||
jsonString, err := json.Marshal(in.Interface())
|
||||
pretty := false
|
||||
for _, s := range strings.Split(param.String(), ",") {
|
||||
switch s {
|
||||
case "pretty":
|
||||
pretty = true
|
||||
}
|
||||
}
|
||||
var err error
|
||||
var jsonBytes []byte
|
||||
if pretty {
|
||||
jsonBytes, err = json.MarshalIndent(in.Interface(), "", " ")
|
||||
|
||||
} else {
|
||||
jsonBytes, err = json.Marshal(in.Interface())
|
||||
}
|
||||
if err != nil {
|
||||
return nil, &pongo2.Error{
|
||||
Sender: "filter:json",
|
||||
@@ -51,7 +73,7 @@ func JSONFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.E
|
||||
}
|
||||
}
|
||||
|
||||
return pongo2.AsSafeValue(string(jsonString)), nil
|
||||
return pongo2.AsSafeValue(string(jsonBytes)), nil
|
||||
}
|
||||
|
||||
// MarkdownFilter is a pongo2 filter, which converts markdown to html
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/flosch/pongo2"
|
||||
)
|
||||
|
||||
func jsonWebRequest(url string) map[string]interface{} {
|
||||
func jsonWebRequest(url string) config.MapString {
|
||||
Log.Noticef("requesting url via GET %s", url)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
@@ -38,7 +38,7 @@ func jsonWebRequest(url string) map[string]interface{} {
|
||||
Log.Panicf("is not json '%s' from url '%s'", contentType, url)
|
||||
}
|
||||
|
||||
jsonMap := make(map[string]interface{})
|
||||
jsonMap := make(config.MapString)
|
||||
err = json.Unmarshal(body, &jsonMap)
|
||||
if err != nil {
|
||||
Log.Panicf("could not read json from '%s': %s", url, err)
|
||||
@@ -69,7 +69,7 @@ func add2Nav(currentNode *config.PathConfigTree, pathConfig *config.PathConfig,
|
||||
}
|
||||
if dataMapKey != "" {
|
||||
if newNodeConfig.Config.Data == nil {
|
||||
newNodeConfig.Config.Data = make(map[string]interface{})
|
||||
newNodeConfig.Config.Data = make(config.MapString)
|
||||
}
|
||||
// as submap in Data
|
||||
newNodeConfig.Config.Data[dataMapKey] = ctx
|
||||
|
||||
Reference in New Issue
Block a user