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

68 lines
1.4 KiB
Go
Raw Normal View History

package main
import (
"io/ioutil"
"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 {
if !page.Static {
2017-06-11 20:30:25 -04:00
p, err := readPage(page.Path, siteData)
2017-06-11 14:57:57 -04:00
if err != nil {
return err
}
page = p
}
2017-06-11 14:57:57 -04:00
// 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")
}
destPath := filepath.Join(siteConfig.DestinationDir, path)
2017-06-11 20:05:17 -04:00
if err := os.MkdirAll(filepath.Dir(destPath), 0777); err != nil {
return err
}
if page.Static {
2017-06-11 20:05:17 -04:00
if err := os.Link(filepath.Join(siteConfig.SourceDir, page.Path), destPath); err != nil {
return err
}
} else {
2017-06-10 17:51:46 -04:00
// fmt.Println("render", filepath.Join(siteConfig.SourceDir, page.Path), "->", destPath)
2017-06-11 20:30:25 -04:00
body, err := page.Render()
if err != nil {
return err
}
if err = ioutil.WriteFile(destPath, body, 0644); err != nil {
2017-06-11 20:05:17 -04:00
return err
}
}
}
return nil
}