2017-06-17 16:51:32 +02:00
|
|
|
package gojekyll
|
2017-06-10 21:38:09 +02:00
|
|
|
|
|
|
|
import (
|
2017-06-13 17:00:24 +02:00
|
|
|
"fmt"
|
2017-06-10 21:38:09 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-17 01:17:22 +02:00
|
|
|
|
2017-06-17 02:11:52 +02:00
|
|
|
"github.com/osteele/gojekyll/helpers"
|
2017-06-10 21:38:09 +02:00
|
|
|
)
|
|
|
|
|
2017-06-20 14:59:54 +02:00
|
|
|
// PageContainer has a slice of pages
|
|
|
|
type PageContainer interface {
|
|
|
|
Pages() []Page
|
|
|
|
}
|
|
|
|
|
2017-06-17 16:51:32 +02:00
|
|
|
// BuildOptions holds options for Build and Clean
|
|
|
|
type BuildOptions struct {
|
|
|
|
DryRun bool
|
|
|
|
UseHardLinks bool
|
2017-06-18 22:55:52 +02:00
|
|
|
Verbose bool
|
2017-06-17 16:51:32 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 14:59:54 +02:00
|
|
|
// Pages is a list of pages.
|
|
|
|
func (site *Site) Pages() []Page {
|
|
|
|
pages := make([]Page, len(site.Paths))
|
|
|
|
i := 0
|
|
|
|
for _, p := range site.Paths {
|
|
|
|
pages[i] = p
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return pages
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pages is a list of pages.
|
|
|
|
func (coll *Collection) Pages() []Page {
|
|
|
|
return coll.pages
|
|
|
|
}
|
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
// Clean the destination. Remove files that aren't in keep_files, and resulting empty diretories.
|
2017-06-19 20:03:04 +02:00
|
|
|
func (site *Site) Clean(options BuildOptions) error {
|
|
|
|
// TODO PERF when called as part of build, keep files that will be re-generated
|
2017-06-17 02:06:55 +02:00
|
|
|
removeFiles := func(name string, info os.FileInfo, err error) error {
|
2017-06-18 22:55:52 +02:00
|
|
|
if options.Verbose {
|
|
|
|
fmt.Println("rm", name)
|
|
|
|
}
|
2017-06-13 17:00:24 +02:00
|
|
|
switch {
|
|
|
|
case err != nil && os.IsNotExist(err):
|
|
|
|
return nil
|
|
|
|
case err != nil:
|
2017-06-10 21:38:09 +02:00
|
|
|
return err
|
2017-06-13 17:00:24 +02:00
|
|
|
case info.IsDir():
|
2017-06-10 21:38:09 +02:00
|
|
|
return nil
|
2017-06-19 20:03:04 +02:00
|
|
|
case site.KeepFile(name):
|
2017-06-13 17:00:24 +02:00
|
|
|
return nil
|
2017-06-17 16:51:32 +02:00
|
|
|
case options.DryRun:
|
2017-06-18 22:55:52 +02:00
|
|
|
return nil
|
2017-06-13 17:00:24 +02:00
|
|
|
default:
|
2017-06-17 02:06:55 +02:00
|
|
|
return os.Remove(name)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-19 20:03:04 +02:00
|
|
|
if err := filepath.Walk(site.Destination, removeFiles); err != nil {
|
2017-06-10 23:51:46 +02:00
|
|
|
return err
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-19 20:03:04 +02:00
|
|
|
return helpers.RemoveEmptyDirectories(site.Destination)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
// Build cleans the destination and create files in it.
|
|
|
|
// It attends to the global options.dry_run.
|
2017-06-20 14:59:54 +02:00
|
|
|
func (site *Site) Build(options BuildOptions) (int, error) {
|
|
|
|
count := 0
|
|
|
|
if err := site.Clean(options); err != nil {
|
|
|
|
return count, err
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-20 14:59:54 +02:00
|
|
|
if err := site.CopySassFileIncludes(); err != nil {
|
|
|
|
return count, err
|
2017-06-19 20:03:04 +02:00
|
|
|
}
|
2017-06-20 14:59:54 +02:00
|
|
|
for _, coll := range site.Collections {
|
|
|
|
n, err := site.WritePages(coll, options)
|
|
|
|
if err != nil {
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
count += n
|
2017-06-19 20:03:04 +02:00
|
|
|
}
|
2017-06-20 14:59:54 +02:00
|
|
|
n, err := site.WritePages(site, options)
|
|
|
|
return count + n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WritePages cleans the destination and create files in it.
|
|
|
|
// It attends to the global options.dry_run.
|
|
|
|
func (site *Site) WritePages(container PageContainer, options BuildOptions) (count int, err error) {
|
|
|
|
for _, page := range container.Pages() {
|
|
|
|
if page.Output() {
|
|
|
|
count++
|
|
|
|
if err = site.WritePage(page, options); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-13 18:03:49 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// WritePage writes a page to the destination directory.
|
2017-06-19 20:03:04 +02:00
|
|
|
func (site *Site) WritePage(page Page, options BuildOptions) error {
|
|
|
|
from := filepath.Join(site.Source, page.Path())
|
|
|
|
to := filepath.Join(site.Destination, page.Permalink())
|
2017-06-17 02:06:55 +02:00
|
|
|
if !page.Static() && filepath.Ext(to) == "" {
|
|
|
|
to = filepath.Join(to, "/index.html")
|
2017-06-13 18:03:49 +02:00
|
|
|
}
|
2017-06-18 22:55:52 +02:00
|
|
|
if options.Verbose {
|
|
|
|
fmt.Println("create", to, "from", page.Source())
|
|
|
|
}
|
|
|
|
if options.DryRun {
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-13 23:19:05 +02:00
|
|
|
// nolint: gas
|
2017-06-17 02:06:55 +02:00
|
|
|
if err := os.MkdirAll(filepath.Dir(to), 0755); err != nil {
|
2017-06-13 18:03:49 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch {
|
2017-06-17 16:51:32 +02:00
|
|
|
case page.Static() && options.UseHardLinks:
|
2017-06-17 02:06:55 +02:00
|
|
|
return os.Link(from, to)
|
2017-06-15 02:44:22 +02:00
|
|
|
case page.Static():
|
2017-06-17 02:11:52 +02:00
|
|
|
return helpers.CopyFileContents(to, from, 0644)
|
2017-06-13 18:03:49 +02:00
|
|
|
default:
|
2017-06-17 02:11:52 +02:00
|
|
|
return helpers.VisitCreatedFile(to, page.Write)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
}
|