166 lines
4.0 KiB
Go
166 lines
4.0 KiB
Go
package helper
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"gitbase.de/apairon/mark2web/config"
|
|
"github.com/davecgh/go-spew/spew"
|
|
"github.com/flosch/pongo2"
|
|
)
|
|
|
|
// 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()
|
|
fmt.Printf("request GET %s\n", u)
|
|
|
|
resp, err := http.Get(u)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
spew.Dump(string(body))
|
|
|
|
if resp.StatusCode >= 400 {
|
|
panic(resp.Status)
|
|
}
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
|
|
|
if strings.Contains(contentType, "json") {
|
|
|
|
} else {
|
|
panic("invalid content-type")
|
|
}
|
|
|
|
jsonMap := make(map[string]interface{})
|
|
err = json.Unmarshal(body, &jsonMap)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return pongo2.AsValue(jsonMap)
|
|
}
|
|
|
|
// RenderFn renders a pongo2 template with additional context
|
|
func RenderFn(templateFilename, outDir, ctx *pongo2.Value, param ...*pongo2.Value) *pongo2.Value {
|
|
ctxMapKey := ""
|
|
body := ""
|
|
|
|
for i, p := range param {
|
|
switch i {
|
|
case 0:
|
|
ctxMapKey = p.String()
|
|
case 1:
|
|
body = p.String()
|
|
}
|
|
}
|
|
|
|
newNodeConfig := new(config.PathConfigTree)
|
|
fillNodeConfig(
|
|
newNodeConfig,
|
|
currentTreeNodeConfig.InputPath,
|
|
currentTreeNodeConfig.OutputPath,
|
|
outDir.String(),
|
|
currentPathConfig,
|
|
)
|
|
if newNodeConfig.Config.Data == nil {
|
|
newNodeConfig.Config.Data = make(map[string]interface{})
|
|
}
|
|
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
|
|
}
|
|
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
|
|
}
|
|
}
|
|
newNodeConfig.Config.Index = &config.IndexConfig{
|
|
InputFile: &indexInFile,
|
|
OutputFile: &indexOutFile,
|
|
InputString: &body,
|
|
}
|
|
newNodeConfig.Hidden = true
|
|
|
|
currentTreeNodeConfig.Sub = append(currentTreeNodeConfig.Sub, newNodeConfig)
|
|
spew.Dump(currentTreeNodeConfig)
|
|
|
|
/*
|
|
oDirSlug := slug.Slug(outDir.String())
|
|
navPath := path.Clean((*currentContext)["CurrentPath"].(string) + "/" + oDirSlug)
|
|
|
|
CreateDirectory(newNodeConfig.OutputPath)
|
|
|
|
newContext := make(map[string]interface{})
|
|
if err := config.Merge(&newContext, *currentContext); err != nil {
|
|
Log.Panicf("unable to merge context")
|
|
}
|
|
newContext[subCtxName.String()] = ctx
|
|
newContext["CurrentPath"] = navPath
|
|
|
|
// remember old to set after recursion
|
|
oldContext := currentContext
|
|
oldNodeConfig := currentTreeNodeConfig
|
|
result, err := RenderTemplate(
|
|
templateFilename.String(),
|
|
newNodeConfig,
|
|
currentPathConfig,
|
|
&newContext,
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
currentContext = oldContext
|
|
currentTreeNodeConfig = oldNodeConfig
|
|
|
|
result = FixAssetsPath(
|
|
result,
|
|
navPath,
|
|
)
|
|
//spew.Dump(result)
|
|
|
|
// build output filename
|
|
outputFilename := "index.html"
|
|
|
|
var indexOutputFile *string
|
|
if i := currentPathConfig.Index; i != nil {
|
|
indexOutputFile = i.OutputFile
|
|
}
|
|
|
|
if indexOutputFile != nil && *indexOutputFile != "" {
|
|
outputFilename = *indexOutputFile
|
|
}
|
|
|
|
outFile := newNodeConfig.OutputPath + "/" + outputFilename
|
|
Log.Debugf("using '%s' as output file", outFile)
|
|
|
|
Log.Noticef("writing to output file: %s", outFile)
|
|
err = ioutil.WriteFile(outFile, []byte(result), 0644)
|
|
if err != nil {
|
|
log.Panicf("could not write to output file '%s': %s", outFile, err)
|
|
}
|
|
*/
|
|
|
|
return pongo2.AsValue(nil)
|
|
}
|