1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 17:17:49 +01:00
gojekyll/site/read.go

113 lines
2.8 KiB
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package site
import (
"os"
"path/filepath"
2017-08-21 21:06:53 +02:00
"sort"
2017-07-28 00:07:42 +02:00
"strings"
2017-07-04 15:09:36 +02:00
"github.com/osteele/gojekyll/collection"
2017-07-01 01:37:31 +02:00
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/pages"
2017-07-09 01:57:41 +02:00
"github.com/osteele/gojekyll/plugins"
2017-07-09 22:17:20 +02:00
"github.com/osteele/gojekyll/utils"
)
2017-07-04 15:09:36 +02:00
// FromDirectory reads the configuration file, if it exists.
2017-07-25 15:48:49 +02:00
func FromDirectory(dir string, flags config.Flags) (*Site, error) {
2017-07-04 15:09:36 +02:00
s := New(flags)
2017-08-24 18:25:38 +02:00
if err := s.cfg.FromDirectory(dir); err != nil {
2017-07-01 01:37:31 +02:00
return nil, err
}
2017-08-24 18:25:38 +02:00
s.cfg.ApplyFlags(s.flags)
2017-07-01 01:37:31 +02:00
return s, nil
}
2017-07-15 15:44:22 +02:00
// Read loads the site data and files.
2017-07-09 01:57:41 +02:00
func (s *Site) Read() error {
2017-07-24 13:44:49 +02:00
s.Routes = make(map[string]pages.Document)
2017-08-24 18:25:38 +02:00
plugins.Install(s.cfg.Plugins, s)
2017-07-24 13:44:49 +02:00
if err := s.findTheme(); err != nil {
return err
}
2017-07-09 01:57:41 +02:00
if err := s.readDataFiles(); err != nil {
return err
}
2017-07-24 13:44:49 +02:00
if err := s.readThemeAssets(); err != nil {
return err
}
if err := s.readFiles(s.SourceDir(), s.SourceDir()); err != nil {
return err
}
if err := s.ReadCollections(); err != nil {
2017-06-29 17:00:59 +02:00
return err
}
2017-08-18 17:07:01 +02:00
if err := s.initializeRenderers(); err != nil {
2017-07-15 14:59:19 +02:00
return err
}
2017-07-09 04:47:50 +02:00
return s.runHooks(func(p plugins.Plugin) error { return p.PostRead(s) })
}
2017-07-04 15:09:36 +02:00
// readFiles scans the source directory and creates pages and collection.
2017-07-24 13:44:49 +02:00
func (s *Site) readFiles(dir, base string) error {
return filepath.Walk(dir, func(filename string, info os.FileInfo, err error) error {
if err != nil {
return err
}
2017-07-24 13:44:49 +02:00
rel := utils.MustRel(base, filename)
switch {
2017-07-24 13:44:49 +02:00
case info.IsDir() && s.Exclude(rel):
return filepath.SkipDir
2017-07-28 00:07:42 +02:00
case info.IsDir():
return nil
case s.Exclude(rel):
return nil
case strings.HasPrefix(rel, "_"):
return nil
}
2017-08-24 18:25:38 +02:00
defaultFrontmatter := s.cfg.GetFrontMatterDefaults("", rel)
2017-07-24 13:44:49 +02:00
d, err := pages.NewFile(s, filename, filepath.ToSlash(rel), defaultFrontmatter)
if err != nil {
return utils.WrapPathError(err, filename)
}
2017-07-24 13:44:49 +02:00
s.AddDocument(d, true)
2017-08-10 17:45:46 +02:00
if p, ok := d.(pages.Page); ok {
s.nonCollectionPages = append(s.nonCollectionPages, p)
}
return nil
2017-07-24 13:44:49 +02:00
})
}
2017-08-10 17:45:46 +02:00
// AddDocument adds a document to the site's fields.
2017-07-10 20:33:06 +02:00
// It ignores unpublished documents unless config.Unpublished is true.
func (s *Site) AddDocument(d pages.Document, output bool) {
2017-08-24 18:25:38 +02:00
if d.Published() || s.cfg.Unpublished {
2017-07-10 20:33:06 +02:00
s.docs = append(s.docs, d)
2017-06-23 21:27:13 +02:00
if output {
2017-07-10 20:33:06 +02:00
s.Routes[d.Permalink()] = d
2017-06-23 21:27:13 +02:00
}
}
}
// ReadCollections reads the pages of the collections named in the site configuration.
// It adds each collection's pages to the site map, and creates a template site variable for each collection.
2017-08-21 21:06:53 +02:00
func (s *Site) ReadCollections() (err error) {
var cols []*collection.Collection
2017-08-24 18:25:38 +02:00
for name, data := range s.cfg.Collections {
2017-07-04 15:09:36 +02:00
c := collection.New(s, name, data)
2017-08-21 21:06:53 +02:00
cols = append(cols, c)
err = c.ReadPages()
if err != nil {
break
}
for _, p := range c.Pages() {
2017-06-29 17:00:59 +02:00
s.AddDocument(p, c.Output())
}
}
2017-08-21 21:06:53 +02:00
sort.Slice(cols, func(i, j int) bool {
return cols[i].Name < cols[j].Name
})
s.Collections = cols
return nil
}