1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 09:44:41 +01:00

Reduce cyclo of collection.ReadPages

But maybe at too much expense in other complexity; pondering…
This commit is contained in:
Oliver Steele 2017-07-01 18:43:22 -04:00
parent 8041fa9116
commit d53a3acea4
3 changed files with 123 additions and 68 deletions

View File

@ -1,11 +1,7 @@
package collections
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/constants"
@ -92,67 +88,3 @@ func (c *Collection) PermalinkPattern() string {
}
return templates.VariableMap(c.Metadata).String("permalink", defaultPattern)
}
// ReadPages scans the file system for collection pages, and adds them to c.Pages.
func (c *Collection) ReadPages(sitePath string, frontMatterDefaults func(string, string) map[string]interface{}) error {
buildTime := time.Now()
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
}
relname, err := filepath.Rel(sitePath, filename)
switch {
case strings.HasPrefix(filepath.Base(relname), "."):
return nil
case err != nil:
return err
case info.IsDir():
return nil
}
defaultFrontMatter := templates.MergeVariableMaps(pageDefaults, frontMatterDefaults(relname, c.Name))
if c.IsPostsCollection() {
t, ok := DateFromFilename(relname)
if !ok {
return nil
}
if t.After(buildTime) && !c.Config().Future {
return nil
}
defaultFrontMatter["date"] = t
}
p, err := pages.NewFile(filename, c, filepath.ToSlash(relname), defaultFrontMatter)
switch {
case err != nil:
return err
case p.Published() || c.Config().Unpublished:
c.pages = append(c.pages, p)
}
return nil
}
if c.IsPostsCollection() && c.Config().Drafts {
if err := filepath.Walk(filepath.Join(sitePath, "_drafts"), walkFn); err != nil {
return err
}
}
return filepath.Walk(filepath.Join(sitePath, c.PathPrefix()), walkFn)
}
// DateFromFilename returns the date for a filename that uses Jekyll post convention.
// It also returns a bool indicating whether a date was found.
func DateFromFilename(s string) (time.Time, bool) {
layout := "2006-01-02-"
t, err := time.Parse(layout, filepath.Base(s + layout)[:len(layout)])
return t, err == nil
}

69
collections/read.go Normal file
View File

@ -0,0 +1,69 @@
package collections
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/templates"
)
// ReadPages scans the file system for collection pages, and adds them to c.Pages.
func (c *Collection) ReadPages(sitePath string, frontMatterDefaults func(string, string) map[string]interface{}) error {
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
}
relname, err := filepath.Rel(sitePath, filename)
switch {
case strings.HasPrefix(filepath.Base(relname), "."):
return nil
case err != nil:
return err
case info.IsDir():
return nil
}
fm := templates.MergeVariableMaps(pageDefaults, frontMatterDefaults(relname, c.Name))
return c.readFile(filename, relname, fm)
}
if c.IsPostsCollection() && c.Config().Drafts {
if err := filepath.Walk(filepath.Join(sitePath, "_drafts"), walkFn); err != nil {
return err
}
}
return filepath.Walk(filepath.Join(sitePath, c.PathPrefix()), walkFn)
}
// 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
case strategy.future(rel) && !c.Config().Future:
return nil
default:
strategy.addDate(rel, fm)
}
p, err := pages.NewFile(abs, c, filepath.ToSlash(rel), fm)
switch {
case err != nil:
return err
case p.Published() || c.Config().Unpublished:
c.pages = append(c.pages, p)
}
return nil
}

54
collections/strategies.go Normal file
View File

@ -0,0 +1,54 @@
package collections
import (
"path/filepath"
"time"
)
// A collectionStrategy encapsulates behavior differences between the _post
// collection and other collections.
type collectionStrategy interface {
addDate(filename string, fm map[string]interface{})
collectible(filename string) bool
future(filename string) bool
}
func (c *Collection) strategy() collectionStrategy {
if c.IsPostsCollection() {
return postsStrategy{}
}
return defaultStrategy{}
}
type defaultStrategy struct{}
func (s defaultStrategy) addDate(_ string, _ map[string]interface{}) {}
func (s defaultStrategy) collectible(filename string) bool { return true }
func (s defaultStrategy) future(filename string) bool { return false }
type postsStrategy struct{}
func (s postsStrategy) addDate(filename string, fm map[string]interface{}) {
if t, found := DateFromFilename(filename); found {
fm["date"] = t
}
}
func (s postsStrategy) collectible(filename string) bool {
_, ok := DateFromFilename(filename)
return ok
}
func (s postsStrategy) future(filename string) bool {
t, ok := DateFromFilename(filename)
return false
return ok && t.After(time.Now())
}
// DateFromFilename returns the date for a filename that uses Jekyll post convention.
// It also returns a bool indicating whether a date was found.
func DateFromFilename(s string) (time.Time, bool) {
layout := "2006-01-02-"
t, err := time.Parse(layout, filepath.Base(s + layout)[:len(layout)])
return t, err == nil
}