1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 18:37:50 +01:00
gojekyll/collection/read.go

89 lines
2.2 KiB
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package collection
import (
"fmt"
"os"
"path/filepath"
2017-07-03 15:48:41 +02:00
"sort"
"strings"
2017-07-02 03:12:22 +02:00
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/templates"
)
2017-07-03 17:48:06 +02:00
const draftsPath = "_drafts"
2017-07-04 14:29:11 +02:00
// ScanDirectory scans the file system for collection pages, and adds them to c.Pages.
func (c *Collection) ScanDirectory(dirname string) error {
2017-07-03 19:03:45 +02:00
sitePath := c.config.Source
pageDefaults := map[string]interface{}{
"collection": c.Name,
"permalink": c.PermalinkPattern(),
}
walkFn := func(filename string, info os.FileInfo, err error) error {
if err != nil {
// if the issue is simply that the directory doesn't exist, warn instead of error
if os.IsNotExist(err) {
if !c.IsPostsCollection() {
fmt.Printf("Missing collection directory: _%s\n", c.Name)
}
return nil
}
return err
}
2017-07-02 03:12:22 +02:00
relname := helpers.MustRel(sitePath, filename)
switch {
case strings.HasPrefix(filepath.Base(relname), "."):
return nil
case err != nil:
return err
case info.IsDir():
return nil
}
2017-07-03 19:03:45 +02:00
fm := templates.MergeVariableMaps(pageDefaults, c.config.GetFrontMatterDefaults(c.Name, relname))
return c.readFile(filename, relname, fm)
}
2017-07-04 14:29:11 +02:00
return filepath.Walk(filepath.Join(sitePath, dirname), walkFn)
}
// ReadPages scans the file system for collection pages, and adds them to c.Pages.
func (c *Collection) ReadPages() error {
2017-07-03 19:03:45 +02:00
if c.IsPostsCollection() && c.config.Drafts {
2017-07-04 14:29:11 +02:00
if err := c.ScanDirectory(draftsPath); err != nil {
return err
}
2017-07-03 19:03:45 +02:00
}
2017-07-04 14:29:11 +02:00
if err := c.ScanDirectory(c.PathPrefix()); err != nil {
2017-07-03 19:03:45 +02:00
return err
}
if c.IsPostsCollection() {
2017-07-03 15:48:41 +02:00
sort.Sort(pagesByDate{c.pages})
}
2017-07-03 19:03:45 +02:00
return nil
}
// readFile mutates fm.
func (c *Collection) readFile(abs string, rel string, fm map[string]interface{}) error {
strategy := c.strategy()
switch {
case !strategy.collectible(rel):
return nil
2017-07-03 19:03:45 +02:00
case strategy.future(rel) && !c.config.Future:
return nil
default:
strategy.addDate(rel, fm)
}
2017-07-02 18:09:15 +02:00
f, err := pages.NewFile(abs, c, filepath.ToSlash(rel), fm)
switch {
case err != nil:
return err
2017-07-02 18:09:15 +02:00
case f.Static():
return nil
2017-07-03 19:03:45 +02:00
case f.Published() || c.config.Unpublished:
2017-07-02 19:46:05 +02:00
p := f.(pages.Page)
c.pages = append(c.pages, p)
}
return nil
}