2015-12-07 13:57:01 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
2014-04-23 02:59:19 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-04-23 02:59:19 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-04-23 02:59:19 -04:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2014-04-24 18:11:08 -04:00
|
|
|
import (
|
2014-05-27 18:34:29 -04:00
|
|
|
"html/template"
|
2014-04-24 18:11:08 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
)
|
2014-04-23 02:59:19 -04:00
|
|
|
|
|
|
|
type MenuEntry struct {
|
2015-03-18 01:16:54 -04:00
|
|
|
URL string
|
2014-05-14 18:01:13 -04:00
|
|
|
Name string
|
|
|
|
Menu string
|
|
|
|
Identifier string
|
2014-05-27 18:34:29 -04:00
|
|
|
Pre template.HTML
|
|
|
|
Post template.HTML
|
2014-05-14 18:01:13 -04:00
|
|
|
Weight int
|
|
|
|
Parent string
|
|
|
|
Children Menu
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-14 18:01:13 -04:00
|
|
|
func (me *MenuEntry) KeyName() string {
|
|
|
|
if me.Identifier != "" {
|
|
|
|
return me.Identifier
|
|
|
|
}
|
|
|
|
return me.Name
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
func (me *MenuEntry) hopefullyUniqueID() string {
|
2014-05-14 18:01:13 -04:00
|
|
|
if me.Identifier != "" {
|
|
|
|
return me.Identifier
|
2015-03-18 01:16:54 -04:00
|
|
|
} else if me.URL != "" {
|
|
|
|
return me.URL
|
2014-05-14 18:01:13 -04:00
|
|
|
} else {
|
|
|
|
return me.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *MenuEntry) IsEqual(inme *MenuEntry) bool {
|
2015-03-18 01:16:54 -04:00
|
|
|
return me.hopefullyUniqueID() == inme.hopefullyUniqueID() && me.Parent == inme.Parent
|
2014-05-14 18:01:13 -04:00
|
|
|
}
|
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
func (me *MenuEntry) IsSameResource(inme *MenuEntry) bool {
|
2015-03-18 01:16:54 -04:00
|
|
|
return me.URL != "" && inme.URL != "" && me.URL == inme.URL
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2014-04-24 18:11:08 -04:00
|
|
|
func (me *MenuEntry) MarshallMap(ime map[string]interface{}) {
|
|
|
|
for k, v := range ime {
|
|
|
|
loki := strings.ToLower(k)
|
|
|
|
switch loki {
|
|
|
|
case "url":
|
2015-03-18 01:16:54 -04:00
|
|
|
me.URL = cast.ToString(v)
|
2014-04-24 18:11:08 -04:00
|
|
|
case "weight":
|
|
|
|
me.Weight = cast.ToInt(v)
|
|
|
|
case "name":
|
|
|
|
me.Name = cast.ToString(v)
|
2014-05-27 18:34:29 -04:00
|
|
|
case "pre":
|
|
|
|
me.Pre = template.HTML(cast.ToString(v))
|
|
|
|
case "post":
|
|
|
|
me.Post = template.HTML(cast.ToString(v))
|
2014-05-14 18:01:13 -04:00
|
|
|
case "identifier":
|
|
|
|
me.Identifier = cast.ToString(v)
|
2014-04-24 18:11:08 -04:00
|
|
|
case "parent":
|
|
|
|
me.Parent = cast.ToString(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-23 02:59:19 -04:00
|
|
|
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.
|
|
|
|
}
|
2014-09-09 05:59:47 -04:00
|
|
|
sort.Stable(ms)
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2015-03-07 06:52:29 -05:00
|
|
|
var defaultMenuEntrySort = func(m1, m2 *MenuEntry) bool {
|
2014-04-23 02:59:19 -04:00
|
|
|
if m1.Weight == m2.Weight {
|
2015-11-09 15:01:36 -05:00
|
|
|
if m1.Name == m2.Name {
|
|
|
|
return m1.Identifier < m2.Identifier
|
|
|
|
}
|
2014-04-23 02:59:19 -04:00
|
|
|
return m1.Name < m2.Name
|
|
|
|
}
|
2015-03-07 06:52:29 -05:00
|
|
|
return m1.Weight < m2.Weight
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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]) }
|
|
|
|
|
2016-02-07 06:34:43 -05:00
|
|
|
func (m Menu) Sort() Menu {
|
2015-03-07 06:52:29 -05:00
|
|
|
MenuEntryBy(defaultMenuEntrySort).Sort(m)
|
2016-02-07 06:34:43 -05:00
|
|
|
return m
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2015-03-07 06:52:29 -05:00
|
|
|
func (m Menu) Limit(n int) Menu {
|
2016-02-06 16:53:59 -05:00
|
|
|
if len(m) > n {
|
2015-03-07 06:52:29 -05:00
|
|
|
return m[0:n]
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
2015-03-07 06:52:29 -05:00
|
|
|
return m
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2015-03-07 06:52:29 -05:00
|
|
|
func (m Menu) ByWeight() Menu {
|
|
|
|
MenuEntryBy(defaultMenuEntrySort).Sort(m)
|
|
|
|
return m
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2015-03-07 06:52:29 -05:00
|
|
|
func (m Menu) ByName() Menu {
|
2014-04-23 02:59:19 -04:00
|
|
|
title := func(m1, m2 *MenuEntry) bool {
|
|
|
|
return m1.Name < m2.Name
|
|
|
|
}
|
|
|
|
|
2015-03-07 06:52:29 -05:00
|
|
|
MenuEntryBy(title).Sort(m)
|
|
|
|
return m
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|