mark2web/helper/template_functions.go

107 lines
2.6 KiB
Go
Raw Normal View History

2019-02-27 15:58:10 +01:00
package helper
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
2019-02-28 10:43:30 +01:00
"gitbase.de/apairon/mark2web/config"
2019-02-27 15:58:10 +01:00
"github.com/flosch/pongo2"
)
2019-02-27 17:33:26 +01:00
// RequestFn will make a web request and returns map[string]interface form pongo2
func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value {
2019-02-27 15:58:10 +01:00
u := url.String()
Log.Noticef("requesting url via GET %s", u)
2019-02-27 15:58:10 +01:00
resp, err := http.Get(u)
if err != nil {
2019-02-28 15:40:06 +01:00
Log.Panicf("could not get url '%s': %s", u, err)
2019-02-27 15:58:10 +01:00
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2019-02-28 15:40:06 +01:00
Log.Panicf("could not read body from url '%s': %s", u, err)
2019-02-27 15:58:10 +01:00
}
2019-02-28 15:40:06 +01:00
Log.Debugf("output from url '%s':\n%s", u, string(body))
2019-02-27 15:58:10 +01:00
if resp.StatusCode >= 400 {
2019-02-28 15:40:06 +01:00
Log.Panicf("bad status '%d - %s' from url '%s'", resp.StatusCode, resp.Status, u)
2019-02-27 15:58:10 +01:00
}
contentType := resp.Header.Get("Content-Type")
if strings.Contains(contentType, "json") {
} else {
2019-02-28 15:40:06 +01:00
Log.Panicf("is not json '%s' from url '%s'", contentType, u)
2019-02-27 15:58:10 +01:00
}
jsonMap := make(map[string]interface{})
err = json.Unmarshal(body, &jsonMap)
if err != nil {
2019-02-28 15:40:06 +01:00
Log.Panicf("could not read json from '%s': %s", u, err)
2019-02-27 15:58:10 +01:00
}
return pongo2.AsValue(jsonMap)
}
2019-02-27 17:33:26 +01:00
// RenderFn renders a pongo2 template with additional context
2019-02-28 14:14:31 +01:00
func RenderFn(templateFilename, outDir, ctx *pongo2.Value, param ...*pongo2.Value) *pongo2.Value {
ctxMapKey := ""
2019-02-28 15:13:59 +01:00
body := ""
2019-02-28 14:14:31 +01:00
for i, p := range param {
switch i {
case 0:
ctxMapKey = p.String()
2019-02-28 15:13:59 +01:00
case 1:
body = p.String()
2019-02-28 14:14:31 +01:00
}
2019-02-28 10:43:30 +01:00
}
newNodeConfig := new(config.PathConfigTree)
2019-02-28 14:14:31 +01:00
fillNodeConfig(
2019-02-28 10:43:30 +01:00
newNodeConfig,
2019-02-28 14:14:31 +01:00
currentTreeNodeConfig.InputPath,
currentTreeNodeConfig.OutputPath,
outDir.String(),
2019-02-28 10:43:30 +01:00
currentPathConfig,
)
2019-02-28 14:14:31 +01:00
if newNodeConfig.Config.Data == nil {
newNodeConfig.Config.Data = make(map[string]interface{})
2019-02-27 17:33:26 +01:00
}
2019-02-28 14:14:31 +01:00
if ctxMapKey != "" {
// as submap in Data
newNodeConfig.Config.Data[ctxMapKey] = ctx.Interface()
} else if m, ok := ctx.Interface().(map[string]interface{}); ok {
// direct set data
newNodeConfig.Config.Data = m
2019-02-28 10:43:30 +01:00
}
2019-02-28 14:14:31 +01:00
tplFilename := templateFilename.String()
// fake via normal file behavior
newNodeConfig.Config.Template = &tplFilename
newNodeConfig.InputFiles = []string{""} // empty file is special for use InputString
indexInFile := ""
indexOutFile := "index.html"
if idx := newNodeConfig.Config.Index; idx != nil {
if idx.OutputFile != nil && *idx.OutputFile != "" {
indexOutFile = *idx.OutputFile
}
2019-02-28 10:43:30 +01:00
}
2019-02-28 14:14:31 +01:00
newNodeConfig.Config.Index = &config.IndexConfig{
2019-02-28 15:13:59 +01:00
InputFile: &indexInFile,
OutputFile: &indexOutFile,
InputString: &body,
2019-02-28 10:43:30 +01:00
}
2019-02-28 14:14:31 +01:00
newNodeConfig.Hidden = true
currentTreeNodeConfig.Sub = append(currentTreeNodeConfig.Sub, newNodeConfig)
2019-02-28 10:43:30 +01:00
2019-02-27 17:33:26 +01:00
return pongo2.AsValue(nil)
}