mirror of
https://github.com/danog/gojekyll.git
synced 2025-01-22 21:01:18 +01:00
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/osteele/gojekyll/helpers"
|
|
)
|
|
|
|
// Clean the destination. Remove files that aren't in keep_files, and resulting empty diretories.
|
|
// It attends to the global options.dry_run.
|
|
func (s *Site) Clean() error {
|
|
removeFiles := func(name string, info os.FileInfo, err error) error {
|
|
switch {
|
|
case err != nil && os.IsNotExist(err):
|
|
return nil
|
|
case err != nil:
|
|
return err
|
|
case info.IsDir():
|
|
return nil
|
|
case s.KeepFile(name):
|
|
return nil
|
|
case options.dryRun:
|
|
fmt.Println("rm", name)
|
|
default:
|
|
return os.Remove(name)
|
|
}
|
|
return nil
|
|
}
|
|
if err := filepath.Walk(s.Destination, removeFiles); err != nil {
|
|
return err
|
|
}
|
|
return helpers.RemoveEmptyDirectories(s.Destination)
|
|
}
|
|
|
|
// Build cleans the destination and create files in it.
|
|
// It attends to the global options.dry_run.
|
|
func (s *Site) Build() (count int, err error) {
|
|
if err = s.Clean(); err != nil {
|
|
return
|
|
}
|
|
for _, page := range s.Paths {
|
|
count++
|
|
if err = s.WritePage(page); err != nil {
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// WritePage writes a page to the destination directory.
|
|
func (s *Site) WritePage(page Page) error {
|
|
from := filepath.Join(s.Source, page.Path())
|
|
to := filepath.Join(s.Destination, page.Permalink())
|
|
if !page.Static() && filepath.Ext(to) == "" {
|
|
to = filepath.Join(to, "/index.html")
|
|
}
|
|
// nolint: gas
|
|
if err := os.MkdirAll(filepath.Dir(to), 0755); err != nil {
|
|
return err
|
|
}
|
|
switch {
|
|
case options.dryRun:
|
|
fmt.Println("create", to, "from", page.Source())
|
|
return nil
|
|
case page.Static() && options.useHardLinks:
|
|
return os.Link(from, to)
|
|
case page.Static():
|
|
return helpers.CopyFileContents(to, from, 0644)
|
|
default:
|
|
return helpers.VisitCreatedFile(to, page.Write)
|
|
}
|
|
}
|