mark2web/helper/template_functions.go

119 lines
3.0 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-03-10 13:26:26 +01:00
func jsonWebRequest(url string) map[string]interface{} {
Log.Noticef("requesting url via GET %s", url)
2019-02-27 15:58:10 +01:00
2019-03-10 13:26:26 +01:00
resp, err := http.Get(url)
2019-02-27 15:58:10 +01:00
if err != nil {
2019-03-10 13:26:26 +01:00
Log.Panicf("could not get url '%s': %s", url, err)
2019-02-27 15:58:10 +01:00
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2019-03-10 13:26:26 +01:00
Log.Panicf("could not read body from url '%s': %s", url, err)
2019-02-27 15:58:10 +01:00
}
2019-03-10 13:26:26 +01:00
Log.Debugf("output from url '%s':\n%s", url, string(body))
2019-02-27 15:58:10 +01:00
if resp.StatusCode >= 400 {
2019-03-10 13:26:26 +01:00
Log.Panicf("bad status '%d - %s' from url '%s'", resp.StatusCode, resp.Status, url)
2019-02-27 15:58:10 +01:00
}
contentType := resp.Header.Get("Content-Type")
if strings.Contains(contentType, "json") {
} else {
2019-03-10 13:26:26 +01:00
Log.Panicf("is not json '%s' from url '%s'", contentType, url)
2019-02-27 15:58:10 +01:00
}
jsonMap := make(map[string]interface{})
err = json.Unmarshal(body, &jsonMap)
if err != nil {
2019-03-10 13:26:26 +01:00
Log.Panicf("could not read json from '%s': %s", url, err)
2019-02-27 15:58:10 +01:00
}
2019-03-10 13:26:26 +01:00
return jsonMap
}
// RequestFn will make a web request and returns map[string]interface form pongo2
func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value {
u := url.String()
return pongo2.AsValue(jsonWebRequest(u))
2019-02-27 15:58:10 +01:00
}
2019-02-27 17:33:26 +01:00
2019-03-10 16:02:53 +01:00
func add2Nav(currentNode *config.PathConfigTree, pathConfig *config.PathConfig, tplFilename, outDir string, navname string, ctx interface{}, dataMapKey string, body string, hidden bool) {
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-03-10 16:02:53 +01:00
currentNode.InputPath,
currentNode.OutputPath,
outDir,
pathConfig,
2019-02-28 10:43:30 +01:00
)
2019-03-10 16:02:53 +01:00
if navname != "" {
newNodeConfig.Config.This = config.ThisPathConfig{
Navname: &navname,
}
2019-02-27 17:33:26 +01:00
}
2019-03-10 16:02:53 +01:00
if dataMapKey != "" {
if newNodeConfig.Config.Data == nil {
newNodeConfig.Config.Data = make(map[string]interface{})
}
2019-02-28 14:14:31 +01:00
// as submap in Data
2019-03-10 16:02:53 +01:00
newNodeConfig.Config.Data[dataMapKey] = ctx
} else if m, ok := ctx.(map[string]interface{}); ok {
2019-02-28 14:14:31 +01:00
// direct set data
newNodeConfig.Config.Data = m
2019-02-28 10:43:30 +01:00
}
2019-02-28 14:14:31 +01:00
// 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-03-10 16:02:53 +01:00
newNodeConfig.Hidden = hidden
currentNode.Sub = append(currentNode.Sub, newNodeConfig)
}
// RenderFn renders a pongo2 template with additional context
func RenderFn(templateFilename, outDir, ctx *pongo2.Value, param ...*pongo2.Value) *pongo2.Value {
dataMapKey := ""
body := ""
for i, p := range param {
switch i {
case 0:
dataMapKey = p.String()
case 1:
body = p.String()
}
}
2019-02-28 14:14:31 +01:00
2019-03-10 16:02:53 +01:00
add2Nav(currentTreeNodeConfig, currentPathConfig, templateFilename.String(), outDir.String(), "", ctx.Interface(), dataMapKey, body, true)
2019-02-28 10:43:30 +01:00
2019-02-27 17:33:26 +01:00
return pongo2.AsValue(nil)
}