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"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-06-19 20:44:34 +02:00
|
|
|
|
2017-06-17 02:11:52 +02:00
|
|
|
"github.com/osteele/gojekyll/helpers"
|
2017-06-18 11:59:12 +02:00
|
|
|
"github.com/osteele/gojekyll/liquid"
|
2017-06-22 17:02:32 +02:00
|
|
|
"github.com/osteele/gojekyll/pages"
|
2017-06-22 16:42:57 +02:00
|
|
|
"github.com/osteele/gojekyll/templates"
|
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-19 04:24:10 +02:00
|
|
|
ConfigFile *string
|
|
|
|
Source string
|
|
|
|
Destination string
|
|
|
|
UseRemoteLiquidEngine bool
|
2017-06-13 17:27:24 +02:00
|
|
|
|
|
|
|
Collections []*Collection
|
2017-06-22 16:42:57 +02:00
|
|
|
Variables templates.VariableMap
|
2017-06-22 17:02:32 +02:00
|
|
|
Paths map[string]pages.Page // URL path -> Page
|
2017-06-13 17:27:24 +02:00
|
|
|
|
2017-06-18 11:59:12 +02:00
|
|
|
config SiteConfig
|
|
|
|
liquidEngine liquid.Engine
|
|
|
|
sassTempDir string
|
2017-06-13 17:00:24 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 14:35:18 +02:00
|
|
|
// SourceDir returns the sites source directory.
|
|
|
|
func (s *Site) SourceDir() string { return s.Source }
|
|
|
|
|
2017-06-22 16:37:31 +02:00
|
|
|
// Output returns true, indicating that the files in the site should be written.
|
|
|
|
func (s *Site) Output() bool { return true }
|
|
|
|
|
|
|
|
// PathPrefix returns the relative directory prefix.
|
|
|
|
func (s *Site) PathPrefix() string { return "" }
|
|
|
|
|
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.
|
2017-06-22 14:35:18 +02:00
|
|
|
func (s *Site) Reload() error {
|
|
|
|
copy, err := NewSiteFromDirectory(s.Source)
|
2017-06-17 18:31:18 +02:00
|
|
|
if err != nil {
|
2017-06-17 19:18:28 +02:00
|
|
|
return err
|
2017-06-17 18:31:18 +02:00
|
|
|
}
|
2017-06-22 14:35:18 +02:00
|
|
|
copy.Destination = s.Destination
|
|
|
|
*s = *copy
|
|
|
|
s.sassTempDir = ""
|
2017-06-19 20:03:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads the site data and files. It doesn't load the configuration file; NewSiteFromDirectory did that.
|
2017-06-22 14:35:18 +02:00
|
|
|
func (s *Site) Load() (err error) {
|
|
|
|
err = s.readFiles()
|
2017-06-19 20:03:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-06-22 14:35:18 +02:00
|
|
|
err = s.initSiteVariables()
|
2017-06-19 20:44:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-06-22 14:35:18 +02:00
|
|
|
s.liquidEngine, err = s.makeLiquidEngine()
|
2017-06-19 20:03:04 +02:00
|
|
|
return
|
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-22 14:35:18 +02:00
|
|
|
func (s *Site) KeepFile(path string) bool {
|
2017-06-13 17:00:24 +02:00
|
|
|
// TODO
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-06-22 16:37:31 +02:00
|
|
|
// RelPathPage returns a Page, give a file path relative to site source directory.
|
2017-06-22 17:02:32 +02:00
|
|
|
func (s *Site) RelPathPage(relpath string) (pages.Page, bool) {
|
2017-06-22 14:35:18 +02:00
|
|
|
for _, p := range s.Paths {
|
2017-06-22 16:37:31 +02:00
|
|
|
if p.SiteRelPath() == relpath {
|
|
|
|
return p, true
|
2017-06-17 05:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-22 16:37:31 +02:00
|
|
|
return nil, false
|
2017-06-17 05:50:30 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 16:37:31 +02:00
|
|
|
// RelPathURL returns a page's relative URL, give a file path relative to the site source directory.
|
|
|
|
func (s *Site) RelPathURL(relpath string) (string, bool) {
|
|
|
|
var url string
|
|
|
|
p, found := s.RelPathPage(relpath)
|
|
|
|
if found {
|
|
|
|
url = p.Permalink()
|
2017-06-13 14:55:15 +02:00
|
|
|
}
|
2017-06-22 16:37:31 +02:00
|
|
|
return url, found
|
2017-06-13 14:55:15 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 16:37:31 +02:00
|
|
|
// URLPage returns the page that will be served at URL
|
2017-06-22 17:02:32 +02:00
|
|
|
func (s *Site) URLPage(urlpath string) (p pages.Page, found bool) {
|
2017-06-22 14:35:18 +02:00
|
|
|
p, found = s.Paths[urlpath]
|
2017-06-17 05:50:30 +02:00
|
|
|
if !found {
|
2017-06-22 14:35:18 +02:00
|
|
|
p, found = s.Paths[filepath.Join(urlpath, "index.html")]
|
2017-06-17 05:50:30 +02:00
|
|
|
}
|
|
|
|
if !found {
|
2017-06-22 14:35:18 +02:00
|
|
|
p, found = s.Paths[filepath.Join(urlpath, "index.htm")]
|
2017-06-17 05:50:30 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:19:05 +02:00
|
|
|
// Exclude returns a boolean indicating that the site excludes a file.
|
2017-06-22 14:35:18 +02:00
|
|
|
func (s *Site) Exclude(path string) bool {
|
2017-06-13 17:00:24 +02:00
|
|
|
// TODO exclude based on glob, not exact match
|
2017-06-22 14:35:18 +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.
|
2017-06-22 14:35:18 +02:00
|
|
|
func (s *Site) LayoutsDir() string {
|
|
|
|
return filepath.Join(s.Source, s.config.LayoutsDir)
|
2017-06-17 03:53:14 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 14:59:54 +02:00
|
|
|
// readFiles scans the source directory and creates pages and collections.
|
2017-06-22 14:35:18 +02:00
|
|
|
func (s *Site) readFiles() error {
|
2017-06-22 17:02:32 +02:00
|
|
|
s.Paths = make(map[string]pages.Page)
|
2017-06-12 23:12:40 +02:00
|
|
|
|
2017-06-22 16:37:31 +02:00
|
|
|
walkFn := func(filename string, info os.FileInfo, err error) error {
|
2017-06-10 21:38:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-22 16:37:31 +02:00
|
|
|
relname, err := filepath.Rel(s.Source, filename)
|
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-22 14:35:18 +02:00
|
|
|
case info.IsDir() && s.Exclude(relname):
|
2017-06-13 17:00:24 +02:00
|
|
|
return filepath.SkipDir
|
2017-06-22 14:35:18 +02:00
|
|
|
case info.IsDir(), s.Exclude(relname):
|
2017-06-10 21:38:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-22 14:35:18 +02:00
|
|
|
defaults := s.GetFrontMatterDefaults(relname, "")
|
2017-06-22 17:02:32 +02:00
|
|
|
p, err := pages.NewPageFromFile(s, s, filename, relname, defaults)
|
2017-06-10 23:51:46 +02:00
|
|
|
if err != nil {
|
2017-06-22 16:37:31 +02:00
|
|
|
return helpers.PathError(err, "read", filename)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-15 02:44:22 +02:00
|
|
|
if p.Published() {
|
2017-06-22 14:35:18 +02:00
|
|
|
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-22 14:35:18 +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-22 14:35:18 +02:00
|
|
|
return s.ReadCollections()
|
2017-06-13 14:55:15 +02:00
|
|
|
}
|
2017-06-10 23:51:46 +02:00
|
|
|
|
2017-06-20 14:59:54 +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-06-22 14:35:18 +02:00
|
|
|
func (s *Site) ReadCollections() error {
|
|
|
|
for name, data := range s.config.Collections {
|
2017-06-22 16:37:31 +02:00
|
|
|
c := NewCollection(s, name, data)
|
|
|
|
s.Collections = append(s.Collections, c)
|
|
|
|
if err := c.ReadPages(); err != nil {
|
2017-06-20 14:59:54 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-06-22 16:37:31 +02:00
|
|
|
for _, p := range c.Pages() {
|
|
|
|
if p.Published() {
|
|
|
|
s.Paths[p.Permalink()] = p
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 14:59:54 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-21 15:11:09 +02:00
|
|
|
// CollectionVariable creates the value of the site.[collectionName] variable
|
2017-06-22 14:35:18 +02:00
|
|
|
func (s *Site) CollectionVariable() error {
|
2017-06-22 15:36:39 +02:00
|
|
|
for _, c := range s.Collections {
|
|
|
|
for _, p := range c.Pages() {
|
|
|
|
if err := p.Write(s, ioutil.Discard); err != nil {
|
2017-06-20 22:17:59 +02:00
|
|
|
return err
|
2017-06-19 20:44:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-22 14:35:18 +02:00
|
|
|
s.updateCollectionVariables()
|
2017-06-20 22:17:59 +02:00
|
|
|
return nil
|
2017-06-14 19:20:52 +02:00
|
|
|
}
|
2017-06-17 04:03:28 +02:00
|
|
|
|
2017-06-22 14:35:18 +02:00
|
|
|
func (s *Site) makeLocalLiquidEngine() liquid.Engine {
|
2017-06-18 23:00:33 +02:00
|
|
|
engine := liquid.NewLocalWrapperEngine()
|
2017-06-22 16:37:31 +02:00
|
|
|
engine.LinkTagHandler(s.RelPathURL)
|
2017-06-19 16:27:57 +02:00
|
|
|
includeHandler := func(name string, w io.Writer, scope map[string]interface{}) error {
|
2017-06-22 14:35:18 +02:00
|
|
|
filename := filepath.Join(s.Source, s.config.IncludesDir, name)
|
2017-06-18 11:59:12 +02:00
|
|
|
template, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
2017-06-19 16:27:57 +02:00
|
|
|
return err
|
2017-06-18 11:59:12 +02:00
|
|
|
}
|
2017-06-18 23:00:33 +02:00
|
|
|
text, err := engine.ParseAndRender(template, scope)
|
|
|
|
if err != nil {
|
2017-06-19 16:27:57 +02:00
|
|
|
return err
|
2017-06-18 23:00:33 +02:00
|
|
|
}
|
2017-06-18 11:59:12 +02:00
|
|
|
_, err = w.Write(text)
|
2017-06-19 16:27:57 +02:00
|
|
|
return err
|
2017-06-17 05:36:27 +02:00
|
|
|
}
|
2017-06-18 23:00:33 +02:00
|
|
|
engine.IncludeHandler(includeHandler)
|
|
|
|
return engine
|
2017-06-17 05:36:27 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 14:35:18 +02:00
|
|
|
func (s *Site) makeLiquidClient() (engine liquid.RemoteEngine, err error) {
|
2017-06-19 16:27:57 +02:00
|
|
|
engine, err = liquid.NewRPCClientEngine(liquid.DefaultServer)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-06-18 23:00:33 +02:00
|
|
|
urls := map[string]string{}
|
2017-06-22 14:35:18 +02:00
|
|
|
for _, p := range s.Paths {
|
2017-06-22 16:37:31 +02:00
|
|
|
urls[p.SiteRelPath()] = p.Permalink()
|
2017-06-18 21:37:21 +02:00
|
|
|
}
|
2017-06-19 16:27:57 +02:00
|
|
|
err = engine.FileURLMap(urls)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-06-22 14:35:18 +02:00
|
|
|
err = engine.IncludeDirs([]string{filepath.Join(s.Source, s.config.IncludesDir)})
|
2017-06-19 16:27:57 +02:00
|
|
|
return
|
2017-06-18 21:37:21 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 14:35:18 +02:00
|
|
|
func (s *Site) makeLiquidEngine() (liquid.Engine, error) {
|
|
|
|
if s.UseRemoteLiquidEngine {
|
|
|
|
return s.makeLiquidClient()
|
2017-06-19 20:03:04 +02:00
|
|
|
}
|
2017-06-22 14:35:18 +02:00
|
|
|
return s.makeLocalLiquidEngine(), nil
|
2017-06-19 20:03:04 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 15:36:39 +02:00
|
|
|
// TemplateEngine create a liquid engine configured to with include paths and link tag resolution
|
2017-06-19 16:27:57 +02:00
|
|
|
// for this site.
|
2017-06-22 15:36:39 +02:00
|
|
|
func (s *Site) TemplateEngine() liquid.Engine {
|
2017-06-22 14:35:18 +02:00
|
|
|
return s.liquidEngine
|
2017-06-18 21:37:21 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 05:30:10 +02:00
|
|
|
// GetFrontMatterDefaults implements https://jekyllrb.com/docs/configuration/#front-matter-defaults
|
2017-06-22 16:42:57 +02:00
|
|
|
func (s *Site) GetFrontMatterDefaults(relpath, typename string) (m templates.VariableMap) {
|
2017-06-22 14:35:18 +02:00
|
|
|
for _, entry := range s.config.Defaults {
|
2017-06-17 05:30:10 +02:00
|
|
|
scope := &entry.Scope
|
|
|
|
hasPrefix := strings.HasPrefix(relpath, scope.Path)
|
|
|
|
hasType := scope.Type == "" || scope.Type == typename
|
|
|
|
if hasPrefix && hasType {
|
2017-06-22 16:42:57 +02:00
|
|
|
m = templates.MergeVariableMaps(m, entry.Values)
|
2017-06-17 05:30:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2017-06-17 04:03:28 +02:00
|
|
|
}
|