ref: 9ecf58e29ba8dafb99f51bfba9bfe9e576410d73
parent: 69c1944f1fa78502aa3bd19ff59b2a96c8c7a6c4
author: spf13 <steve.francia@gmail.com>
date: Tue Apr 22 22:59:19 EDT 2014
new menus system including active link & nesting
--- /dev/null
+++ b/hugolib/menu.go
@@ -1,0 +1,144 @@
+// Copyright © 2013-14 Steve Francia <spf@spf13.com>.
+//
+// Licensed under the Simple Public License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://opensource.org/licenses/Simple-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package hugolib
+
+import "sort"
+
+type MenuEntry struct {+ Url string
+ Name string
+ Menu string
+ PreName string
+ PostName string
+ Weight int
+ Parent string
+ Children Menu
+}
+
+type Menu []*MenuEntry
+type Menus map[string]*Menu
+type PageMenus map[string]*MenuEntry
+
+func (me *MenuEntry) AddChild(child *MenuEntry) {+ me.Children = append(me.Children, child)
+ me.Children.Sort()
+}
+
+func (me *MenuEntry) HasChildren() bool {+ return me.Children != nil
+}
+
+//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)
+ if n > cap(slice) {+ size := cap(slice) * 2
+ if size < n {+ size = n
+ }
+ new := make(Menu, size)
+ copy(new, slice)
+ slice = new
+ }
+ slice = slice[0:n]
+ copy(slice[n-len(x):], x)
+ return slice
+ }
+
+ m = app(m, me)
+ m.Sort()
+ return m
+}
+
+/*
+ * Implementation of a custom sorter for Menu
+ */
+
+// A type to implement the sort interface for Menu
+type MenuSorter struct {+ menu Menu
+ by MenuEntryBy
+}
+
+// Closure used in the Sort.Less method.
+type MenuEntryBy func(m1, m2 *MenuEntry) bool
+
+func (by MenuEntryBy) Sort(menu Menu) {+ ms := &MenuSorter{+ menu: menu,
+ by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
+ }
+ sort.Sort(ms)
+}
+
+var DefaultMenuEntrySort = func(m1, m2 *MenuEntry) bool {+ if m1.Weight == m2.Weight {+ return m1.Name < m2.Name
+ } else {+ return m1.Weight < m2.Weight
+ }
+}
+
+func (ms *MenuSorter) Len() int { return len(ms.menu) }+func (ms *MenuSorter) Swap(i, j int) { ms.menu[i], ms.menu[j] = ms.menu[j], ms.menu[i] }+
+// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
+func (ms *MenuSorter) Less(i, j int) bool { return ms.by(ms.menu[i], ms.menu[j]) }+
+func (p Menu) Sort() {+ MenuEntryBy(DefaultMenuEntrySort).Sort(p)
+}
+
+func (p Menu) Limit(n int) Menu {+ if len(p) < n {+ return p[0:n]
+ } else {+ return p
+ }
+}
+
+func (p Menu) ByWeight() Menu {+ MenuEntryBy(DefaultMenuEntrySort).Sort(p)
+ return p
+}
+
+func (p Menu) ByName() Menu {+ title := func(m1, m2 *MenuEntry) bool {+ return m1.Name < m2.Name
+ }
+
+ MenuEntryBy(title).Sort(p)
+ return p
+}
+
+func (m Menu) Reverse() Menu {+ for i, j := 0, len(m)-1; i < j; i, j = i+1, j-1 {+ m[i], m[j] = m[j], m[i]
+ }
+
+ return m
+}
--- a/hugolib/node.go
+++ b/hugolib/node.go
@@ -31,7 +31,18 @@
UrlPath
}
-func (n Node) RSSlink() template.HTML {+func (n *Node) Now() time.Time {+ return time.Now()
+}
+
+func (n *Node) HasMenuCurrent(menu string, me *MenuEntry) bool {+ return false
+}
+func (n *Node) IsMenuCurrent(menu string, name string) bool {+ return false
+}
+
+func (n *Node) RSSlink() template.HTML {return n.RSSLink
}
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -419,6 +419,97 @@
return nil
}
+func (page *Page) HasMenuCurrent(menu string, me *MenuEntry) bool {+ menus := page.Menus()
+
+ if m, ok := menus[menu]; ok {+ if me.HasChildren() {+ for _, child := range me.Children {+ if child.Name == m.Name {+ return true
+ }
+ }
+ }
+ }
+
+ return false
+
+}
+
+func (page *Page) IsMenuCurrent(menu string, name string) bool {+ menus := page.Menus()
+
+ if me, ok := menus[menu]; ok {+ return me.Name == name
+ }
+
+ return false
+}
+
+func (page *Page) Menus() PageMenus {+ ret := PageMenus{}+
+ if ms, ok := page.Params["menu"]; ok {+ link, _ := page.Permalink()
+
+ me := MenuEntry{Name: page.LinkTitle(), Weight: page.Weight, Url: link}+
+ // Could be the name of the menu to attach it to
+ mname, err := cast.ToStringE(ms)
+
+ if err == nil {+ me.Menu = mname
+ ret[mname] = &me
+ return ret
+ }
+
+ // Could be an slice of strings
+ mnames, err := cast.ToStringSliceE(ms)
+
+ if err == nil {+ for _, mname := range mnames {+ me.Menu = mname
+ ret[mname] = &me
+ return ret
+ }
+ }
+
+ // Could be a structured menu entry
+ menus, err := cast.ToStringMapE(ms)
+
+ if err != nil {+ jww.ERROR.Printf("unable to process menus for %q\n", page.Title)+ }
+
+ for name, menu := range menus {+ menuEntry := MenuEntry{Name: page.LinkTitle(), Url: link, Weight: page.Weight, Menu: name}+ jww.DEBUG.Printf("found menu: %q, in %q\n", name, page.Title)+
+ ime, err := cast.ToStringMapE(menu)
+ 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)
+ }
+ }
+
+ ret[name] = &menuEntry
+ }
+ return ret
+ }
+
+ return nil
+}
+
type frontmatterType struct {markstart, markend []byte
parse func([]byte) (interface{}, error)--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -63,6 +63,7 @@
Sections Taxonomy
Info *SiteInfo
Shortcodes map[string]ShortcodeFunc
+ Menus Menus
timer *nitro.B
Target target.Output
Alias target.AliasPublisher
@@ -76,6 +77,7 @@
Taxonomies TaxonomyList
Indexes *TaxonomyList // legacy, should be identical to Taxonomies
Recent *Pages
+ Menus *Menus
Title string
Author map[string]string
LanguageCode string
@@ -241,6 +243,8 @@
Base: s.absContentDir(),
}
+ s.Menus = Menus{}+
s.initializeSiteInfo()
s.Shortcodes = make(map[string]ShortcodeFunc)
@@ -266,6 +270,7 @@
Copyright: viper.GetString("copyright"), DisqusShortname: viper.GetString("DisqusShortname"),Recent: &s.Pages,
+ Menus: &s.Menus,
Params: params,
Permalinks: permalinks,
}
@@ -343,6 +348,65 @@
}
func (s *Site) BuildSiteMeta() (err error) {+
+ s.assembleMenus()
+
+ if len(s.Pages) == 0 {+ return
+ }
+
+ s.assembleTaxonomies()
+ s.assembleSections()
+ s.Info.LastChange = s.Pages[0].Date
+
+ return
+}
+
+func (s *Site) assembleMenus() {+
+ type twoD struct {+ MenuName, EntryName string
+ }
+ flat := map[twoD]*MenuEntry{}+ children := map[twoD]Menu{}+
+ //creating flat hash
+ for _, p := range s.Pages {+ for name, me := range p.Menus() {+ flat[twoD{name, me.Name}] = me+ }
+ }
+
+ // Create Children Menus First
+ for _, e := range flat {+ if e.Parent != "" {+ children[twoD{e.Menu, e.Parent}] = children[twoD{e.Menu, e.Parent}].Add(e)+ }
+ }
+
+ // Placing Children in Parents (in flat)
+ for p, childmenu := range children {+ _, ok := flat[twoD{p.MenuName, p.EntryName}]+ if !ok {+ // if parent does not exist, create one without a url
+ flat[twoD{p.MenuName, p.EntryName}] = &MenuEntry{Name: p.EntryName, Url: ""}+ }
+ flat[twoD{p.MenuName, p.EntryName}].Children = childmenu+ }
+
+ // Assembling Top Level of Tree
+ for menu, e := range flat {+ if e.Parent == "" {+ _, ok := s.Menus[menu.MenuName]
+ if !ok {+ s.Menus[menu.MenuName] = &Menu{}+ }
+ *s.Menus[menu.MenuName] = s.Menus[menu.MenuName].Add(e)
+ }
+ }
+}
+
+func (s *Site) assembleTaxonomies() {s.Taxonomies = make(TaxonomyList)
s.Sections = make(Taxonomy)
@@ -376,6 +440,11 @@
}
}
+ s.Info.Taxonomies = s.Taxonomies
+ s.Info.Indexes = &s.Taxonomies
+}
+
+func (s *Site) assembleSections() { for i, p := range s.Pages { s.Sections.Add(p.Section, WeightedPage{s.Pages[i].Weight, s.Pages[i]})}
@@ -383,21 +452,6 @@
for k := range s.Sections {s.Sections[k].Sort()
}
-
- s.Info.Taxonomies = s.Taxonomies
- s.Info.Indexes = &s.Taxonomies
-
- if len(s.Pages) == 0 {- return
- }
- s.Info.LastChange = s.Pages[0].Date
-
- // populate pages with site metadata
- for _, p := range s.Pages {- p.Site = s.Info
- }
-
- return
}
func (s *Site) possibleTaxonomies() (taxonomies []string) {--
⑨