2017-06-22 21:21:47 +02:00
|
|
|
package sites
|
2017-06-10 21:38:09 +02:00
|
|
|
|
|
|
|
import (
|
2017-06-24 19:30:01 +02:00
|
|
|
"fmt"
|
2017-06-10 21:38:09 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-06-19 20:44:34 +02:00
|
|
|
|
2017-06-22 21:21:47 +02:00
|
|
|
"github.com/osteele/gojekyll/collections"
|
|
|
|
"github.com/osteele/gojekyll/config"
|
2017-06-17 02:11:52 +02:00
|
|
|
"github.com/osteele/gojekyll/helpers"
|
2017-06-22 17:02:32 +02:00
|
|
|
"github.com/osteele/gojekyll/pages"
|
2017-06-24 20:00:19 +02:00
|
|
|
"github.com/osteele/gojekyll/pipelines"
|
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
|
|
|
|
2017-06-22 21:21:47 +02:00
|
|
|
Collections []*collections.Collection
|
2017-06-22 16:42:57 +02:00
|
|
|
Variables templates.VariableMap
|
2017-06-23 21:27:13 +02:00
|
|
|
Paths map[string]pages.Page // URL path -> Page, only for output pages
|
2017-06-13 17:27:24 +02:00
|
|
|
|
2017-06-24 19:30:01 +02:00
|
|
|
config config.Config
|
2017-06-24 20:00:19 +02:00
|
|
|
pipeline pipelines.PipelineInterface
|
|
|
|
pages []pages.Page // all pages, output or not
|
2017-06-13 17:00:24 +02:00
|
|
|
}
|
|
|
|
|
2017-06-23 21:27:13 +02:00
|
|
|
// Pages returns a slice of pages.
|
|
|
|
func (s *Site) Pages() []pages.Page { return s.pages }
|
2017-06-22 16:37:31 +02:00
|
|
|
|
|
|
|
// 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 {
|
2017-06-22 21:21:47 +02:00
|
|
|
return &Site{config: config.Default()}
|
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:
|
2017-06-22 21:21:47 +02:00
|
|
|
err = config.Unmarshal(bytes, &s.config)
|
|
|
|
if err != nil {
|
2017-06-16 22:44:09 +02:00
|
|
|
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-29 00:44:39 +02:00
|
|
|
// SetAbsoluteURL overrides the loaded configuration.
|
|
|
|
// The server uses this.
|
|
|
|
func (s *Site) SetAbsoluteURL(url string) {
|
|
|
|
s.config.AbsoluteURL = url
|
|
|
|
s.config.Variables["url"] = url
|
|
|
|
s.Variables["url"] = url
|
|
|
|
}
|
|
|
|
|
2017-06-24 20:00:19 +02:00
|
|
|
// FilenameURLs returns a map of relative filenames to URL paths
|
|
|
|
func (s *Site) FilenameURLs() map[string]string {
|
|
|
|
urls := map[string]string{}
|
|
|
|
for _, page := range s.Pages() {
|
|
|
|
urls[page.SiteRelPath()] = page.Permalink()
|
|
|
|
}
|
|
|
|
return urls
|
|
|
|
}
|
|
|
|
|
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-24 20:00:19 +02:00
|
|
|
// RelativeFilenameToURL returns a page's relative URL, give a file path relative to the site source directory.
|
2017-06-24 19:30:01 +02:00
|
|
|
func (s *Site) RelativeFilenameToURL(relpath string) (string, bool) {
|
2017-06-22 16:37:31 +02:00
|
|
|
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-24 19:30:01 +02:00
|
|
|
// RenderingPipeline returns the rendering pipeline.
|
2017-06-24 20:00:19 +02:00
|
|
|
func (s *Site) RenderingPipeline() pipelines.PipelineInterface {
|
2017-06-24 19:30:01 +02:00
|
|
|
if s.pipeline == nil {
|
|
|
|
panic(fmt.Errorf("uninitialized rendering pipeline"))
|
|
|
|
}
|
|
|
|
return s.pipeline
|
|
|
|
}
|
|
|
|
|
2017-06-24 20:00:19 +02:00
|
|
|
// InitializeRenderingPipeline initializes the rendering pipeline
|
2017-06-29 00:44:39 +02:00
|
|
|
func (s *Site) InitializeRenderingPipeline() error {
|
2017-06-24 20:00:19 +02:00
|
|
|
o := pipelines.PipelineOptions{UseRemoteLiquidEngine: s.UseRemoteLiquidEngine}
|
2017-06-29 00:44:39 +02:00
|
|
|
var err error
|
2017-06-24 20:00:19 +02:00
|
|
|
s.pipeline, err = pipelines.NewPipeline(s.Source, s.config, s, o)
|
2017-06-29 00:44:39 +02:00
|
|
|
return err
|
2017-06-24 19:30:01 +02:00
|
|
|
}
|
|
|
|
|
2017-06-24 20:00:19 +02:00
|
|
|
// OutputExt returns the output extension.
|
|
|
|
func (s *Site) OutputExt(pathname string) string {
|
|
|
|
return s.config.OutputExt(pathname)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|