2017-07-04 15:09:36 +02:00
|
|
|
package collection
|
2017-07-02 00:43:22 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-03 15:48:41 +02:00
|
|
|
"sort"
|
2017-07-02 00:43:22 +02:00
|
|
|
|
2017-08-20 21:05:07 +02:00
|
|
|
"github.com/osteele/gojekyll/frontmatter"
|
2017-07-02 00:43:22 +02:00
|
|
|
"github.com/osteele/gojekyll/pages"
|
2017-07-09 22:17:20 +02:00
|
|
|
"github.com/osteele/gojekyll/utils"
|
2017-07-02 00:43:22 +02:00
|
|
|
)
|
|
|
|
|
2017-07-10 19:54:52 +02:00
|
|
|
// ReadPages scans the file system for collection pages, and adds them to c.Pages.
|
|
|
|
func (c *Collection) ReadPages() error {
|
2017-08-10 17:03:24 +02:00
|
|
|
if c.IsPostsCollection() && c.cfg.Drafts {
|
2017-07-10 19:54:52 +02: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-20 18:12:06 +02:00
|
|
|
addPrevNext(c.pages)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addPrevNext(ps []pages.Page) {
|
|
|
|
const prevPageField = "previous"
|
|
|
|
const nextPageField = "next"
|
|
|
|
var prev pages.Page
|
|
|
|
for _, p := range ps {
|
|
|
|
p.FrontMatter()[prevPageField] = prev
|
2017-08-10 16:51:29 +02:00
|
|
|
if prev != nil {
|
2017-08-20 18:12:06 +02:00
|
|
|
prev.FrontMatter()[nextPageField] = p
|
2017-08-10 16:51:29 +02:00
|
|
|
}
|
2017-08-20 18:12:06 +02:00
|
|
|
prev = p
|
|
|
|
}
|
|
|
|
if prev != nil {
|
|
|
|
prev.FrontMatter()[nextPageField] = nil
|
2017-07-10 19:54:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-20 18:12:06 +02:00
|
|
|
sitePath := c.cfg.Source
|
|
|
|
dir := filepath.Join(sitePath, dirname)
|
|
|
|
return filepath.Walk(dir, func(filename string, info os.FileInfo, err error) error {
|
2017-07-02 00:43:22 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2017-07-12 01:23:42 +02:00
|
|
|
siteRel := utils.MustRel(sitePath, filename)
|
2017-07-02 00:43:22 +02:00
|
|
|
switch {
|
|
|
|
case info.IsDir():
|
|
|
|
return nil
|
2017-07-12 01:23:42 +02:00
|
|
|
case c.site.Exclude(siteRel):
|
2017-07-10 19:54:52 +02:00
|
|
|
return nil
|
|
|
|
default:
|
2017-07-14 17:30:39 +02:00
|
|
|
return c.readPost(filename, utils.MustRel(dir, filename))
|
2017-07-02 00:43:22 +02:00
|
|
|
}
|
2017-08-20 18:12:06 +02:00
|
|
|
})
|
2017-07-04 14:29:11 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 17:30:39 +02:00
|
|
|
func (c *Collection) readPost(abs string, rel string) error {
|
2017-08-10 17:03:24 +02:00
|
|
|
siteRel := utils.MustRel(c.cfg.Source, abs)
|
2017-07-02 00:43:22 +02:00
|
|
|
strategy := c.strategy()
|
|
|
|
switch {
|
2017-08-20 18:12:06 +02:00
|
|
|
case !strategy.isCollectible(rel):
|
2017-07-02 00:43:22 +02:00
|
|
|
return nil
|
2017-08-20 18:12:06 +02:00
|
|
|
case strategy.isFuture(rel) && !c.cfg.Future:
|
2017-07-02 00:43:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-20 21:05:07 +02:00
|
|
|
pageDefaults := frontmatter.FrontMatter{
|
2017-07-12 01:23:42 +02:00
|
|
|
"collection": c.Name,
|
|
|
|
"permalink": c.PermalinkPattern(),
|
|
|
|
}
|
2017-08-20 21:05:07 +02:00
|
|
|
fm := pageDefaults.Merged(c.cfg.GetFrontMatterDefaults(c.Name, siteRel))
|
2017-08-20 18:12:06 +02:00
|
|
|
strategy.parseFilename(rel, fm)
|
2017-07-10 19:23:51 +02:00
|
|
|
f, err := pages.NewFile(c.site, abs, filepath.ToSlash(rel), fm)
|
2017-07-02 00:43:22 +02:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
2017-07-02 18:09:15 +02:00
|
|
|
case f.Static():
|
|
|
|
return nil
|
2017-08-10 17:03:24 +02:00
|
|
|
case f.Published() || c.cfg.Unpublished:
|
2017-07-14 17:30:39 +02:00
|
|
|
p := f.(pages.Page) // f.Static() guarantees this
|
2017-07-02 00:43:22 +02:00
|
|
|
c.pages = append(c.pages, p)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|