2015-12-07 18:57:01 +00:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
2014-04-23 06:59:19 +00:00
|
|
|
//
|
2015-11-24 03:16:36 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-04-23 06:59:19 +00:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-24 03:16:36 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-04-23 06:59:19 +00: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 22:11:08 +00:00
|
|
|
import (
|
2014-05-27 22:34:29 +00:00
|
|
|
"html/template"
|
2014-04-24 22:11:08 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
)
|
2014-04-23 06:59:19 +00:00
|
|
|
|
2016-11-08 22:34:52 +00:00
|
|
|
// TODO(bep) np menu entries in section content etc.?
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// MenuEntry represents a menu item defined in either Page front matter
|
|
|
|
// or in the site config.
|
2014-04-23 06:59:19 +00:00
|
|
|
type MenuEntry struct {
|
2015-03-18 05:16:54 +00:00
|
|
|
URL string
|
2014-05-14 22:01:13 +00:00
|
|
|
Name string
|
|
|
|
Menu string
|
|
|
|
Identifier string
|
2014-05-27 22:34:29 +00:00
|
|
|
Pre template.HTML
|
|
|
|
Post template.HTML
|
2014-05-14 22:01:13 +00:00
|
|
|
Weight int
|
|
|
|
Parent string
|
|
|
|
Children Menu
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// Menu is a collection of menu entries.
|
2014-04-23 06:59:19 +00:00
|
|
|
type Menu []*MenuEntry
|
2016-03-22 23:29:39 +00:00
|
|
|
|
|
|
|
// Menus is a dictionary of menus.
|
2014-04-23 06:59:19 +00:00
|
|
|
type Menus map[string]*Menu
|
2016-03-22 23:29:39 +00:00
|
|
|
|
|
|
|
// PageMenus is a dictionary of menus defined in the Pages.
|
2014-04-23 06:59:19 +00:00
|
|
|
type PageMenus map[string]*MenuEntry
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// addChild adds a new child to this menu entry.
|
|
|
|
// The default sort order will then be applied.
|
|
|
|
func (m *MenuEntry) addChild(child *MenuEntry) {
|
|
|
|
m.Children = append(m.Children, child)
|
|
|
|
m.Children.Sort()
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// HasChildren returns whether this menu item has any children.
|
|
|
|
func (m *MenuEntry) HasChildren() bool {
|
|
|
|
return m.Children != nil
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// KeyName returns the key used to identify this menu entry.
|
|
|
|
func (m *MenuEntry) KeyName() string {
|
|
|
|
if m.Identifier != "" {
|
|
|
|
return m.Identifier
|
2014-05-14 22:01:13 +00:00
|
|
|
}
|
2016-03-22 23:29:39 +00:00
|
|
|
return m.Name
|
2014-05-14 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
func (m *MenuEntry) hopefullyUniqueID() string {
|
|
|
|
if m.Identifier != "" {
|
|
|
|
return m.Identifier
|
|
|
|
} else if m.URL != "" {
|
|
|
|
return m.URL
|
2014-05-14 22:01:13 +00:00
|
|
|
} else {
|
2016-03-22 23:29:39 +00:00
|
|
|
return m.Name
|
2014-05-14 22:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// IsEqual returns whether the two menu entries represents the same menu entry.
|
|
|
|
func (m *MenuEntry) IsEqual(inme *MenuEntry) bool {
|
|
|
|
return m.hopefullyUniqueID() == inme.hopefullyUniqueID() && m.Parent == inme.Parent
|
2014-05-14 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// IsSameResource returns whether the two menu entries points to the same
|
|
|
|
// resource (URL).
|
|
|
|
func (m *MenuEntry) IsSameResource(inme *MenuEntry) bool {
|
|
|
|
return m.URL != "" && inme.URL != "" && m.URL == inme.URL
|
2014-10-18 18:25:10 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
func (m *MenuEntry) marshallMap(ime map[string]interface{}) {
|
2014-04-24 22:11:08 +00:00
|
|
|
for k, v := range ime {
|
|
|
|
loki := strings.ToLower(k)
|
|
|
|
switch loki {
|
|
|
|
case "url":
|
2016-03-22 23:29:39 +00:00
|
|
|
m.URL = cast.ToString(v)
|
2014-04-24 22:11:08 +00:00
|
|
|
case "weight":
|
2016-03-22 23:29:39 +00:00
|
|
|
m.Weight = cast.ToInt(v)
|
2014-04-24 22:11:08 +00:00
|
|
|
case "name":
|
2016-03-22 23:29:39 +00:00
|
|
|
m.Name = cast.ToString(v)
|
2014-05-27 22:34:29 +00:00
|
|
|
case "pre":
|
2016-03-22 23:29:39 +00:00
|
|
|
m.Pre = template.HTML(cast.ToString(v))
|
2014-05-27 22:34:29 +00:00
|
|
|
case "post":
|
2016-03-22 23:29:39 +00:00
|
|
|
m.Post = template.HTML(cast.ToString(v))
|
2014-05-14 22:01:13 +00:00
|
|
|
case "identifier":
|
2016-03-22 23:29:39 +00:00
|
|
|
m.Identifier = cast.ToString(v)
|
2014-04-24 22:11:08 +00:00
|
|
|
case "parent":
|
2016-03-22 23:29:39 +00:00
|
|
|
m.Parent = cast.ToString(v)
|
2014-04-24 22:11:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
func (m Menu) add(me *MenuEntry) Menu {
|
2014-04-23 06:59:19 +00:00
|
|
|
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
|
2016-03-22 23:29:39 +00:00
|
|
|
type menuSorter struct {
|
2014-04-23 06:59:19 +00:00
|
|
|
menu Menu
|
2016-03-22 23:29:39 +00:00
|
|
|
by menuEntryBy
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Closure used in the Sort.Less method.
|
2016-03-22 23:29:39 +00:00
|
|
|
type menuEntryBy func(m1, m2 *MenuEntry) bool
|
2014-04-23 06:59:19 +00:00
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
func (by menuEntryBy) Sort(menu Menu) {
|
|
|
|
ms := &menuSorter{
|
2014-04-23 06:59:19 +00:00
|
|
|
menu: menu,
|
|
|
|
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
|
|
|
|
}
|
2014-09-09 09:59:47 +00:00
|
|
|
sort.Stable(ms)
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 11:52:29 +00:00
|
|
|
var defaultMenuEntrySort = func(m1, m2 *MenuEntry) bool {
|
2014-04-23 06:59:19 +00:00
|
|
|
if m1.Weight == m2.Weight {
|
2015-11-09 20:01:36 +00:00
|
|
|
if m1.Name == m2.Name {
|
|
|
|
return m1.Identifier < m2.Identifier
|
|
|
|
}
|
2014-04-23 06:59:19 +00:00
|
|
|
return m1.Name < m2.Name
|
|
|
|
}
|
2015-03-07 11:52:29 +00:00
|
|
|
return m1.Weight < m2.Weight
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00: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] }
|
2014-04-23 06:59:19 +00:00
|
|
|
|
|
|
|
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
|
2016-03-22 23:29:39 +00:00
|
|
|
func (ms *menuSorter) Less(i, j int) bool { return ms.by(ms.menu[i], ms.menu[j]) }
|
2014-04-23 06:59:19 +00:00
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// Sort sorts the menu by weight, name and then by identifier.
|
2016-02-07 11:34:43 +00:00
|
|
|
func (m Menu) Sort() Menu {
|
2016-03-22 23:29:39 +00:00
|
|
|
menuEntryBy(defaultMenuEntrySort).Sort(m)
|
2016-02-07 11:34:43 +00:00
|
|
|
return m
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// Limit limits the returned menu to n entries.
|
2015-03-07 11:52:29 +00:00
|
|
|
func (m Menu) Limit(n int) Menu {
|
2016-02-06 21:53:59 +00:00
|
|
|
if len(m) > n {
|
2015-03-07 11:52:29 +00:00
|
|
|
return m[0:n]
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
2015-03-07 11:52:29 +00:00
|
|
|
return m
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// ByWeight sorts the menu by the weight defined in the menu configuration.
|
2015-03-07 11:52:29 +00:00
|
|
|
func (m Menu) ByWeight() Menu {
|
2016-03-22 23:29:39 +00:00
|
|
|
menuEntryBy(defaultMenuEntrySort).Sort(m)
|
2015-03-07 11:52:29 +00:00
|
|
|
return m
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// ByName sorts the menu by the name defined in the menu configuration.
|
2015-03-07 11:52:29 +00:00
|
|
|
func (m Menu) ByName() Menu {
|
2014-04-23 06:59:19 +00:00
|
|
|
title := func(m1, m2 *MenuEntry) bool {
|
|
|
|
return m1.Name < m2.Name
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
menuEntryBy(title).Sort(m)
|
2015-03-07 11:52:29 +00:00
|
|
|
return m
|
2014-04-23 06:59:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:29:39 +00:00
|
|
|
// Reverse reverses the order of the menu entries.
|
2014-04-23 06:59:19 +00: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
|
|
|
|
}
|