1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-23 06:41:10 +01:00
gojekyll/build.go

67 lines
1.4 KiB
Go
Raw Normal View History

package main
import (
"os"
"path/filepath"
"strings"
)
func cleanDirectory() error {
removeFiles := func(path string, info os.FileInfo, err error) error {
if err != nil {
2017-06-10 17:51:46 -04:00
if os.IsNotExist(err) {
return nil
}
return err
}
2017-06-10 17:51:46 -04:00
if info.IsDir() {
return nil
}
// TODO check for inclusion in KeepFiles
err = os.Remove(path)
return err
}
if err := filepath.Walk(siteConfig.DestinationDir, removeFiles); err != nil {
2017-06-10 17:51:46 -04:00
return err
}
return removeEmptyDirectories(siteConfig.DestinationDir)
}
func build() error {
2017-06-11 14:57:57 -04:00
if err := cleanDirectory(); err != nil {
return err
}
2017-06-11 14:57:57 -04:00
for path, page := range siteMap {
// TODO don't do this for js, css, etc. pages
if !page.Static && !strings.HasSuffix(path, ".html") {
2017-06-11 14:57:57 -04:00
path = filepath.Join(path, "/index.html")
}
src := filepath.Join(siteConfig.SourceDir, page.Path)
dst := filepath.Join(siteConfig.DestinationDir, path)
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
2017-06-11 20:05:17 -04:00
return err
}
switch {
case page.Static && options.useHardLinks:
if err := os.Link(src, dst); err != nil {
return err
}
case page.Static:
if err := copyFile(dst, src, 0644); err != nil {
2017-06-11 20:05:17 -04:00
return err
}
default:
// fmt.Println("render", filepath.Join(siteConfig.SourceDir, page.Path), "->", dst)
f, err := os.Create(dst)
2017-06-11 20:30:25 -04:00
if err != nil {
return err
}
defer func() { _ = f.Close() }()
2017-06-11 20:51:01 -04:00
if err := page.Render(f); err != nil {
2017-06-11 20:05:17 -04:00
return err
}
}
}
return nil
}