mark2web/context.go

273 lines
7.7 KiB
Go
Raw Normal View History

2019-03-18 15:14:41 +01:00
package mark2web
2019-03-18 13:34:52 +01:00
import (
"io/ioutil"
"os"
"path"
"regexp"
"strings"
"time"
"gitbase.de/apairon/mark2web/helper"
"github.com/davecgh/go-spew/spew"
"github.com/extemporalgenome/slug"
"github.com/flosch/pongo2"
"gopkg.in/yaml.v2"
)
var CurrentContext *pongo2.Context
2019-03-18 15:14:41 +01:00
var CurrentTreeNode *TreeNode
var CurrentPathConfig *PathConfig
2019-03-18 13:34:52 +01:00
func NewContext() pongo2.Context {
ctx := pongo2.Context{
"fnRequest": RequestFn,
"fnRender": RenderFn,
2019-03-18 15:14:41 +01:00
"AssetsPath": Config.Assets.ToPath,
2019-03-18 13:34:52 +01:00
"Timestamp": time.Now().Unix,
}
CurrentContext = &ctx
return ctx
}
2019-03-18 15:14:41 +01:00
func (node *TreeNode) handleCollections() {
for _, colConfig := range node.Config.This.Collections {
2019-03-18 13:34:52 +01:00
if colConfig != nil {
if colConfig.Name == nil || *colConfig.Name == "" {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("missing Name in collection config in '%s'", node.InputPath)
2019-03-18 13:34:52 +01:00
}
if colConfig.URL == nil || *colConfig.URL == "" {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("missing EntriesJSON in collection config in '%s'", node.InputPath)
2019-03-18 13:34:52 +01:00
}
}
if node.ColMap == nil {
2019-03-18 15:14:41 +01:00
node.ColMap = make(MapString)
2019-03-18 13:34:52 +01:00
}
ctx := NewContext()
ctx["This"] = node.Config.This
ctx["Data"] = node.Config.Data
url, err := pongo2.RenderTemplateString(*colConfig.URL, ctx)
if err != nil {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("invalid template string for Collection Element.URL in '%s': %s", node.InputPath, err)
2019-03-18 13:34:52 +01:00
}
colData := helper.JSONWebRequest(url)
node.ColMap[*colConfig.Name] = colData
if navT := colConfig.NavTemplate; navT != nil {
var entries []interface{}
var ok bool
if navT.EntriesAttribute != "" {
var colDataMap map[string]interface{}
if colDataMap, ok = colData.(map[string]interface{}); ok {
entries, ok = colDataMap[navT.EntriesAttribute].([]interface{})
if !ok {
helper.Log.Debug(spew.Sdump(colDataMap))
helper.Log.Panicf("invalid json data in [%s] from url '%s' for entries", navT.EntriesAttribute, url)
}
}
} else {
entries, ok = colData.([]interface{})
}
if !ok {
helper.Log.Debug(spew.Sdump(colData))
helper.Log.Panicf("invalid json data from url '%s', need array of objects for entries or object with configured NavTemplate.EntriesAttribute", url)
}
// build navigation with detail sites
for idx, colEl := range entries {
ctxE := make(pongo2.Context)
err := helper.Merge(&ctxE, ctx)
if err != nil {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("could not merge context in '%s': %s", node.InputPath, err)
2019-03-18 13:34:52 +01:00
}
var jsonCtx map[string]interface{}
if jsonCtx, ok = colEl.(map[string]interface{}); !ok {
helper.Log.Debug(spew.Sdump(colEl))
helper.Log.Panicf("no json object for entry index %d from url '%s'", idx, url)
}
err = helper.Merge(&ctxE, pongo2.Context(jsonCtx))
if err != nil {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("could not merge context in '%s': %s", node.InputPath, err)
2019-03-18 13:34:52 +01:00
}
tpl := ""
if navT.Template != "" {
tpl, err = pongo2.RenderTemplateString(navT.Template, ctxE)
if err != nil {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("invalid template string for NavTemplate.Template in '%s': %s", node.InputPath, err)
2019-03-18 13:34:52 +01:00
}
}
if tpl == "" {
2019-03-18 15:14:41 +01:00
tpl = *node.Config.Template
2019-03-18 13:34:52 +01:00
}
dataKey := ""
if navT.DataKey != "" {
dataKey, err = pongo2.RenderTemplateString(navT.DataKey, ctxE)
if err != nil {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("invalid template string for NavTemplate.DataKey in '%s': %s", node.InputPath, err)
2019-03-18 13:34:52 +01:00
}
}
goTo, err := pongo2.RenderTemplateString(navT.GoTo, ctxE)
if err != nil {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("invalid template string for NavTemplate.GoTo in '%s': %s", node.InputPath, err)
2019-03-18 13:34:52 +01:00
}
goTo = strings.Trim(goTo, "/")
goTo = path.Clean(goTo)
if strings.Contains(goTo, "..") {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("going back via .. in NavTemplate.GoTo forbidden in collection config in '%s': %s", node.InputPath, goTo)
2019-03-18 13:34:52 +01:00
}
if goTo == "." {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("invalid config '.' for NavTemplate.GoTo in collection config in '%s'", node.InputPath)
2019-03-18 13:34:52 +01:00
}
if goTo == "" {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("missing NavTemplate.GoTo in collection config in '%s'", node.InputPath)
2019-03-18 13:34:52 +01:00
}
navname := ""
if navT.Navname != "" {
navname, err = pongo2.RenderTemplateString(navT.Navname, ctxE)
if err != nil {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("invalid template string for NavTemplate.Navname in '%s': %s", node.InputPath, err)
2019-03-18 13:34:52 +01:00
}
}
body := ""
if navT.Body != "" {
body, err = pongo2.RenderTemplateString(navT.Body, ctxE)
if err != nil {
2019-03-18 15:14:41 +01:00
helper.Log.Panicf("invalid template string for NavTemplate.Body in '%s': %s", node.InputPath, err)
2019-03-18 13:34:52 +01:00
}
}
Add2Nav(node, node.Config, tpl, goTo, navname, colEl, dataKey, body, navT.Hidden)
}
}
2019-03-18 15:14:41 +01:00
}
}
2019-03-18 15:35:17 +01:00
func (node *TreeNode) fillConfig(inBase, outBase, dir string, conf *PathConfig) {
if node.root == nil {
// this must be root
node.root = node
}
2019-03-18 15:14:41 +01:00
inPath := inBase
if dir != "" {
inPath += "/" + dir
}
helper.Log.Infof("reading input directory: %s", inPath)
node.InputPath = inPath
// read config
newConfig := new(PathConfig)
helper.Log.Debug("looking for config.yml ...")
configFile := inPath + "/config.yml"
if _, err := os.Stat(configFile); os.IsNotExist(err) {
helper.Log.Debug("no config.yml found in this directory, using upper configs")
helper.Merge(newConfig, conf)
// remove this
newConfig.This = ThisPathConfig{}
} else {
helper.Log.Debug("reading config...")
data, err := ioutil.ReadFile(configFile)
if err != nil {
helper.Log.Panicf("could not read file '%s': %s", configFile, err)
}
err = yaml.Unmarshal(data, newConfig)
if err != nil {
helper.Log.Panicf("could not parse YAML file '%s': %s", configFile, err)
}
2019-03-18 13:34:52 +01:00
2019-03-18 15:14:41 +01:00
helper.Log.Debug("merging config with upper config")
oldThis := newConfig.This
helper.Merge(newConfig, conf)
newConfig.This = oldThis
helper.Log.Debug(spew.Sdump(newConfig))
}
node.Config = newConfig
// calc outDir
stripedDir := dir
var regexStr *string
if newConfig.Path != nil {
regexStr = newConfig.Path.Strip
}
if regexStr != nil && *regexStr != "" {
if regex, err := regexp.Compile(*regexStr); err != nil {
helper.Log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, inBase+"/"+dir, err)
} else {
stripedDir = regex.ReplaceAllString(stripedDir, "$1")
}
}
if node.Config.This.Navname == nil {
navname := strings.Replace(stripedDir, "_", " ", -1)
node.Config.This.Navname = &navname
2019-03-18 13:34:52 +01:00
}
2019-03-18 15:14:41 +01:00
stripedDir = slug.Slug(stripedDir)
outPath := outBase + "/" + stripedDir
outPath = path.Clean(outPath)
helper.Log.Infof("calculated output directory: %s", outPath)
node.OutputPath = outPath
// handle collections
node.handleCollections()
2019-03-18 13:34:52 +01:00
}
2019-03-18 15:14:41 +01:00
func Add2Nav(currentNode *TreeNode, pathConfig *PathConfig, tplFilename, outDir string, navname string, ctx interface{}, dataMapKey string, body string, hidden bool) {
newNode := new(TreeNode)
2019-03-18 15:35:17 +01:00
newNode.fillConfig(
2019-03-18 13:34:52 +01:00
currentNode.InputPath,
currentNode.OutputPath,
outDir,
pathConfig,
)
if navname != "" {
2019-03-18 15:14:41 +01:00
newNode.Config.This = ThisPathConfig{
2019-03-18 13:34:52 +01:00
Navname: &navname,
}
}
if dataMapKey != "" {
2019-03-18 15:14:41 +01:00
if newNode.Config.Data == nil {
newNode.Config.Data = make(MapString)
2019-03-18 13:34:52 +01:00
}
// as submap in Data
2019-03-18 15:14:41 +01:00
newNode.Config.Data[dataMapKey] = ctx
2019-03-18 13:34:52 +01:00
} else if m, ok := ctx.(map[string]interface{}); ok {
// direct set data
2019-03-18 15:14:41 +01:00
newNode.Config.Data = m
2019-03-18 13:34:52 +01:00
}
// fake via normal file behavior
2019-03-18 15:14:41 +01:00
newNode.Config.Template = &tplFilename
newNode.InputFiles = []string{""} // empty file is special for use InputString
2019-03-18 13:34:52 +01:00
indexInFile := ""
indexOutFile := "index.html"
2019-03-18 15:14:41 +01:00
if idx := newNode.Config.Index; idx != nil {
2019-03-18 13:34:52 +01:00
if idx.OutputFile != nil && *idx.OutputFile != "" {
indexOutFile = *idx.OutputFile
}
}
2019-03-18 15:14:41 +01:00
newNode.Config.Index = &IndexConfig{
2019-03-18 13:34:52 +01:00
InputFile: &indexInFile,
OutputFile: &indexOutFile,
InputString: &body,
}
2019-03-18 15:14:41 +01:00
newNode.Hidden = hidden
2019-03-18 13:34:52 +01:00
2019-03-18 15:14:41 +01:00
currentNode.Sub = append(currentNode.Sub, newNode)
2019-03-18 13:34:52 +01:00
}