1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 21:07:48 +01:00
gojekyll/site/drop.go

87 lines
1.9 KiB
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package site
2017-07-03 16:39:55 +02:00
import (
"log"
2017-07-03 16:39:55 +02:00
"time"
2017-07-07 18:24:00 +02:00
"github.com/osteele/gojekyll/pages"
"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{} {
s.dropOnce.Do(func() {
if err := s.initializeDrop(); err != nil {
2017-08-03 15:52:41 +02:00
log.Fatalf("ToLiquid failed: %s\n", err)
}
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
}
func (s *Site) initializeDrop() error {
2017-09-03 18:18:17 +02:00
var docs []Page
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,
"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(),
"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 {
drop[c.Name] = c.Pages()
2017-07-03 16:39:55 +02:00
}
s.drop = drop
2017-07-03 16:39:55 +02:00
s.setPostVariables()
return s.runHooks(func(h plugins.Plugin) error {
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) {
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
}