2017-07-04 15:09:36 +02:00
|
|
|
package site
|
2017-07-03 16:39:55 +02:00
|
|
|
|
|
|
|
import (
|
2017-07-04 15:09:36 +02:00
|
|
|
"github.com/osteele/gojekyll/collection"
|
2017-07-03 16:39:55 +02:00
|
|
|
)
|
|
|
|
|
2017-07-04 15:09:36 +02:00
|
|
|
func (s *Site) findPostCollection() *collection.Collection {
|
2017-07-03 16:39:55 +02:00
|
|
|
for _, c := range s.Collections {
|
|
|
|
if c.Name == "posts" {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) setPostVariables() {
|
|
|
|
c := s.findPostCollection()
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var (
|
2017-07-09 20:05:46 +02:00
|
|
|
ps = c.Pages()
|
|
|
|
related = ps
|
2017-07-03 16:39:55 +02:00
|
|
|
)
|
|
|
|
if len(related) > 10 {
|
|
|
|
related = related[:10]
|
|
|
|
}
|
2017-09-03 18:18:17 +02:00
|
|
|
s.drop["categories"] = s.groupPagesBy(func(p Page) []string { return p.Categories() })
|
|
|
|
s.drop["tags"] = s.groupPagesBy(func(p Page) []string { return p.Tags() })
|
2017-07-09 20:05:46 +02:00
|
|
|
s.drop["related_posts"] = related
|
|
|
|
}
|
|
|
|
|
2017-09-03 18:18:17 +02:00
|
|
|
func (s *Site) groupPagesBy(getter func(Page) []string) map[string][]Page {
|
|
|
|
categories := map[string][]Page{}
|
2017-07-09 20:05:46 +02:00
|
|
|
for _, p := range s.Pages() {
|
2017-07-03 16:39:55 +02:00
|
|
|
for _, k := range p.Categories() {
|
|
|
|
ps, found := categories[k]
|
|
|
|
if !found {
|
2017-09-03 18:18:17 +02:00
|
|
|
ps = []Page{}
|
2017-07-03 16:39:55 +02:00
|
|
|
}
|
|
|
|
categories[k] = append(ps, p)
|
|
|
|
}
|
|
|
|
}
|
2017-07-09 20:05:46 +02:00
|
|
|
return categories
|
2017-07-03 16:39:55 +02:00
|
|
|
}
|