github.com/rezahousseini/hugo@v0.32.3/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  // addChild adds a new child to this menu entry.
    50  // The default sort order will then be applied.
    51  func (m *MenuEntry) addChild(child *MenuEntry) {
    52  	m.Children = append(m.Children, child)
    53  	m.Children.Sort()
    54  }
    55  
    56  // HasChildren returns whether this menu item has any children.
    57  func (m *MenuEntry) HasChildren() bool {
    58  	return m.Children != nil
    59  }
    60  
    61  // KeyName returns the key used to identify this menu entry.
    62  func (m *MenuEntry) KeyName() string {
    63  	if m.Identifier != "" {
    64  		return m.Identifier
    65  	}
    66  	return m.Name
    67  }
    68  
    69  func (m *MenuEntry) hopefullyUniqueID() string {
    70  	if m.Identifier != "" {
    71  		return m.Identifier
    72  	} else if m.URL != "" {
    73  		return m.URL
    74  	} else {
    75  		return m.Name
    76  	}
    77  }
    78  
    79  // IsEqual returns whether the two menu entries represents the same menu entry.
    80  func (m *MenuEntry) IsEqual(inme *MenuEntry) bool {
    81  	return m.hopefullyUniqueID() == inme.hopefullyUniqueID() && m.Parent == inme.Parent
    82  }
    83  
    84  // IsSameResource returns whether the two menu entries points to the same
    85  // resource (URL).
    86  func (m *MenuEntry) IsSameResource(inme *MenuEntry) bool {
    87  	return m.URL != "" && inme.URL != "" && m.URL == inme.URL
    88  }
    89  
    90  func (m *MenuEntry) marshallMap(ime map[string]interface{}) {
    91  	for k, v := range ime {
    92  		loki := strings.ToLower(k)
    93  		switch loki {
    94  		case "url":
    95  			m.URL = cast.ToString(v)
    96  		case "weight":
    97  			m.Weight = cast.ToInt(v)
    98  		case "name":
    99  			m.Name = cast.ToString(v)
   100  		case "title":
   101  			m.title = cast.ToString(v)
   102  		case "pre":
   103  			m.Pre = template.HTML(cast.ToString(v))
   104  		case "post":
   105  			m.Post = template.HTML(cast.ToString(v))
   106  		case "identifier":
   107  			m.Identifier = cast.ToString(v)
   108  		case "parent":
   109  			m.Parent = cast.ToString(v)
   110  		}
   111  	}
   112  }
   113  
   114  func (m Menu) add(me *MenuEntry) Menu {
   115  	app := func(slice Menu, x ...*MenuEntry) Menu {
   116  		n := len(slice) + len(x)
   117  		if n > cap(slice) {
   118  			size := cap(slice) * 2
   119  			if size < n {
   120  				size = n
   121  			}
   122  			new := make(Menu, size)
   123  			copy(new, slice)
   124  			slice = new
   125  		}
   126  		slice = slice[0:n]
   127  		copy(slice[n-len(x):], x)
   128  		return slice
   129  	}
   130  
   131  	m = app(m, me)
   132  	m.Sort()
   133  	return m
   134  }
   135  
   136  /*
   137   * Implementation of a custom sorter for Menu
   138   */
   139  
   140  // A type to implement the sort interface for Menu
   141  type menuSorter struct {
   142  	menu Menu
   143  	by   menuEntryBy
   144  }
   145  
   146  // Closure used in the Sort.Less method.
   147  type menuEntryBy func(m1, m2 *MenuEntry) bool
   148  
   149  func (by menuEntryBy) Sort(menu Menu) {
   150  	ms := &menuSorter{
   151  		menu: menu,
   152  		by:   by, // The Sort method's receiver is the function (closure) that defines the sort order.
   153  	}
   154  	sort.Stable(ms)
   155  }
   156  
   157  var defaultMenuEntrySort = func(m1, m2 *MenuEntry) bool {
   158  	if m1.Weight == m2.Weight {
   159  		if m1.Name == m2.Name {
   160  			return m1.Identifier < m2.Identifier
   161  		}
   162  		return m1.Name < m2.Name
   163  	}
   164  
   165  	if m2.Weight == 0 {
   166  		return true
   167  	}
   168  
   169  	if m1.Weight == 0 {
   170  		return false
   171  	}
   172  
   173  	return m1.Weight < m2.Weight
   174  }
   175  
   176  func (ms *menuSorter) Len() int      { return len(ms.menu) }
   177  func (ms *menuSorter) Swap(i, j int) { ms.menu[i], ms.menu[j] = ms.menu[j], ms.menu[i] }
   178  
   179  // Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
   180  func (ms *menuSorter) Less(i, j int) bool { return ms.by(ms.menu[i], ms.menu[j]) }
   181  
   182  // Sort sorts the menu by weight, name and then by identifier.
   183  func (m Menu) Sort() Menu {
   184  	menuEntryBy(defaultMenuEntrySort).Sort(m)
   185  	return m
   186  }
   187  
   188  // Limit limits the returned menu to n entries.
   189  func (m Menu) Limit(n int) Menu {
   190  	if len(m) > n {
   191  		return m[0:n]
   192  	}
   193  	return m
   194  }
   195  
   196  // ByWeight sorts the menu by the weight defined in the menu configuration.
   197  func (m Menu) ByWeight() Menu {
   198  	menuEntryBy(defaultMenuEntrySort).Sort(m)
   199  	return m
   200  }
   201  
   202  // ByName sorts the menu by the name defined in the menu configuration.
   203  func (m Menu) ByName() Menu {
   204  	title := func(m1, m2 *MenuEntry) bool {
   205  		return m1.Name < m2.Name
   206  	}
   207  
   208  	menuEntryBy(title).Sort(m)
   209  	return m
   210  }
   211  
   212  // Reverse reverses the order of the menu entries.
   213  func (m Menu) Reverse() Menu {
   214  	for i, j := 0, len(m)-1; i < j; i, j = i+1, j-1 {
   215  		m[i], m[j] = m[j], m[i]
   216  	}
   217  
   218  	return m
   219  }
   220  
   221  func (m *MenuEntry) Title() string {
   222  	if m.title != "" {
   223  		return m.title
   224  	}
   225  
   226  	if m.Page != nil {
   227  		return m.Page.LinkTitle()
   228  	}
   229  
   230  	return ""
   231  }