1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 08:08:59 +01:00
gojekyll/build.go
2017-06-10 19:32:39 -04:00

59 lines
1.2 KiB
Go

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 {
if os.IsNotExist(err) {
return nil
}
return err
}
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 {
return err
}
return removeEmptyDirectories(siteConfig.DestinationDir)
}
func build() error {
err := cleanDirectory()
if err != nil {
return err
}
for _, page := range siteMap {
if !page.Static {
page, err = readFile(page.Path, siteData, true)
}
if err != nil {
return err
}
path := page.Permalink
// TODO only do this for MIME pages
if !page.Static && !strings.HasSuffix(path, ".html") {
path += "/index.html"
}
destPath := filepath.Join(siteConfig.DestinationDir, path)
os.MkdirAll(filepath.Dir(destPath), 0777)
if page.Static {
os.Link(filepath.Join(siteConfig.SourceDir, page.Path), destPath)
} else {
// fmt.Println("render", filepath.Join(siteConfig.SourceDir, page.Path), "->", destPath)
ioutil.WriteFile(destPath, page.Body, 0644)
}
}
return nil
}