2017-06-17 16:51:32 +02:00
|
|
|
package gojekyll
|
2017-06-10 21:38:09 +02:00
|
|
|
|
|
|
|
import (
|
2017-06-17 05:36:27 +02:00
|
|
|
"io"
|
2017-06-10 21:38:09 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-06-17 05:36:27 +02:00
|
|
|
"path"
|
2017-06-10 21:38:09 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-06-16 04:31:36 +02:00
|
|
|
"time"
|
2017-06-10 21:38:09 +02:00
|
|
|
|
2017-06-17 05:36:27 +02:00
|
|
|
"github.com/acstech/liquid"
|
|
|
|
"github.com/acstech/liquid/core"
|
2017-06-17 02:11:52 +02:00
|
|
|
"github.com/osteele/gojekyll/helpers"
|
2017-06-17 05:36:27 +02:00
|
|
|
liquidHelper "github.com/osteele/gojekyll/liquid"
|
2017-06-10 21:38:09 +02:00
|
|
|
)
|
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
// Site is a Jekyll site.
|
|
|
|
type Site struct {
|
2017-06-16 04:31:36 +02:00
|
|
|
ConfigFile *string
|
|
|
|
Source string
|
|
|
|
Destination string
|
2017-06-13 17:27:24 +02:00
|
|
|
|
|
|
|
Collections []*Collection
|
2017-06-14 23:41:15 +02:00
|
|
|
Variables VariableMap
|
2017-06-15 02:44:22 +02:00
|
|
|
Paths map[string]Page // URL path -> Page
|
2017-06-13 17:27:24 +02:00
|
|
|
|
2017-06-17 05:36:27 +02:00
|
|
|
config SiteConfig
|
|
|
|
liquidConfiguration *core.Configuration
|
|
|
|
sassTempDir string
|
2017-06-13 17:00:24 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 04:31:36 +02:00
|
|
|
// NewSite creates a new site record, initialized with the site defaults.
|
2017-06-13 18:00:14 +02:00
|
|
|
func NewSite() *Site {
|
|
|
|
s := new(Site)
|
2017-06-17 03:53:14 +02:00
|
|
|
if err := s.readConfigBytes([]byte(defaultSiteConfig)); err != nil {
|
2017-06-13 14:54:35 +02:00
|
|
|
panic(err)
|
2017-06-12 23:12:40 +02:00
|
|
|
}
|
2017-06-13 18:00:14 +02:00
|
|
|
return s
|
2017-06-13 14:54:35 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 22:44:09 +02:00
|
|
|
// NewSiteFromDirectory reads the configuration file, if it exists.
|
|
|
|
func NewSiteFromDirectory(source string) (*Site, error) {
|
|
|
|
s := NewSite()
|
2017-06-13 17:27:24 +02:00
|
|
|
configPath := filepath.Join(source, "_config.yml")
|
2017-06-13 18:00:14 +02:00
|
|
|
bytes, err := ioutil.ReadFile(configPath)
|
2017-06-13 17:27:24 +02:00
|
|
|
switch {
|
2017-06-16 22:44:09 +02:00
|
|
|
case err != nil && os.IsNotExist(err):
|
|
|
|
// ok
|
|
|
|
case err != nil:
|
|
|
|
return nil, err
|
|
|
|
default:
|
|
|
|
if err = s.readConfigBytes(bytes); err != nil {
|
|
|
|
return nil, err
|
2017-06-13 17:27:24 +02:00
|
|
|
}
|
2017-06-13 18:00:14 +02:00
|
|
|
s.Source = filepath.Join(source, s.config.Source)
|
|
|
|
s.ConfigFile = &configPath
|
2017-06-13 17:27:24 +02:00
|
|
|
}
|
2017-06-16 22:44:09 +02:00
|
|
|
s.Destination = filepath.Join(s.Source, s.config.Destination)
|
|
|
|
return s, nil
|
2017-06-13 17:27:24 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 18:31:18 +02:00
|
|
|
// Reload reloads the config file and pages.
|
2017-06-17 19:18:28 +02:00
|
|
|
// If there's an error loading the config file, it has no effect.
|
|
|
|
func (s *Site) Reload() error {
|
2017-06-17 18:31:18 +02:00
|
|
|
copy, err := NewSiteFromDirectory(s.Source)
|
|
|
|
if err != nil {
|
2017-06-17 19:18:28 +02:00
|
|
|
return err
|
2017-06-17 18:31:18 +02:00
|
|
|
}
|
2017-06-17 19:18:28 +02:00
|
|
|
copy.Destination = s.Destination
|
|
|
|
*s = *copy
|
|
|
|
return s.ReadFiles()
|
2017-06-17 18:31:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 23:19:05 +02:00
|
|
|
// KeepFile returns a boolean indicating that clean should leave the file in the destination directory.
|
2017-06-13 18:00:14 +02:00
|
|
|
func (s *Site) KeepFile(path string) bool {
|
2017-06-13 17:00:24 +02:00
|
|
|
// TODO
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-06-17 05:50:30 +02:00
|
|
|
// FindPageByFilePath returns a Page or nil, referenced by relative path.
|
|
|
|
func (s *Site) FindPageByFilePath(relpath string) Page {
|
|
|
|
for _, p := range s.Paths {
|
|
|
|
if p.Path() == relpath {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
// GetFileURL returns the URL path given a file path, relative to the site source directory.
|
|
|
|
func (s *Site) GetFileURL(path string) (string, bool) {
|
2017-06-15 02:44:22 +02:00
|
|
|
for _, p := range s.Paths {
|
|
|
|
if p.Path() == path {
|
|
|
|
return p.Permalink(), true
|
2017-06-13 14:55:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2017-06-17 05:50:30 +02:00
|
|
|
// PageForURL returns the page that will be served at URL
|
|
|
|
func (s *Site) PageForURL(urlpath string) (p Page, found bool) {
|
|
|
|
p, found = s.Paths[urlpath]
|
|
|
|
if !found {
|
|
|
|
p, found = s.Paths[filepath.Join(urlpath, "index.html")]
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
p, found = s.Paths[filepath.Join(urlpath, "index.htm")]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:19:05 +02:00
|
|
|
// Exclude returns a boolean indicating that the site excludes a file.
|
2017-06-13 17:00:24 +02:00
|
|
|
func (s *Site) Exclude(path string) bool {
|
|
|
|
// TODO exclude based on glob, not exact match
|
2017-06-17 02:11:52 +02:00
|
|
|
inclusionMap := helpers.StringArrayToMap(s.config.Include)
|
|
|
|
exclusionMap := helpers.StringArrayToMap(s.config.Exclude)
|
2017-06-13 17:00:24 +02:00
|
|
|
base := filepath.Base(path)
|
|
|
|
switch {
|
2017-06-13 18:38:06 +02:00
|
|
|
case inclusionMap[path]:
|
|
|
|
return false
|
2017-06-13 17:00:24 +02:00
|
|
|
case path == ".":
|
|
|
|
return false
|
|
|
|
case exclusionMap[path]:
|
|
|
|
return true
|
|
|
|
case strings.HasPrefix(base, "."), strings.HasPrefix(base, "_"):
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
|
2017-06-17 03:53:14 +02:00
|
|
|
// LayoutsDir returns the path to the layouts directory.
|
|
|
|
func (s *Site) LayoutsDir() string {
|
|
|
|
return filepath.Join(s.Source, s.config.LayoutsDir)
|
|
|
|
}
|
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
// ReadFiles scans the source directory and creates pages and collections.
|
|
|
|
func (s *Site) ReadFiles() error {
|
2017-06-15 02:44:22 +02:00
|
|
|
s.Paths = make(map[string]Page)
|
2017-06-12 23:12:40 +02:00
|
|
|
|
2017-06-17 05:36:27 +02:00
|
|
|
walkFn := func(name string, info os.FileInfo, err error) error {
|
2017-06-10 21:38:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-17 05:36:27 +02:00
|
|
|
relname, err := filepath.Rel(s.Source, name)
|
2017-06-10 21:38:09 +02:00
|
|
|
if err != nil {
|
2017-06-17 05:36:27 +02:00
|
|
|
panic(err)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-13 17:00:24 +02:00
|
|
|
switch {
|
2017-06-17 05:30:10 +02:00
|
|
|
case info.IsDir() && s.Exclude(relname):
|
2017-06-13 17:00:24 +02:00
|
|
|
return filepath.SkipDir
|
2017-06-17 05:30:10 +02:00
|
|
|
case info.IsDir(), s.Exclude(relname):
|
2017-06-10 21:38:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-17 05:36:27 +02:00
|
|
|
defaults := s.GetFrontMatterDefaults(relname, "")
|
|
|
|
p, err := ReadPage(s, nil, relname, defaults)
|
2017-06-10 23:51:46 +02:00
|
|
|
if err != nil {
|
2017-06-17 19:06:53 +02:00
|
|
|
return helpers.PathError(err, "read", name)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-15 02:44:22 +02:00
|
|
|
if p.Published() {
|
|
|
|
s.Paths[p.Permalink()] = p
|
2017-06-11 01:32:39 +02:00
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-11 01:32:39 +02:00
|
|
|
|
2017-06-13 17:27:24 +02:00
|
|
|
if err := filepath.Walk(s.Source, walkFn); err != nil {
|
2017-06-13 17:00:24 +02:00
|
|
|
return err
|
2017-06-13 14:55:15 +02:00
|
|
|
}
|
2017-06-16 22:44:09 +02:00
|
|
|
if err := s.ReadCollections(); err != nil {
|
2017-06-14 19:20:52 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-06-15 13:19:49 +02:00
|
|
|
s.initTemplateAttributes()
|
2017-06-14 19:20:52 +02:00
|
|
|
return nil
|
2017-06-13 14:55:15 +02:00
|
|
|
}
|
2017-06-10 23:51:46 +02:00
|
|
|
|
2017-06-15 13:19:49 +02:00
|
|
|
func (s *Site) initTemplateAttributes() {
|
2017-06-16 04:31:36 +02:00
|
|
|
// TODO site: {pages, posts, related_posts, static_files, html_pages, html_files, collections, data, documents, categories.CATEGORY, tags.TAG}
|
2017-06-16 21:47:41 +02:00
|
|
|
s.Variables = MergeVariableMaps(s.Variables, VariableMap{
|
2017-06-16 04:31:36 +02:00
|
|
|
"time": time.Now(),
|
|
|
|
})
|
2017-06-14 19:20:52 +02:00
|
|
|
for _, c := range s.Collections {
|
2017-06-15 13:19:49 +02:00
|
|
|
s.Variables[c.Name] = c.PageTemplateObjects()
|
2017-06-14 19:20:52 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-17 04:03:28 +02:00
|
|
|
|
2017-06-17 05:36:27 +02:00
|
|
|
// LiquidConfiguration configures the liquid tags with site-specific behavior.
|
|
|
|
func (s *Site) LiquidConfiguration() *core.Configuration {
|
|
|
|
if s.liquidConfiguration != nil {
|
|
|
|
return s.liquidConfiguration
|
|
|
|
}
|
|
|
|
liquidHelper.SetFilePathURLGetter(s.GetFileURL)
|
|
|
|
includeHandler := func(name string, writer io.Writer, data map[string]interface{}) {
|
|
|
|
name = strings.TrimLeft(strings.TrimRight(name, "}}"), "{{")
|
|
|
|
filename := path.Join(s.Source, s.config.IncludesDir, name)
|
|
|
|
template, err := liquid.ParseFile(filename, s.liquidConfiguration)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
template.Render(writer, data)
|
|
|
|
}
|
|
|
|
s.liquidConfiguration = liquid.Configure().IncludeHandler(includeHandler)
|
|
|
|
return s.liquidConfiguration
|
|
|
|
}
|
|
|
|
|
2017-06-17 05:30:10 +02:00
|
|
|
// GetFrontMatterDefaults implements https://jekyllrb.com/docs/configuration/#front-matter-defaults
|
|
|
|
func (s *Site) GetFrontMatterDefaults(relpath, typename string) (m VariableMap) {
|
|
|
|
for _, entry := range s.config.Defaults {
|
|
|
|
scope := &entry.Scope
|
|
|
|
hasPrefix := strings.HasPrefix(relpath, scope.Path)
|
|
|
|
hasType := scope.Type == "" || scope.Type == typename
|
|
|
|
if hasPrefix && hasType {
|
|
|
|
m = MergeVariableMaps(m, entry.Values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2017-06-17 04:03:28 +02:00
|
|
|
}
|