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

273 lines
6.9 KiB
Go
Raw Normal View History

package gojekyll
import (
2017-06-17 05:36:27 +02:00
"io"
"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"
)
// Site is a Jekyll site.
type Site struct {
2017-06-19 04:24:10 +02:00
ConfigFile *string
Source string
Destination string
UseRemoteLiquidEngine bool
Collections []*Collection
2017-06-14 23:41:15 +02:00
Variables VariableMap
Paths map[string]Page // URL path -> Page
2017-06-18 11:59:12 +02:00
config SiteConfig
liquidEngine liquid.Engine
sassTempDir string
}
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)
if err := s.readConfigBytes([]byte(defaultSiteConfig)); err != nil {
panic(err)
}
2017-06-13 18:00:14 +02:00
return s
}
// NewSiteFromDirectory reads the configuration file, if it exists.
func NewSiteFromDirectory(source string) (*Site, error) {
s := NewSite()
configPath := filepath.Join(source, "_config.yml")
2017-06-13 18:00:14 +02:00
bytes, err := ioutil.ReadFile(configPath)
switch {
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 18:00:14 +02:00
s.Source = filepath.Join(source, s.config.Source)
s.ConfigFile = &configPath
}
s.Destination = filepath.Join(s.Source, s.config.Destination)
return s, nil
}
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-19 20:03:04 +02:00
func (site *Site) Reload() error {
copy, err := NewSiteFromDirectory(site.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-19 20:03:04 +02:00
copy.Destination = site.Destination
*site = *copy
site.sassTempDir = ""
return nil
}
// Load loads the site data and files. It doesn't load the configuration file; NewSiteFromDirectory did that.
func (site *Site) Load() (err error) {
2017-06-20 14:59:54 +02:00
err = site.readFiles()
2017-06-19 20:03:04 +02:00
if err != nil {
return
}
2017-06-19 20:44:34 +02:00
err = site.initSiteVariables()
if err != nil {
return
}
2017-06-19 20:03:04 +02:00
site.liquidEngine, err = site.makeLiquidEngine()
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-19 20:03:04 +02:00
func (site *Site) KeepFile(path string) bool {
// TODO
return false
}
// FindPageByFilePath returns a Page or nil, referenced by relative path.
2017-06-19 20:03:04 +02:00
func (site *Site) FindPageByFilePath(relpath string) Page {
for _, p := range site.Paths {
if p.Path() == relpath {
return p
}
}
return nil
}
// GetFileURL returns the URL path given a file path, relative to the site source directory.
2017-06-19 20:03:04 +02:00
func (site *Site) GetFileURL(relpath string) (string, bool) {
for _, p := range site.Paths {
if p.Path() == relpath {
return p.Permalink(), true
}
}
return "", false
}
// PageForURL returns the page that will be served at URL
2017-06-19 20:03:04 +02:00
func (site *Site) PageForURL(urlpath string) (p Page, found bool) {
p, found = site.Paths[urlpath]
if !found {
2017-06-19 20:03:04 +02:00
p, found = site.Paths[filepath.Join(urlpath, "index.html")]
}
if !found {
2017-06-19 20:03:04 +02:00
p, found = site.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-19 20:03:04 +02:00
func (site *Site) Exclude(path string) bool {
// TODO exclude based on glob, not exact match
2017-06-19 20:03:04 +02:00
inclusionMap := helpers.StringArrayToMap(site.config.Include)
exclusionMap := helpers.StringArrayToMap(site.config.Exclude)
base := filepath.Base(path)
switch {
2017-06-13 18:38:06 +02:00
case inclusionMap[path]:
return false
case path == ".":
return false
case exclusionMap[path]:
return true
case strings.HasPrefix(base, "."), strings.HasPrefix(base, "_"):
return true
default:
return false
}
}
// LayoutsDir returns the path to the layouts directory.
2017-06-19 20:03:04 +02:00
func (site *Site) LayoutsDir() string {
return filepath.Join(site.Source, site.config.LayoutsDir)
}
2017-06-20 14:59:54 +02:00
// readFiles scans the source directory and creates pages and collections.
func (site *Site) readFiles() error {
2017-06-19 20:03:04 +02:00
site.Paths = make(map[string]Page)
2017-06-17 05:36:27 +02:00
walkFn := func(name string, info os.FileInfo, err error) error {
if err != nil {
return err
}
2017-06-19 20:03:04 +02:00
relname, err := filepath.Rel(site.Source, name)
if err != nil {
2017-06-17 05:36:27 +02:00
panic(err)
}
switch {
2017-06-19 20:03:04 +02:00
case info.IsDir() && site.Exclude(relname):
return filepath.SkipDir
2017-06-19 20:03:04 +02:00
case info.IsDir(), site.Exclude(relname):
return nil
}
2017-06-19 20:03:04 +02:00
defaults := site.GetFrontMatterDefaults(relname, "")
p, err := ReadPage(site, nil, relname, defaults)
2017-06-10 23:51:46 +02:00
if err != nil {
return helpers.PathError(err, "read", name)
}
if p.Published() {
2017-06-19 20:03:04 +02:00
site.Paths[p.Permalink()] = p
}
return nil
}
2017-06-19 20:03:04 +02:00
if err := filepath.Walk(site.Source, walkFn); err != nil {
return err
}
2017-06-19 20:03:04 +02:00
return site.ReadCollections()
}
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.
func (site *Site) ReadCollections() error {
for name, data := range site.config.Collections {
coll := NewCollection(site, name, data)
site.Collections = append(site.Collections, coll)
if err := coll.ReadPages(); err != nil {
return err
}
}
return nil
}
2017-06-20 22:17:59 +02:00
func (site *Site) CreateCollectionContent() error {
for _, coll := range site.Collections {
for _, p := range coll.Pages() {
if err := p.Write(ioutil.Discard); err != nil {
return err
2017-06-19 20:44:34 +02:00
}
}
}
2017-06-20 22:17:59 +02:00
site.updateCollectionVariables()
return nil
2017-06-14 19:20:52 +02:00
}
2017-06-19 20:03:04 +02:00
func (site *Site) makeLocalLiquidEngine() liquid.Engine {
2017-06-18 23:00:33 +02:00
engine := liquid.NewLocalWrapperEngine()
2017-06-19 20:03:04 +02:00
engine.LinkTagHandler(site.GetFileURL)
2017-06-19 16:27:57 +02:00
includeHandler := func(name string, w io.Writer, scope map[string]interface{}) error {
2017-06-19 20:03:04 +02:00
filename := filepath.Join(site.Source, site.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-19 20:03:04 +02:00
func (site *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-19 20:03:04 +02:00
for _, p := range site.Paths {
2017-06-18 23:00:33 +02:00
urls[p.Path()] = p.Permalink()
}
2017-06-19 16:27:57 +02:00
err = engine.FileURLMap(urls)
if err != nil {
return
}
2017-06-19 20:03:04 +02:00
err = engine.IncludeDirs([]string{filepath.Join(site.Source, site.config.IncludesDir)})
2017-06-19 16:27:57 +02:00
return
}
2017-06-19 20:03:04 +02:00
func (site *Site) makeLiquidEngine() (liquid.Engine, error) {
if site.UseRemoteLiquidEngine {
return site.makeLiquidClient()
}
return site.makeLocalLiquidEngine(), nil
}
2017-06-19 16:27:57 +02:00
// LiquidEngine create a liquid engine configured to with include paths and link tag resolution
// for this site.
2017-06-19 20:03:04 +02:00
func (site *Site) LiquidEngine() liquid.Engine {
return site.liquidEngine
}
2017-06-17 05:30:10 +02:00
// GetFrontMatterDefaults implements https://jekyllrb.com/docs/configuration/#front-matter-defaults
2017-06-19 20:03:04 +02:00
func (site *Site) GetFrontMatterDefaults(relpath, typename string) (m VariableMap) {
for _, entry := range site.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 {
m = MergeVariableMaps(m, entry.Values)
}
}
return
}