package hugolib
-import "sort"
+import (
+ "sort"
+ "strings"
+
+ "github.com/spf13/cast"
+)
type MenuEntry struct {
Url string
return me.Children != nil
}
+func (me *MenuEntry) MarshallMap(ime map[string]interface{}) {
+ for k, v := range ime {
+ loki := strings.ToLower(k)
+ switch loki {
+ case "url":
+ me.Url = cast.ToString(v)
+ case "weight":
+ me.Weight = cast.ToInt(v)
+ case "name":
+ me.Name = cast.ToString(v)
+ case "parent":
+ me.Parent = cast.ToString(v)
+ }
+ }
+}
+
//func (me *MenuEntry) RelUrl() string {
//link, err := p.permalink()
//if err != nil {
jww.ERROR.Printf("unable to process menus for %q\n", page.Title)
}
- for k, v := range ime {
- loki := strings.ToLower(k)
- switch loki {
- case "weight":
- menuEntry.Weight = cast.ToInt(v)
- case "name":
- menuEntry.Name = cast.ToString(v)
- case "parent":
- menuEntry.Parent = cast.ToString(v)
- }
- }
-
+ menuEntry.MarshallMap(ime)
ret[name] = &menuEntry
}
return ret
return
}
+func (s *Site) getMenusFromConfig() Menus {
+
+ ret := Menus{}
+
+ if menus := viper.GetStringMap("menu"); menus != nil {
+ for name, menu := range menus {
+ m, err := cast.ToSliceE(menu)
+ if err != nil {
+ jww.ERROR.Printf("unable to process menus in site config\n")
+ jww.ERROR.Println(err)
+ } else {
+ for _, entry := range m {
+ jww.DEBUG.Printf("found menu: %q, in site config\n", name)
+
+ menuEntry := MenuEntry{Menu: name}
+ ime, err := cast.ToStringMapE(entry)
+ if err != nil {
+ jww.ERROR.Printf("unable to process menus in site config\n")
+ jww.ERROR.Println(err)
+ }
+
+ menuEntry.MarshallMap(ime)
+ if ret[name] == nil {
+ ret[name] = &Menu{}
+ }
+ *ret[name] = ret[name].Add(&menuEntry)
+ }
+ }
+ }
+ return ret
+ }
+ return ret
+}
+
func (s *Site) assembleMenus() {
type twoD struct {
flat := map[twoD]*MenuEntry{}
children := map[twoD]Menu{}
+ menuConfig := s.getMenusFromConfig()
+ for name, menu := range menuConfig {
+ for _, me := range *menu {
+ flat[twoD{name, me.Name}] = me
+ }
+ }
+
//creating flat hash
for _, p := range s.Pages {
for name, me := range p.Menus() {