mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Added support for indexes of indexes & ordered indexes
This commit is contained in:
parent
ddad1e04ac
commit
52c089ffbd
2 changed files with 38 additions and 13 deletions
|
@ -17,10 +17,15 @@ import (
|
|||
"sort"
|
||||
)
|
||||
|
||||
type IndexCount struct {
|
||||
Name string
|
||||
Count int
|
||||
}
|
||||
|
||||
type Index map[string]Pages
|
||||
type IndexList map[string]Index
|
||||
|
||||
type OrderedIndex []*Pages
|
||||
type OrderedIndex []IndexCount
|
||||
type OrderedIndexList map[string]OrderedIndex
|
||||
|
||||
// KeyPrep... Indexes should be case insensitive. Can make it easily conditional later.
|
||||
|
@ -35,24 +40,21 @@ func (i Index) Add(key string, p *Page) {
|
|||
i[key] = append(i[key], p)
|
||||
}
|
||||
|
||||
func (l IndexList) BuildOrderedIndexList() *OrderedIndexList {
|
||||
func (l IndexList) BuildOrderedIndexList() OrderedIndexList {
|
||||
oil := make(OrderedIndexList, len(l))
|
||||
for idx_name, index := range l {
|
||||
i := 0
|
||||
oi := make(OrderedIndex, len(index))
|
||||
for _, e := range index {
|
||||
oi[i] = &e
|
||||
for name, pages := range index {
|
||||
oi[i] = IndexCount{name, len(pages)}
|
||||
i++
|
||||
}
|
||||
oi.Sort()
|
||||
sort.Sort(oi)
|
||||
oil[idx_name] = oi
|
||||
}
|
||||
return &oil
|
||||
return oil
|
||||
}
|
||||
|
||||
func (idx OrderedIndex) Len() int { return len(idx) }
|
||||
|
||||
func (idx OrderedIndex) Less(i, j int) bool { return len(*idx[i]) < len(*idx[j]) }
|
||||
func (idx OrderedIndex) Swap(i, j int) { idx[i], idx[j] = idx[j], idx[i] }
|
||||
func (idx OrderedIndex) Sort() { sort.Sort(idx) }
|
||||
func (idx OrderedIndex) Limit(n int) OrderedIndex { return idx[0:n] }
|
||||
func (idx OrderedIndex) Len() int { return len(idx) }
|
||||
func (idx OrderedIndex) Less(i, j int) bool { return idx[i].Count > idx[j].Count }
|
||||
func (idx OrderedIndex) Swap(i, j int) { idx[i], idx[j] = idx[j], idx[i] }
|
||||
|
|
|
@ -45,7 +45,7 @@ type Site struct {
|
|||
|
||||
type SiteInfo struct {
|
||||
BaseUrl template.URL
|
||||
Indexes *OrderedIndexList
|
||||
Indexes OrderedIndexList
|
||||
Recent *Pages
|
||||
LastChange time.Time
|
||||
Title string
|
||||
|
@ -94,6 +94,7 @@ func (site *Site) Render() {
|
|||
site.AbsUrlify()
|
||||
site.timer.Step("absolute URLify")
|
||||
site.RenderIndexes()
|
||||
site.RenderIndexesIndexes()
|
||||
site.timer.Step("render and write indexes")
|
||||
site.RenderLists()
|
||||
site.timer.Step("render and write lists")
|
||||
|
@ -263,6 +264,7 @@ func (s *Site) BuildSiteMeta() (err error) {
|
|||
}
|
||||
|
||||
s.Info.Indexes = s.Indexes.BuildOrderedIndexList()
|
||||
|
||||
if len(s.Pages) == 0 {
|
||||
return errors.New(fmt.Sprintf("Unable to build site metadata, no pages found in directory %s", s.c.ContentDir))
|
||||
}
|
||||
|
@ -352,6 +354,27 @@ func (s *Site) RenderIndexes() {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *Site) RenderIndexesIndexes() {
|
||||
layout := "indexes" + slash + "index.html"
|
||||
if s.Tmpl.Lookup(layout) != nil {
|
||||
for singular, plural := range s.c.Indexes {
|
||||
n := s.NewNode()
|
||||
n.Title = strings.Title(plural)
|
||||
url := Urlize(plural)
|
||||
n.Url = url + "/index.html"
|
||||
n.Permalink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(n.Url)))
|
||||
n.Data["Singular"] = singular
|
||||
n.Data["Plural"] = plural
|
||||
n.Data["Index"] = s.Indexes[plural]
|
||||
n.Data["OrderedIndex"] = s.Info.Indexes[plural]
|
||||
fmt.Println(s.Info.Indexes)
|
||||
|
||||
x := s.RenderThing(n, layout)
|
||||
s.WritePublic(plural+slash+"index.html", x.Bytes())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Site) RenderLists() {
|
||||
for section, data := range s.Sections {
|
||||
n := s.NewNode()
|
||||
|
|
Loading…
Reference in a new issue