Added support for indexes of indexes & ordered indexes

This commit is contained in:
spf13 2013-08-03 03:09:28 -04:00
parent ddad1e04ac
commit 52c089ffbd
2 changed files with 38 additions and 13 deletions

View file

@ -17,10 +17,15 @@ import (
"sort" "sort"
) )
type IndexCount struct {
Name string
Count int
}
type Index map[string]Pages type Index map[string]Pages
type IndexList map[string]Index type IndexList map[string]Index
type OrderedIndex []*Pages type OrderedIndex []IndexCount
type OrderedIndexList map[string]OrderedIndex type OrderedIndexList map[string]OrderedIndex
// KeyPrep... Indexes should be case insensitive. Can make it easily conditional later. // 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) i[key] = append(i[key], p)
} }
func (l IndexList) BuildOrderedIndexList() *OrderedIndexList { func (l IndexList) BuildOrderedIndexList() OrderedIndexList {
oil := make(OrderedIndexList, len(l)) oil := make(OrderedIndexList, len(l))
for idx_name, index := range l { for idx_name, index := range l {
i := 0 i := 0
oi := make(OrderedIndex, len(index)) oi := make(OrderedIndex, len(index))
for _, e := range index { for name, pages := range index {
oi[i] = &e oi[i] = IndexCount{name, len(pages)}
i++ i++
} }
oi.Sort() sort.Sort(oi)
oil[idx_name] = oi oil[idx_name] = oi
} }
return &oil return oil
} }
func (idx OrderedIndex) Len() int { return len(idx) } 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) 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) 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] }

View file

@ -45,7 +45,7 @@ type Site struct {
type SiteInfo struct { type SiteInfo struct {
BaseUrl template.URL BaseUrl template.URL
Indexes *OrderedIndexList Indexes OrderedIndexList
Recent *Pages Recent *Pages
LastChange time.Time LastChange time.Time
Title string Title string
@ -94,6 +94,7 @@ func (site *Site) Render() {
site.AbsUrlify() site.AbsUrlify()
site.timer.Step("absolute URLify") site.timer.Step("absolute URLify")
site.RenderIndexes() site.RenderIndexes()
site.RenderIndexesIndexes()
site.timer.Step("render and write indexes") site.timer.Step("render and write indexes")
site.RenderLists() site.RenderLists()
site.timer.Step("render and write lists") site.timer.Step("render and write lists")
@ -263,6 +264,7 @@ func (s *Site) BuildSiteMeta() (err error) {
} }
s.Info.Indexes = s.Indexes.BuildOrderedIndexList() s.Info.Indexes = s.Indexes.BuildOrderedIndexList()
if len(s.Pages) == 0 { if len(s.Pages) == 0 {
return errors.New(fmt.Sprintf("Unable to build site metadata, no pages found in directory %s", s.c.ContentDir)) 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() { func (s *Site) RenderLists() {
for section, data := range s.Sections { for section, data := range s.Sections {
n := s.NewNode() n := s.NewNode()