2017-07-04 09:09:36 -04:00
|
|
|
package collection
|
2017-07-01 18:43:22 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-03 09:48:41 -04:00
|
|
|
"sort"
|
2017-07-01 18:43:22 -04:00
|
|
|
|
|
|
|
"github.com/osteele/gojekyll/pages"
|
|
|
|
"github.com/osteele/gojekyll/templates"
|
2017-07-09 16:17:20 -04:00
|
|
|
"github.com/osteele/gojekyll/utils"
|
2017-07-01 18:43:22 -04:00
|
|
|
)
|
|
|
|
|
2017-07-03 11:48:06 -04:00
|
|
|
const draftsPath = "_drafts"
|
|
|
|
|
2017-07-10 13:54:52 -04:00
|
|
|
// ReadPages scans the file system for collection pages, and adds them to c.Pages.
|
|
|
|
func (c *Collection) ReadPages() error {
|
2017-08-10 11:03:24 -04:00
|
|
|
if c.IsPostsCollection() && c.cfg.Drafts {
|
2017-07-10 13:54:52 -04:00
|
|
|
if err := c.scanDirectory(draftsPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := c.scanDirectory(c.PathPrefix()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.IsPostsCollection() {
|
|
|
|
sort.Sort(pagesByDate{c.pages})
|
2017-08-10 10:51:29 -04:00
|
|
|
var prev pages.Page
|
|
|
|
for _, p := range c.pages {
|
|
|
|
p.FrontMatter()["previous"] = prev
|
|
|
|
if prev != nil {
|
|
|
|
prev.FrontMatter()["next"] = p
|
|
|
|
}
|
|
|
|
prev = p
|
|
|
|
}
|
|
|
|
if prev != nil {
|
|
|
|
prev.FrontMatter()["next"] = nil
|
|
|
|
}
|
2017-07-10 13:54:52 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// scanDirectory scans the file system for collection pages, and adds them to c.Pages.
|
|
|
|
//
|
|
|
|
// This function is distinct from ReadPages so that the posts collection can call it twice.
|
|
|
|
func (c *Collection) scanDirectory(dirname string) error {
|
2017-07-11 19:23:42 -04:00
|
|
|
var (
|
2017-08-10 11:03:24 -04:00
|
|
|
sitePath = c.cfg.Source
|
2017-07-11 19:23:42 -04:00
|
|
|
dir = filepath.Join(sitePath, dirname)
|
|
|
|
)
|
2017-07-01 18:43:22 -04:00
|
|
|
walkFn := func(filename string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2017-07-11 19:23:42 -04:00
|
|
|
siteRel := utils.MustRel(sitePath, filename)
|
2017-07-01 18:43:22 -04:00
|
|
|
switch {
|
|
|
|
case info.IsDir():
|
|
|
|
return nil
|
2017-07-11 19:23:42 -04:00
|
|
|
case c.site.Exclude(siteRel):
|
2017-07-10 13:54:52 -04:00
|
|
|
return nil
|
|
|
|
default:
|
2017-07-14 11:30:39 -04:00
|
|
|
return c.readPost(filename, utils.MustRel(dir, filename))
|
2017-07-01 18:43:22 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-11 19:23:42 -04:00
|
|
|
return filepath.Walk(dir, walkFn)
|
2017-07-04 08:29:11 -04:00
|
|
|
}
|
|
|
|
|
2017-07-14 11:30:39 -04:00
|
|
|
func (c *Collection) readPost(abs string, rel string) error {
|
2017-08-10 11:03:24 -04:00
|
|
|
siteRel := utils.MustRel(c.cfg.Source, abs)
|
2017-07-01 18:43:22 -04:00
|
|
|
strategy := c.strategy()
|
|
|
|
switch {
|
|
|
|
case !strategy.collectible(rel):
|
|
|
|
return nil
|
2017-08-10 11:03:24 -04:00
|
|
|
case strategy.future(rel) && !c.cfg.Future:
|
2017-07-01 18:43:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-07-11 19:23:42 -04:00
|
|
|
pageDefaults := map[string]interface{}{
|
|
|
|
"collection": c.Name,
|
|
|
|
"permalink": c.PermalinkPattern(),
|
|
|
|
}
|
2017-08-10 11:03:24 -04:00
|
|
|
fm := templates.MergeVariableMaps(pageDefaults, c.cfg.GetFrontMatterDefaults(c.Name, siteRel))
|
2017-07-11 19:23:42 -04:00
|
|
|
strategy.addDate(rel, fm)
|
2017-07-10 13:23:51 -04:00
|
|
|
f, err := pages.NewFile(c.site, abs, filepath.ToSlash(rel), fm)
|
2017-07-01 18:43:22 -04:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
2017-07-02 12:09:15 -04:00
|
|
|
case f.Static():
|
|
|
|
return nil
|
2017-08-10 11:03:24 -04:00
|
|
|
case f.Published() || c.cfg.Unpublished:
|
2017-07-14 11:30:39 -04:00
|
|
|
p := f.(pages.Page) // f.Static() guarantees this
|
2017-07-01 18:43:22 -04:00
|
|
|
c.pages = append(c.pages, p)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|