pongo2 as template engine

This commit is contained in:
Sebastian Frank
2019-02-14 13:46:33 +01:00
parent aeb7517473
commit 4df0b5e31f
16 changed files with 142 additions and 134 deletions

45
main.go
View File

@@ -10,8 +10,8 @@ import (
"github.com/imdario/mergo"
"github.com/aymerick/raymond"
"github.com/davecgh/go-spew/spew"
"github.com/flosch/pongo2"
"github.com/op/go-logging"
cpy "github.com/otiai10/copy"
"gopkg.in/russross/blackfriday.v2"
@@ -23,7 +23,7 @@ var log = logging.MustGetLogger("myLogger")
var inDir *string
var outDir *string
var templateCache = make(map[string]*raymond.Template)
var templateCache = make(map[string]*pongo2.Template)
// GlobalConfig is config which is used only once in root dir
type GlobalConfig struct {
@@ -388,9 +388,9 @@ func processContent(conf *PathConfigTree) {
// use --- for splitting document in markdown parts
regex := regexp.MustCompile("\\r?\\n\\r?---\\r?\\n\\r?")
inputParts := regex.Split(string(input), -1)
htmlParts := make([]string, 0)
htmlParts := make([]*pongo2.Value, 0)
for _, iPart := range inputParts {
htmlParts = append(htmlParts, string(blackfriday.Run([]byte(iPart))))
htmlParts = append(htmlParts, pongo2.AsSafeValue(string(blackfriday.Run([]byte(iPart)))))
}
log.Debugf("rendering template '%s' for '%s'", *newConfig.Template, outFile)
@@ -398,7 +398,7 @@ func processContent(conf *PathConfigTree) {
template := templateCache[templateFile]
if template == nil {
var err error
if template, err = raymond.ParseFile(templateFile); err != nil {
if template, err = pongo2.FromFile(templateFile); err != nil {
log.Panicf("could not parse template '%s': %s", templateFile, err)
} else {
templateCache[templateFile] = template
@@ -418,16 +418,16 @@ func processContent(conf *PathConfigTree) {
buildNavigation(contentConfig, &navMap, &navSlice, &navActive, curNavPath)
// read yaml header as data for template
ctx := make(map[string]interface{})
ctx := make(pongo2.Context)
ctx["Meta"] = newConfig.Meta
ctx["Data"] = newConfig.Data
ctx["NavMap"] = navMap
ctx["NavSlice"] = navSlice
ctx["NavActive"] = navActive
ctx["Body"] = string(html)
ctx["Body"] = pongo2.AsSafeValue(string(html))
ctx["BodyParts"] = htmlParts
result, err := template.Exec(ctx)
result, err := template.Execute(ctx)
if err != nil {
log.Panicf("could not execute template '%s' for input file '%s': %s", templateFile, inFile, err)
}
@@ -591,7 +591,7 @@ func main() {
log.Debugf("reading input directory %s", *inDir)
defaultTemplate := "index.html"
defaultTemplate := "base.html"
defaultInputFile := "README.md"
defaultOutputFile := "index.html"
defaultPathStrip := "^[0-9]*_(.*)"
@@ -615,33 +615,6 @@ func main() {
//spew.Dump(navMap)
partialsPath := *inDir + "/templates/partials"
if d, err := os.Stat(partialsPath); !os.IsNotExist(err) {
if d != nil && d.IsDir() {
log.Debugf("register template partials from '%s'", partialsPath)
if entries, err := ioutil.ReadDir(partialsPath); err == nil {
for _, f := range entries {
if !f.IsDir() {
pFile := partialsPath + "/" + f.Name()
log.Infof("registering partial: %s", pFile)
pContent, err := ioutil.ReadFile(pFile)
if err != nil {
log.Panicf("could not read partial '%s': %s", pFile, err)
}
raymond.RegisterPartial(f.Name(), string(pContent))
}
}
} else {
log.Panicf("could not read from partials directory '%s': %s", partialsPath, err)
}
} else if err == nil {
log.Panicf("template partials directory '%s' is not a directory", partialsPath)
} else {
log.Panicf("unknown error on partials directory '%s': %s", partialsPath, err)
}
}
processContent(contentConfig)
processAssets()