Creating site menu configuration and have the docs site use it

This commit is contained in:
spf13 2014-04-24 16:11:08 -06:00
parent ac82fe32af
commit bdf7cd9f9d
5 changed files with 91 additions and 18 deletions

27
docs/config.toml Normal file
View file

@ -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

View file

@ -1,5 +0,0 @@
indexes:
tag: 'tags'
group: 'groups'
baseurl: 'http://hugo.spf13.com'
...

View file

@ -13,7 +13,12 @@
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 {

View 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

View 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() {