navigation

This commit is contained in:
Sebastian Frank 2019-02-12 15:28:22 +01:00
parent 849e8a945e
commit 223af0a42f
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
3 changed files with 104 additions and 13 deletions

View File

@ -0,0 +1,2 @@
This:
Navname: HOME

View File

@ -3,12 +3,36 @@
<title>{{ Meta.Title }}</title>
<meta name="description" content="{{ Meta.Description }}" />
<meta name="keywords" content="{{ Meta.Keywords }}" />
<style>
a {
color: black;
}
body {
color: #333;
}
</style>
</meta>
<body>
{{{ Body }}}
<pre>
<body style="margin: 0; padding: 2;">
<header style="background-color: lightgrey; padding: 20px;">
<div><b>main Level 1</b></div>
<ul>
{{#each NavMap.de.SubMap.main.SubSlice }}
<li><a href="{{ GoTo }}">{{ Navname }}</a></li>
{{/each}}
</ul>
</header>
{{{ Data }}}
</pre>
<div style="padding: 20px;">
{{{ Body }}}
</div>
<footer style="background-color: lightgrey; padding: 20px;">
<div><b>service Level 1</b></div>
<ul>
{{#each NavMap.de.SubMap.service.SubSlice }}
<li><a href="{{ GoTo }}">{{ Navname }}</a></li>
{{/each}}
</ul>
</footer>
</body>
</html>

81
main.go
View File

@ -8,6 +8,8 @@ import (
"regexp"
"strings"
"github.com/imdario/mergo"
"github.com/aymerick/raymond"
"github.com/davecgh/go-spew/spew"
"github.com/op/go-logging"
@ -147,12 +149,12 @@ func readContentDir(inBase string, outBase string, dir string, conf *PathConfig,
tree.InputPath = inPath
// read config
var newConfig *PathConfig
newConfig := new(PathConfig)
log.Debug("looking for config.yml ...")
configFile := inPath + "/config.yml"
if _, err = os.Stat(configFile); os.IsNotExist(err) {
log.Debug("no config.yml found in this directory, using upper configs")
newConfig = conf
mergo.Merge(newConfig, conf)
// remove this
newConfig.This.GoTo = nil
newConfig.This.Navname = nil
@ -162,7 +164,6 @@ func readContentDir(inBase string, outBase string, dir string, conf *PathConfig,
if err != nil {
log.Panicf("could not read file '%s': %s", configFile, err)
}
newConfig = new(PathConfig)
err = yaml.Unmarshal(data, newConfig)
if err != nil {
log.Panicf("could not parse YAML file '%s': %s", configFile, err)
@ -191,6 +192,11 @@ func readContentDir(inBase string, outBase string, dir string, conf *PathConfig,
log.Noticef("calculated output directory: %s", outPath)
tree.OutputPath = outPath
if tree.Config.This.Navname == nil {
navname := strings.Replace(stripedDir, "_", " ", -1)
tree.Config.This.Navname = &navname
}
// first only files
for _, f := range files {
p := inPath + "/" + f.Name()
@ -218,6 +224,60 @@ func readContentDir(inBase string, outBase string, dir string, conf *PathConfig,
}
}
type navElement struct {
Navname string
GoTo string
SubMap *map[string]*navElement
SubSlice *[]*navElement
}
func buildNavigation(conf *PathConfigTree, curNavMap *map[string]*navElement, curNavSlice *[]*navElement, activeNav string) {
for _, el := range conf.Sub {
subMap := make(map[string]*navElement)
subSlice := make([]*navElement, 0)
navEl := navElement{
SubMap: &subMap,
SubSlice: &subSlice,
}
elPath := strings.TrimPrefix(el.OutputPath, *outDir+"/")
n := el.Config.This.Navname
if n != nil {
navEl.Navname = *n
}
g := el.Config.This.GoTo
if g != nil {
if strings.HasPrefix(*g, "/") {
// abslute
navEl.GoTo = *g
} else {
// relative
navEl.GoTo = elPath + "/" + *g
}
} else {
navEl.GoTo = elPath + "/"
}
if activeNav != "" && activeNav != "/" {
// calculate relative path
tmpPath := ""
for i := len(strings.Split(activeNav, "/")); i > 0; i-- {
tmpPath += "../"
}
navEl.GoTo = tmpPath + navEl.GoTo
//navEl.GoTo = path.Clean(navEl.GoTo)
}
(*curNavMap)[navEl.Navname] = &navEl
if curNavSlice != nil {
*curNavSlice = append(*curNavSlice, &navEl)
}
buildNavigation(el, &subMap, &subSlice, activeNav)
}
}
func processContent(conf *PathConfigTree) {
log.Debugf("trying to create output directory: %s", conf.OutputPath)
@ -247,13 +307,12 @@ func processContent(conf *PathConfigTree) {
}
log.Noticef("processing input file '%s'", inFile)
var newConfig *PathConfig
newConfig := new(PathConfig)
regex := regexp.MustCompile("(?sm)^---(.*)^---")
yamlData := regex.Find(input)
if string(yamlData) != "" {
log.Debugf("found yaml header in '%s', merging config", inFile)
newConfig = new(PathConfig)
err = yaml.Unmarshal(yamlData, newConfig)
if err != nil {
log.Panicf("could not parse YAML header from '%s': %s", inFile, err)
@ -265,7 +324,7 @@ func processContent(conf *PathConfigTree) {
input = regex.ReplaceAll(input, []byte(""))
} else {
newConfig = conf.Config
mergo.Merge(newConfig, conf.Config)
}
// ignore ???
@ -324,12 +383,17 @@ func processContent(conf *PathConfigTree) {
}
}
// build navigation
curNavPath := strings.TrimPrefix(conf.OutputPath, *outDir+"/")
var navMap = make(map[string]*navElement)
buildNavigation(contentConfig, &navMap, nil, curNavPath)
// read yaml header as data for template
ctx := make(map[string]interface{})
ctx["Meta"] = newConfig.Meta
ctx["Data"] = newConfig.Data
ctx["NavMap"] = navMap
ctx["Body"] = string(html)
log.Warning(spew.Sdump(ctx))
result, err := template.Exec(ctx)
if err != nil {
@ -463,9 +527,10 @@ func main() {
defaultPathConfig.Filename.OutputExtension = &defaultFilenameOutputExtension
readContentDir(*inDir+"/content", *outDir, "", defaultPathConfig, contentConfig)
//spew.Dump(contentConfig)
//spew.Dump(navMap)
processContent(contentConfig)
}