Permalink
Browse files

Fixing issue when two menu items have the same name.

  • Loading branch information...
1 parent 4f75ec9 commit a4a1e39a512d8b91ba24e86bb02596aff4c09896 @spf13 committed May 14, 2014
Showing with 43 additions and 29 deletions.
  1. +2 −2 docs/layouts/chrome/menu.html
  2. +32 −21 hugolib/menu.go
  3. +1 −1 hugolib/node.go
  4. +3 −3 hugolib/page.go
  5. +5 −2 hugolib/site.go
@@ -20,7 +20,7 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{{.Name}} <b class="caret"></b></a>
<ul class="dropdown-menu">
{{ range .Children }}
- <li{{if $currentNode.IsMenuCurrent "main" .Name}} class="active"{{end}}><a href="{{.Url}}"> {{ .Name }} </a> </li>
+ <li{{if $currentNode.IsMenuCurrent "main" . }} class="active"{{end}}><a href="{{.Url}}"> {{ .Name }} </a> </li>
{{ end }}
</ul>
{{else}}
@@ -39,7 +39,7 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{{.Name}} <b class="caret"></b></a>
<ul class="dropdown-menu">
{{ range .Children }}
- <li{{if $currentNode.IsMenuCurrent $menu .Name}} class="active"{{end}}><a href="{{.Url}}"> {{ .Name }} </a> </li>
+ <li{{if $currentNode.IsMenuCurrent $menu . }} class="active"{{end}}><a href="{{.Url}}"> {{ .Name }} </a> </li>
{{ end }}
</ul>
{{else}}
View
@@ -21,14 +21,15 @@ import (
)
type MenuEntry struct {
- Url string
- Name string
- Menu string
- PreName string
- PostName string
- Weight int
- Parent string
- Children Menu
+ Url string
+ Name string
+ Menu string
+ Identifier string
+ PreName string
+ PostName string
+ Weight int
+ Parent string
+ Children Menu
}
type Menu []*MenuEntry
@@ -44,6 +45,27 @@ func (me *MenuEntry) HasChildren() bool {
return me.Children != nil
}
+func (me *MenuEntry) KeyName() string {
+ if me.Identifier != "" {
+ return me.Identifier
+ }
+ return me.Name
+}
+
+func (me *MenuEntry) hopefullyUniqueId() string {
+ if me.Identifier != "" {
+ return me.Identifier
+ } else if me.Url != "" {
+ return me.Url
+ } else {
+ return me.Name
+ }
+}
+
+func (me *MenuEntry) IsEqual(inme *MenuEntry) bool {
+ return me.hopefullyUniqueId() == inme.hopefullyUniqueId() && me.Parent == inme.Parent
+}
+
func (me *MenuEntry) MarshallMap(ime map[string]interface{}) {
for k, v := range ime {
loki := strings.ToLower(k)
@@ -54,25 +76,14 @@ func (me *MenuEntry) MarshallMap(ime map[string]interface{}) {
me.Weight = cast.ToInt(v)
case "name":
me.Name = cast.ToString(v)
+ case "identifier":
+ me.Identifier = cast.ToString(v)
case "parent":
me.Parent = cast.ToString(v)
}
}
}
-//func (me *MenuEntry) RelUrl() string {
-//link, err := p.permalink()
-//if err != nil {
-//return "", err
-//}
-
-//link.Scheme = ""
-//link.Host = ""
-//link.User = nil
-//link.Opaque = ""
-//return link.String(), nil
-//}
-
func (m Menu) Add(me *MenuEntry) Menu {
app := func(slice Menu, x ...*MenuEntry) Menu {
n := len(slice) + len(x)
View
@@ -39,7 +39,7 @@ func (n *Node) Now() time.Time {
func (n *Node) HasMenuCurrent(menu string, me *MenuEntry) bool {
return false
}
-func (n *Node) IsMenuCurrent(menu string, name string) bool {
+func (n *Node) IsMenuCurrent(menu string, me *MenuEntry) bool {
return false
}
View
@@ -405,7 +405,7 @@ func (page *Page) HasMenuCurrent(menu string, me *MenuEntry) bool {
if m, ok := menus[menu]; ok {
if me.HasChildren() {
for _, child := range me.Children {
- if child.Name == m.Name {
+ if child.IsEqual(m) {
return true
}
}
@@ -416,11 +416,11 @@ func (page *Page) HasMenuCurrent(menu string, me *MenuEntry) bool {
}
-func (page *Page) IsMenuCurrent(menu string, name string) bool {
+func (page *Page) IsMenuCurrent(menu string, inme *MenuEntry) bool {
menus := page.Menus()
if me, ok := menus[menu]; ok {
- return me.Name == name
+ return me.IsEqual(inme)
}
return false
View
@@ -419,14 +419,17 @@ func (s *Site) assembleMenus() {
menuConfig := s.getMenusFromConfig()
for name, menu := range menuConfig {
for _, me := range *menu {
- flat[twoD{name, me.Name}] = me
+ flat[twoD{name, me.KeyName()}] = me
}
}
//creating flat hash
for _, p := range s.Pages {
for name, me := range p.Menus() {
- flat[twoD{name, me.Name}] = me
+ if _, ok := flat[twoD{name, me.KeyName()}]; ok {
+ jww.ERROR.Printf("Two or more menu items have the same name/identifier in %q Menu. Identified as %q.\n Rename or set a unique identifier. \n", name, me.KeyName())
+ }
+ flat[twoD{name, me.KeyName()}] = me
}
}

0 comments on commit a4a1e39

Please sign in to comment.