Creating site menu configuration and have the docs site use it
authorspf13 <steve.francia@gmail.com>
Thu, 24 Apr 2014 22:11:08 +0000 (16:11 -0600)
committerspf13 <steve.francia@gmail.com>
Thu, 24 Apr 2014 22:11:08 +0000 (16:11 -0600)
docs/config.toml [new file with mode: 0644]
docs/config.yaml [deleted file]
hugolib/menu.go
hugolib/page.go
hugolib/site.go

diff --git a/docs/config.toml b/docs/config.toml
new file mode 100644 (file)
index 0000000..852cb14
--- /dev/null
@@ -0,0 +1,27 @@
+baseurl = "http://hugo.spf13.com"
+
+[indexes]
+  tag = "tags"
+  group = "groups"
+
+[[menu.main]]
+    name = "getting started"
+    weight = -100
+[[menu.main]]
+    name = "content"
+    weight = -90
+[[menu.main]]
+    name = "layout"
+    weight = -80
+[[menu.main]]
+    name = "taxonomy"
+    weight = -70
+[[menu.main]]
+    name = "extras"
+    weight = -60
+[[menu.main]]
+    name = "community"
+    weight = -50
+[[menu.main]]
+    name = "tutorials"
+    weight = -40
diff --git a/docs/config.yaml b/docs/config.yaml
deleted file mode 100644 (file)
index 7b57d5b..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-indexes:
-  tag: 'tags'
-  group: 'groups'
-baseurl: 'http://hugo.spf13.com'
-...
index 7c43cfe151afbaa9e36caa2c430888790a18ced2..abbdf281145029deb0e38f578a4d840b81e15a2f 100644 (file)
 
 package hugolib
 
-import "sort"
+import (
+       "sort"
+       "strings"
+
+       "github.com/spf13/cast"
+)
 
 type MenuEntry struct {
        Url      string
@@ -39,6 +44,22 @@ func (me *MenuEntry) HasChildren() bool {
        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 {
index 0015c79842de3dd0c4eda8fa6a8ee3ae63b14ba7..679f13ebfa0184093de0265a45e1ad71f2f3c148 100644 (file)
@@ -490,18 +490,7 @@ func (page *Page) Menus() PageMenus {
                                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
index 03a74f55fda790a911a2bd7bcd8425f1afcc6ead..b4d39a6a4b67d1e89d13518307ed6fb7b9ca577a 100644 (file)
@@ -362,6 +362,40 @@ func (s *Site) BuildSiteMeta() (err error) {
        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 {
@@ -370,6 +404,13 @@ func (s *Site) assembleMenus() {
        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() {