render paths via fnRender correct
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sebastian Frank 2019-02-28 14:14:31 +01:00
parent d78ecf4682
commit e943271561
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
7 changed files with 125 additions and 68 deletions

View File

@ -55,13 +55,14 @@ type PathConfig struct {
Filename *FilenameConfig `yaml:"Filename"`
Markdown *MarkdownConfig `yaml:"Markdown"`
Data interface{} `yaml:"Data"`
Data map[string]interface{} `yaml:"Data"`
}
// PathConfigTree is complete config tree of content dir
type PathConfigTree struct {
InputPath string
OutputPath string
Hidden bool // for collections which are not part of the navigation
InputFiles []string
OtherFiles []string

View File

@ -164,14 +164,20 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L]
}
for _, file := range conf.InputFiles {
inFile := conf.InputPath + "/" + file
Log.Debugf("reading file: %s", inFile)
var input []byte
inFile := "InputString"
input, err := ioutil.ReadFile(inFile)
if err != nil {
Log.Panicf("could not read '%s':%s", inFile, err)
if file != "" {
inFile = conf.InputPath + "/" + file
Log.Debugf("reading file: %s", inFile)
var err error
input, err = ioutil.ReadFile(inFile)
if err != nil {
Log.Panicf("could not read '%s':%s", inFile, err)
}
Log.Infof("processing input file '%s'", inFile)
}
Log.Infof("processing input file '%s'", inFile)
newConfig := new(config.PathConfig)
@ -179,7 +185,7 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L]
yamlData := regex.Find(input)
if string(yamlData) != "" {
Log.Debugf("found yaml header in '%s', merging config", inFile)
err = yaml.Unmarshal(yamlData, newConfig)
err := yaml.Unmarshal(yamlData, newConfig)
if err != nil {
Log.Panicf("could not parse YAML header from '%s': %s", inFile, err)
}
@ -228,7 +234,10 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L]
indexOutputFile = i.OutputFile
}
if indexInputFile != nil && *indexInputFile == file && indexOutputFile != nil && *indexOutputFile != "" {
if indexInputFile != nil &&
*indexInputFile == file &&
indexOutputFile != nil &&
*indexOutputFile != "" {
outputFilename = *indexOutputFile
} else {
if stripRegex != nil && *stripRegex != "" {
@ -336,7 +345,9 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L]
}
}
for _, el := range conf.Sub {
ProcessContent(rootConf, el)
i := 0
for i < len(conf.Sub) {
ProcessContent(rootConf, conf.Sub[i])
i++
}
}

View File

@ -23,6 +23,10 @@ type NavElement struct {
func BuildNavigation(conf *config.PathConfigTree, curNavMap *map[string]*NavElement, curNavSlice *[]*NavElement, navActive *[]*NavElement, activeNav string) {
for _, el := range conf.Sub {
if el.Hidden {
continue // ignore hidden nav points from collections
}
var ignNav *string
if p := el.Config.Path; p != nil {
ignNav = p.IgnoreForNav

View File

@ -4,14 +4,11 @@ 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"
)
@ -55,66 +52,109 @@ 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 {
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")
func RenderFn(templateFilename, outDir, ctx *pongo2.Value, param ...*pongo2.Value) *pongo2.Value {
ctxMapKey := ""
for i, p := range param {
switch i {
case 0:
ctxMapKey = p.String()
}
}
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(),
fillNodeConfig(
newNodeConfig,
currentTreeNodeConfig.InputPath,
currentTreeNodeConfig.OutputPath,
outDir.String(),
currentPathConfig,
&newContext,
)
if err != nil {
panic(err)
if newNodeConfig.Config.Data == nil {
newNodeConfig.Config.Data = make(map[string]interface{})
}
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 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()
if indexOutputFile != nil && *indexOutputFile != "" {
outputFilename = *indexOutputFile
// 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
}
}
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)
newNodeConfig.Config.Index = &config.IndexConfig{
InputFile: &indexInFile,
OutputFile: &indexOutFile,
}
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)
}

View File

@ -65,6 +65,7 @@ hr {height:1px; background:none; border-bottom:dotted 1px #666; margin-bottom:20
.btn {
padding:8px 15px;
background:#464645;
border: 1px solid #fff;
color:#FFF;
transition:all 0.3s;
text-transform:none;

View File

@ -10,7 +10,7 @@
{{ e.teaser|markdown }}
{% if e.body %}
<a href="{{ e.title|slugify }}" class="btn">mehr lesen</a>
{{ fnRender("base_blog_details.html", e.title, "details", e) }}
{{ fnRender("base_blog_details.html", e.title, e) }}
{% endif %}
{% endfor %}
{% endblock part0 %}
@ -27,7 +27,7 @@
{{ e.teaser|markdown }}
{% if e.body %}
<a href="{{ e.title|slugify }}" class="btn">mehr lesen</a>
{{ fnRender("base_blog_details.html", e.title, "details", e) }}
{{ fnRender("base_blog_details.html", e.title, e) }}
{% endif %}
{% endfor %}
{% endblock part1 %}

View File

@ -1,10 +1,10 @@
{% extends 'base.html' %}
{% block part0 %}
<h1>{{ details.title }}</h1>
{{ details.teaser|markdown }}
<h1>{{ Data.title }}</h1>
{{ Data.teaser|markdown }}
{% endblock part0 %}
{% block part1 %}
{{ details.body|markdown }}
{{ Data.body|markdown }}
{% endblock part1 %}