2017-07-04 15:09:36 +02:00
|
|
|
package site
|
2017-07-03 16:39:55 +02:00
|
|
|
|
|
|
|
import (
|
2017-07-26 16:02:12 +02:00
|
|
|
"log"
|
2017-07-03 16:39:55 +02:00
|
|
|
"time"
|
|
|
|
|
2017-07-07 18:24:00 +02:00
|
|
|
"github.com/osteele/gojekyll/pages"
|
2017-07-24 19:27:05 +02:00
|
|
|
"github.com/osteele/gojekyll/plugins"
|
2017-07-03 16:39:55 +02:00
|
|
|
"github.com/osteele/gojekyll/templates"
|
2017-08-21 21:06:53 +02:00
|
|
|
"github.com/osteele/liquid"
|
2017-07-03 16:39:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ToLiquid returns the site variable for template evaluation.
|
|
|
|
func (s *Site) ToLiquid() interface{} {
|
2017-08-10 17:18:36 +02:00
|
|
|
s.dropOnce.Do(func() {
|
2017-07-26 16:02:12 +02:00
|
|
|
if err := s.initializeDrop(); err != nil {
|
2017-08-03 15:52:41 +02:00
|
|
|
log.Fatalf("ToLiquid failed: %s\n", err)
|
2017-07-26 16:02:12 +02:00
|
|
|
}
|
2017-08-03 15:52:41 +02:00
|
|
|
})
|
2017-08-21 21:06:53 +02:00
|
|
|
return liquid.IterationKeyedMap(s.drop)
|
2017-07-03 16:39:55 +02:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:02:12 +02:00
|
|
|
func (s *Site) initializeDrop() error {
|
2017-09-03 18:18:17 +02:00
|
|
|
var docs []Page
|
2017-08-20 20:53:05 +02:00
|
|
|
for _, c := range s.Collections {
|
|
|
|
docs = append(docs, c.Pages()...)
|
|
|
|
}
|
2017-08-24 18:25:38 +02:00
|
|
|
drop := templates.MergeVariableMaps(s.cfg.Variables(), map[string]interface{}{
|
2017-08-21 21:06:53 +02:00
|
|
|
"collections": s.collectionDrops(),
|
2017-07-07 21:48:31 +02:00
|
|
|
"data": s.data,
|
2017-08-20 20:53:05 +02:00
|
|
|
"documents": docs,
|
2017-07-09 20:05:46 +02:00
|
|
|
"html_files": s.htmlFiles(),
|
2017-07-07 18:24:00 +02:00
|
|
|
"html_pages": s.htmlPages(),
|
2017-08-20 20:53:05 +02:00
|
|
|
"pages": s.nonCollectionPages,
|
2017-07-09 20:05:46 +02:00
|
|
|
"static_files": s.staticFiles(),
|
2017-07-07 21:48:31 +02:00
|
|
|
// TODO read time from _config, if it's available
|
|
|
|
"time": time.Now(),
|
2017-07-03 16:39:55 +02:00
|
|
|
})
|
|
|
|
for _, c := range s.Collections {
|
2017-07-24 19:27:05 +02:00
|
|
|
drop[c.Name] = c.Pages()
|
2017-07-03 16:39:55 +02:00
|
|
|
}
|
2017-07-24 19:27:05 +02:00
|
|
|
s.drop = drop
|
2017-07-03 16:39:55 +02:00
|
|
|
s.setPostVariables()
|
2017-07-26 16:02:12 +02:00
|
|
|
return s.runHooks(func(h plugins.Plugin) error {
|
2017-07-24 19:27:05 +02:00
|
|
|
return h.ModifySiteDrop(s, drop)
|
|
|
|
})
|
2017-07-03 16:39:55 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 21:06:53 +02:00
|
|
|
// The following functions are only used in the drop.
|
2017-07-09 20:05:46 +02:00
|
|
|
//
|
|
|
|
// Since the drop is cached, there's no effort to cache these too.
|
|
|
|
|
2017-08-21 21:06:53 +02:00
|
|
|
func (s *Site) collectionDrops() []interface{} {
|
|
|
|
var drops []interface{}
|
|
|
|
for _, c := range s.Collections {
|
|
|
|
drops = append(drops, c.ToLiquid())
|
|
|
|
}
|
|
|
|
return drops
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) htmlFiles() (result []*pages.StaticFile) {
|
2017-07-09 20:05:46 +02:00
|
|
|
for _, p := range s.staticFiles() {
|
|
|
|
if p.OutputExt() == ".html" {
|
2017-08-21 21:06:53 +02:00
|
|
|
result = append(result, p)
|
2017-07-09 20:05:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-03 18:18:17 +02:00
|
|
|
func (s *Site) htmlPages() (result []Page) {
|
2017-08-20 20:53:05 +02:00
|
|
|
for _, p := range s.nonCollectionPages {
|
2017-07-07 18:24:00 +02:00
|
|
|
if p.OutputExt() == ".html" {
|
2017-08-21 21:06:53 +02:00
|
|
|
result = append(result, p)
|
2017-07-07 18:24:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-07-09 20:05:46 +02:00
|
|
|
|
2017-08-21 21:06:53 +02:00
|
|
|
func (s *Site) staticFiles() (result []*pages.StaticFile) {
|
2017-07-09 20:05:46 +02:00
|
|
|
for _, d := range s.docs {
|
|
|
|
if sd, ok := d.(*pages.StaticFile); ok {
|
2017-08-21 21:06:53 +02:00
|
|
|
result = append(result, sd)
|
2017-07-09 20:05:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|