2017-07-04 15:09:36 +02:00
|
|
|
package site
|
2017-06-23 15:32:08 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-28 00:07:42 +02:00
|
|
|
"strings"
|
2017-06-23 15:32:08 +02:00
|
|
|
|
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"
|
2017-06-23 15:32:08 +02:00
|
|
|
"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-06-23 15:32:08 +02:00
|
|
|
)
|
|
|
|
|
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-07-25 15:48:49 +02:00
|
|
|
if err := s.config.FromDirectory(dir); err != nil {
|
2017-07-01 01:37:31 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-01 21:03:36 +02:00
|
|
|
s.config.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-07-09 04:47:50 +02:00
|
|
|
plugins.Install(s.config.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-06-23 15:32:08 +02:00
|
|
|
}
|
2017-07-15 14:59:19 +02:00
|
|
|
if err := s.initializeRenderingPipeline(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-09 04:47:50 +02:00
|
|
|
return s.runHooks(func(p plugins.Plugin) error { return p.PostRead(s) })
|
2017-06-23 15:32:08 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-06-23 15:32:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-24 13:44:49 +02:00
|
|
|
rel := utils.MustRel(base, filename)
|
2017-06-23 15:32:08 +02:00
|
|
|
switch {
|
2017-07-24 13:44:49 +02:00
|
|
|
case info.IsDir() && s.Exclude(rel):
|
2017-06-23 15:32:08 +02:00
|
|
|
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, "_"):
|
2017-06-23 15:32:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-07-24 13:44:49 +02:00
|
|
|
defaultFrontmatter := s.config.GetFrontMatterDefaults("", rel)
|
|
|
|
d, err := pages.NewFile(s, filename, filepath.ToSlash(rel), defaultFrontmatter)
|
2017-06-23 15:32:08 +02:00
|
|
|
if err != nil {
|
2017-07-12 13:27:46 +02:00
|
|
|
return utils.WrapPathError(err, filename)
|
2017-06-23 15:32:08 +02:00
|
|
|
}
|
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)
|
|
|
|
}
|
2017-06-23 15:32:08 +02:00
|
|
|
return nil
|
2017-07-24 13:44:49 +02:00
|
|
|
})
|
2017-06-23 15:32:08 +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) {
|
|
|
|
if d.Published() || s.config.Unpublished {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 15:32:08 +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.
|
|
|
|
func (s *Site) ReadCollections() error {
|
|
|
|
for name, data := range s.config.Collections {
|
2017-07-04 15:09:36 +02:00
|
|
|
c := collection.New(s, name, data)
|
2017-06-23 15:32:08 +02:00
|
|
|
s.Collections = append(s.Collections, c)
|
2017-07-03 17:48:06 +02:00
|
|
|
if err := c.ReadPages(); err != nil {
|
2017-06-23 15:32:08 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, p := range c.Pages() {
|
2017-06-29 17:00:59 +02:00
|
|
|
s.AddDocument(p, c.Output())
|
2017-06-23 15:32:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|