github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/hugolib/menu.go (about)

     1  // Copyright 2015 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugolib
    15  
    16  import (
    17  	"html/template"
    18  	"sort"
    19  	"strings"
    20  
    21  	"github.com/spf13/cast"
    22  )
    23  
    24  // MenuEntry represents a menu item defined in either Page front matter
    25  // or in the site config.
    26  type MenuEntry struct {
    27  	URL        string
    28  	Page       *Page
    29  	Name       string
    30  	Menu       string
    31  	Identifier string
    32  	title      string
    33  	Pre        template.HTML
    34  	Post       template.HTML
    35  	Weight     int
    36  	Parent     string
    37  	Children   Menu
    38  }
    39  
    40  // Menu is a collection of menu entries.
    41  type Menu []*MenuEntry
    42  
    43  // Menus is a dictionary of menus.
    44  type Menus map[string]*Menu
    45  
    46  // PageMenus is a dictionary of menus defined in the Pages.
    47  type PageMenus map[string]*MenuEntry
    48  
    49  // HasChildren returns whether this menu item has any children.
    50  func (m *MenuEntry) HasChildren() bool {
    51  	return m.Children != nil
    52  }
    53  
    54  // KeyName returns the key used to identify this menu entry.
    55  func (m *MenuEntry) KeyName() string {
    56  	if m.Identifier != "" {
    57  		return m.Identifier
    58  	}
    59  	return m.Name
    60  }
    61  
    62  func (m *MenuEntry) hopefullyUniqueID() string {
    63  	if m.Identifier != "" {
    64  		return m.Identifier
    65  	} else if m.URL != "" {
    66  		return m.URL
    67  	} else {
    68  		return m.Name
    69  	}
    70  }
    71  
    72  // IsEqual returns whether the two menu entries represents the same menu entry.
    73  func (m *MenuEntry) IsEqual(inme *MenuEntry) bool {
    74  	return m.hopefullyUniqueID() == inme.hopefullyUniqueID() && m.Parent == inme.Parent
    75  }
    76  
    77  // IsSameResource returns whether the two menu entries points to the same
    78  // resource (URL).
    79  func (m *MenuEntry) IsSameResource(inme *MenuEntry) bool {
    80  	return m.URL != "" && inme.URL != "" && m.URL == inme.URL
    81  }
    82  
    83  func (m *MenuEntry) marshallMap(ime map[string]interface{}) {
    84  	for k, v := range ime {
    85  		loki := strings.ToLower(k)
    86  		switch loki {
    87  		case "url":
    88  			m.URL = cast.ToString(v)
    89  		case "weight":
    90  			m.Weight = cast.ToInt(v)
    91  		case "name":
    92  			m.Name = cast.ToString(v)
    93  		case "title":
    94  			m.title = cast.ToString(v)
    95  		case "pre":
    96  			m.Pre = template.HTML(cast.ToString(v))
    97  		case "post":
    98  			m.Post = template.HTML(cast.ToString(v))
    99  		case "identifier":
   100  			m.Identifier = cast.ToString(v)
   101  		case "parent":
   102  			m.Parent = cast.ToString(v)
   103  		}
   104  	}
   105  }
   106  
   107  func (m Menu) add(me *MenuEntry) Menu {
   108  	app := func(slice Menu, x ...*MenuEntry) Menu {
   109  		n := len(slice) + len(x)
   110  		if n > cap(slice) {
   111  			size := cap(slice) * 2
   112  			if size < n {
   113  				size = n
   114  			}
   115  			new := make(Menu, size)
   116  			copy(new, slice)
   117  			slice = new
   118  		}
   119  		slice = slice[0:n]
   120  		copy(slice[n-len(x):], x)
   121  		return slice
   122  	}
   123  
   124  	m = app(m, me)
   125  	m.Sort()
   126  	return m
   127  }
   128  
   129  /*
   130   * Implementation of a custom sorter for Menu
   131   */
   132  
   133  // A type to implement the sort interface for Menu
   134  type menuSorter struct {
   135  	menu Menu
   136  	by   menuEntryBy
   137  }
   138  
   139  // Closure used in the Sort.Less method.
   140  type menuEntryBy func(m1, m2 *MenuEntry) bool
   141  
   142  func (by menuEntryBy) Sort(menu Menu) {
   143  	ms := &menuSorter{
   144  		menu: menu,
   145  		by:   by, // The Sort method's receiver is the function (closure) that defines the sort order.
   146  	}
   147  	sort.Stable(ms)
   148  }
   149  
   150  var defaultMenuEntrySort = func(m1, m2 *MenuEntry) bool {
   151  	if m1.Weight == m2.Weight {
   152  		if m1.Name == m2.Name {
   153  			return m1.Identifier < m2.Identifier
   154  		}
   155  		return m1.Name < m2.Name
   156  	}
   157  
   158  	if m2.Weight == 0 {
   159  		return true
   160  	}
   161  
   162  	if m1.Weight == 0 {
   163  		return false
   164  	}
   165  
   166  	return m1.Weight < m2.Weight
   167  }
   168  
   169  func (ms *menuSorter) Len() int      { return len(ms.menu) }
   170  func (ms *menuSorter) Swap(i, j int) { ms.menu[i], ms.menu[j] = ms.menu[j], ms.menu[i] }
   171  
   172  // Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
   173  func (ms *menuSorter) Less(i, j int) bool { return ms.by(ms.menu[i], ms.menu[j]) }
   174  
   175  // Sort sorts the menu by weight, name and then by identifier.
   176  func (m Menu) Sort() Menu {
   177  	menuEntryBy(defaultMenuEntrySort).Sort(m)
   178  	return m
   179  }
   180  
   181  // Limit limits the returned menu to n entries.
   182  func (m Menu) Limit(n int) Menu {
   183  	if len(m) > n {
   184  		return m[0:n]
   185  	}
   186  	return m
   187  }
   188  
   189  // ByWeight sorts the menu by the weight defined in the menu configuration.
   190  func (m Menu) ByWeight() Menu {
   191  	menuEntryBy(defaultMenuEntrySort).Sort(m)
   192  	return m
   193  }
   194  
   195  // ByName sorts the menu by the name defined in the menu configuration.
   196  func (m Menu) ByName() Menu {
   197  	title := func(m1, m2 *MenuEntry) bool {
   198  		return m1.Name < m2.Name
   199  	}
   200  
   201  	menuEntryBy(title).Sort(m)
   202  	return m
   203  }
   204  
   205  // Reverse reverses the order of the menu entries.
   206  func (m Menu) Reverse() Menu {
   207  	for i, j := 0, len(m)-1; i < j; i, j = i+1, j-1 {
   208  		m[i], m[j] = m[j], m[i]
   209  	}
   210  
   211  	return m
   212  }
   213  
   214  func (m *MenuEntry) Title() string {
   215  	if m.title != "" {
   216  		return m.title
   217  	}
   218  
   219  	if m.Page != nil {
   220  		return m.Page.LinkTitle()
   221  	}
   222  
   223  	return ""
   224  }