diff --git a/example/content/de/main/01_home/config.yml b/example/content/de/main/01_home/config.yml new file mode 100644 index 0000000..33b57a7 --- /dev/null +++ b/example/content/de/main/01_home/config.yml @@ -0,0 +1,2 @@ +This: + Navname: HOME \ No newline at end of file diff --git a/example/templates/index.html b/example/templates/index.html index 73965de..9a6a54c 100644 --- a/example/templates/index.html +++ b/example/templates/index.html @@ -3,12 +3,36 @@ {{ Meta.Title }} + - - {{{ Body }}} -
+    
+        
+
main Level 1
+ +
- {{{ Data }}} -
+
+ {{{ Body }}} +
+ + \ No newline at end of file diff --git a/main.go b/main.go index af6a5ca..b6572be 100644 --- a/main.go +++ b/main.go @@ -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) }