generate details sites from fnRender
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sebastian Frank
2019-02-28 10:43:30 +01:00
parent 650bdc2fd6
commit 39f1932cc3
10 changed files with 208 additions and 142 deletions

24
helper/dir.go Normal file
View File

@@ -0,0 +1,24 @@
package helper
import "os"
// CreateDirectory creates direcory with all missing parents and panic if error
func CreateDirectory(dir string) {
Log.Debugf("trying to create output directory: %s", dir)
if dirH, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(dir, 0755)
if err != nil {
Log.Panicf("could not create output directory '%s': %s", dir, err)
}
Log.Noticef("created output directory: %s", dir)
} else if dirH != nil {
if dirH.IsDir() {
Log.Noticef("output directory '%s' already exists", dir)
} else {
Log.Panicf("output directory '%s' is no directory", dir)
}
} else {
Log.Panicf("unknown error for output directory '%s': %s", dir, err)
}
}

View File

@@ -11,9 +11,12 @@ import (
)
var templateCache = make(map[string]*pongo2.Template)
var currentContext map[string]interface{}
var currentContext *map[string]interface{}
var currentTreeNodeConfig *config.PathConfigTree
var currentPathConfig *config.PathConfig
var templateDir string
// BackToRoot builds ../../ string
func BackToRoot(curNavPath string) string {
tmpPath := ""
if curNavPath != "" {
@@ -30,8 +33,10 @@ func SetTemplateDir(dir string) {
}
// RenderTemplate renders a pongo2 template with context
func RenderTemplate(filename string, ctx map[string]interface{}) (string, error) {
func RenderTemplate(filename string, treeNodeConfig *config.PathConfigTree, pathConfig *config.PathConfig, ctx *map[string]interface{}) (string, error) {
currentContext = ctx
currentTreeNodeConfig = treeNodeConfig
currentPathConfig = pathConfig
templateFile := templateDir + "/" + filename
template := templateCache[templateFile]
if template == nil {
@@ -43,9 +48,10 @@ func RenderTemplate(filename string, ctx map[string]interface{}) (string, error)
}
}
return template.Execute(ctx)
return template.Execute(*ctx)
}
// FixAssetsPath replaces assets path based on current path
func FixAssetsPath(str, curNavPath string) string {
if find := config.Config.Assets.FixTemplate.Find; find != "" {
Log.Debugf("fixing assets paths for path '%s'", curNavPath)

View File

@@ -4,10 +4,14 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"path"
"strings"
"gitbase.de/apairon/mark2web/config"
"github.com/davecgh/go-spew/spew"
"github.com/extemporalgenome/slug"
"github.com/flosch/pongo2"
)
@@ -52,16 +56,65 @@ func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value {
// RenderFn renders a pongo2 template with additional context
func RenderFn(templateFilename, outDir, subCtxName, ctx *pongo2.Value) *pongo2.Value {
currentContext[subCtxName.String()] = ctx
result, err := RenderTemplate(templateFilename.String(), currentContext)
oDirSlug := slug.Slug(outDir.String())
navPath := path.Clean((*currentContext)["CurrentPath"].(string) + "/" + oDirSlug)
outputPath := path.Clean(currentTreeNodeConfig.OutputPath + "/" + oDirSlug)
CreateDirectory(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
newNodeConfig := new(config.PathConfigTree)
if err := config.Merge(newNodeConfig, currentTreeNodeConfig); err != nil {
Log.Panicf("unable to merge tree node config")
}
newNodeConfig.OutputPath = outputPath
// 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,
currentContext["CurrentPath"].(string),
navPath,
)
spew.Dump(result)
//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 := 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)
}