1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 02:44:39 +01:00

Remove Site.{Source, Destination}; look in config

This commit is contained in:
Oliver Steele 2017-06-30 20:00:38 -04:00
parent a485a8bcb3
commit c35875d66e
8 changed files with 34 additions and 28 deletions

View File

@ -22,7 +22,7 @@ import (
var commandStartTime = time.Now() var commandStartTime = time.Now()
func buildCommand(site *sites.Site) error { func buildCommand(site *sites.Site) error {
printPathSetting("Destination:", site.Destination) printPathSetting("Destination:", site.DestDir())
printSetting("Generating...", "") printSetting("Generating...", "")
if buildOptions.DryRun { if buildOptions.DryRun {
buildOptions.Verbose = true buildOptions.Verbose = true
@ -125,7 +125,7 @@ func renderCommand(site *sites.Site) error {
if err != nil { if err != nil {
return err return err
} }
printPathSetting("Render:", filepath.Join(site.Source, p.SiteRelPath())) printPathSetting("Render:", filepath.Join(site.SourceDir(), p.SiteRelPath()))
printSetting("URL:", p.Permalink()) printSetting("URL:", p.Permalink())
printSetting("Content:", "") printSetting("Content:", "")
return site.WriteDocument(p, os.Stdout) return site.WriteDocument(p, os.Stdout)

View File

@ -93,19 +93,19 @@ func run(cmd string) error {
// Load the site specified at destination into the site global, and print the common banner settings. // Load the site specified at destination into the site global, and print the common banner settings.
func loadSite(source, destination string) (*sites.Site, error) { func loadSite(source, destination string) (*sites.Site, error) {
site, err := sites.NewSiteFromDirectory(source) if destination == defaultDestination {
destination = ""
}
site, err := sites.NewSiteFromDirectory(source, destination)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if destination != "" && destination != defaultDestination {
site.Destination = destination
}
if site.ConfigFile != nil { if site.ConfigFile != nil {
printPathSetting(configurationFileLabel, *site.ConfigFile) printPathSetting(configurationFileLabel, *site.ConfigFile)
} else { } else {
printSetting(configurationFileLabel, "none") printSetting(configurationFileLabel, "none")
} }
printPathSetting("Source:", site.Source) printPathSetting("Source:", site.SourceDir())
err = site.Load() err = site.Load()
return site, err return site, err
} }

View File

@ -38,7 +38,7 @@ func (s *Server) watchFiles() error {
// remaps permalinks. // remaps permalinks.
urls := map[string]bool{} urls := map[string]bool{}
for _, filename := range filenames { for _, filename := range filenames {
relpath, err := filepath.Rel(site.Source, filename) relpath, err := filepath.Rel(site.SourceDir(), filename)
if err != nil { if err != nil {
log.Println("error:", err) log.Println("error:", err)
continue continue
@ -57,7 +57,7 @@ func (s *Server) watchFiles() error {
} }
}() }()
return watcher.Add(site.Source) return watcher.Add(site.SourceDir())
} }
// debounce relays values from input to output, merging successive values within interval // debounce relays values from input to output, merging successive values within interval

View File

@ -37,10 +37,10 @@ func (s *Site) Clean(options BuildOptions) error {
return os.Remove(name) return os.Remove(name)
} }
} }
if err := filepath.Walk(s.Destination, removeFiles); err != nil { if err := filepath.Walk(s.DestDir(), removeFiles); err != nil {
return err return err
} }
return helpers.RemoveEmptyDirectories(s.Destination) return helpers.RemoveEmptyDirectories(s.DestDir())
} }
// Build cleans the destination and create files in it. // Build cleans the destination and create files in it.

View File

@ -11,7 +11,7 @@ import (
func (s *Site) readDataFiles() error { func (s *Site) readDataFiles() error {
s.data = map[string]interface{}{} s.data = map[string]interface{}{}
dataDir := filepath.Join(s.Source, s.config.DataDir) dataDir := filepath.Join(s.SourceDir(), s.config.DataDir)
files, err := ioutil.ReadDir(dataDir) files, err := ioutil.ReadDir(dataDir)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {

View File

@ -12,7 +12,7 @@ import (
) )
// NewSiteFromDirectory reads the configuration file, if it exists. // NewSiteFromDirectory reads the configuration file, if it exists.
func NewSiteFromDirectory(source string) (*Site, error) { func NewSiteFromDirectory(source, destination string) (*Site, error) {
s := NewSite() s := NewSite()
configPath := filepath.Join(source, "_config.yml") configPath := filepath.Join(source, "_config.yml")
bytes, err := ioutil.ReadFile(configPath) bytes, err := ioutil.ReadFile(configPath)
@ -26,10 +26,12 @@ func NewSiteFromDirectory(source string) (*Site, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
s.Source = filepath.Join(source, s.config.Source)
s.ConfigFile = &configPath s.ConfigFile = &configPath
} }
s.Destination = filepath.Join(s.Source, s.config.Destination) s.config.Source = source
if destination != "" {
s.config.Destination = destination
}
return s, nil return s, nil
} }
@ -44,14 +46,13 @@ func (s *Site) Load() error {
// Reload reloads the config file and pages. // Reload reloads the config file and pages.
// If there's an error loading the config file, it has no effect. // If there's an error loading the config file, it has no effect.
func (s *Site) Reload() error { func (s *Site) Reload() error {
copy, err := NewSiteFromDirectory(s.Source) copy, err := NewSiteFromDirectory(s.SourceDir(), s.config.Destination)
// TODO re-apply site load settings
if err != nil { if err != nil {
return err return err
} }
copy.Destination = s.Destination copy.config.Destination = s.config.Destination
*s = *copy *s = *copy
s.pipeline = nil
s.preparedToRender = false
return s.Load() return s.Load()
} }
@ -64,7 +65,7 @@ func (s *Site) readFiles() error {
return err return err
} }
relname, err := filepath.Rel(s.Source, filename) relname, err := filepath.Rel(s.SourceDir(), filename)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -83,7 +84,7 @@ func (s *Site) readFiles() error {
return nil return nil
} }
if err := filepath.Walk(s.Source, walkFn); err != nil { if err := filepath.Walk(s.SourceDir(), walkFn); err != nil {
return err return err
} }
return s.ReadCollections() return s.ReadCollections()
@ -105,7 +106,7 @@ func (s *Site) ReadCollections() error {
for name, data := range s.config.Collections { for name, data := range s.config.Collections {
c := collections.NewCollection(name, data, s) c := collections.NewCollection(name, data, s)
s.Collections = append(s.Collections, c) s.Collections = append(s.Collections, c)
if err := c.ReadPages(s.Source, s.config.GetFrontMatterDefaults); err != nil { if err := c.ReadPages(s.SourceDir(), s.config.GetFrontMatterDefaults); err != nil {
return err return err
} }
for _, p := range c.Pages() { for _, p := range c.Pages() {

View File

@ -18,9 +18,6 @@ import (
// Site is a Jekyll site. // Site is a Jekyll site.
type Site struct { type Site struct {
ConfigFile *string ConfigFile *string
Source string
Destination string
Collections []*collections.Collection Collections []*collections.Collection
// Variables templates.VariableMap // Variables templates.VariableMap
Routes map[string]pages.Document // URL path -> Page, only for output pages Routes map[string]pages.Document // URL path -> Page, only for output pages
@ -33,6 +30,15 @@ type Site struct {
siteVariables templates.VariableMap siteVariables templates.VariableMap
} }
func (s *Site) SourceDir() string { return s.config.Source }
func (s *Site) DestDir() string {
if filepath.IsAbs(s.config.Destination) {
return s.config.Destination
}
return filepath.Join(s.config.Source, s.config.Destination)
}
// OutputPages returns a list of output pages. // OutputPages returns a list of output pages.
func (s *Site) OutputPages() []pages.Document { func (s *Site) OutputPages() []pages.Document {
out := make([]pages.Document, 0, len(s.Routes)) out := make([]pages.Document, 0, len(s.Routes))
@ -116,7 +122,6 @@ func (c pluginContext) TemplateEngine() liquid.Engine { return c.engine }
// initializeRenderingPipeline initializes the rendering pipeline // initializeRenderingPipeline initializes the rendering pipeline
func (s *Site) initializeRenderingPipeline() (err error) { func (s *Site) initializeRenderingPipeline() (err error) {
options := pipelines.PipelineOptions{ options := pipelines.PipelineOptions{
SourceDir: s.Source,
RelativeFilenameToURL: s.RelativeFilenameToURL, RelativeFilenameToURL: s.RelativeFilenameToURL,
} }
s.pipeline, err = pipelines.NewPipeline(s.config, options) s.pipeline, err = pipelines.NewPipeline(s.config, options)

View File

@ -49,8 +49,8 @@ func (s *Site) WritePages(options BuildOptions) (count int, err error) {
// WritePage writes a page to the destination directory. // WritePage writes a page to the destination directory.
// It attends to options.dry_run. // It attends to options.dry_run.
func (s *Site) WritePage(p pages.Document, options BuildOptions) error { func (s *Site) WritePage(p pages.Document, options BuildOptions) error {
from := filepath.Join(s.Source, filepath.ToSlash(p.SiteRelPath())) from := filepath.Join(s.SourceDir(), filepath.ToSlash(p.SiteRelPath()))
to := filepath.Join(s.Destination, p.Permalink()) to := filepath.Join(s.DestDir(), p.Permalink())
if !p.Static() && filepath.Ext(to) == "" { if !p.Static() && filepath.Ext(to) == "" {
to = filepath.Join(to, "index.html") to = filepath.Join(to, "index.html")
} }