2017-06-10 21:38:09 +02:00
|
|
|
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 23:51:46 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-06-10 23:51:46 +02:00
|
|
|
if info.IsDir() {
|
2017-06-10 21:38:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// TODO check for inclusion in KeepFiles
|
|
|
|
err = os.Remove(path)
|
|
|
|
return err
|
|
|
|
}
|
2017-06-11 01:32:39 +02:00
|
|
|
if err := filepath.Walk(siteConfig.DestinationDir, removeFiles); err != nil {
|
2017-06-10 23:51:46 +02:00
|
|
|
return err
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-11 01:32:39 +02:00
|
|
|
return removeEmptyDirectories(siteConfig.DestinationDir)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func build() error {
|
2017-06-11 20:57:57 +02:00
|
|
|
if err := cleanDirectory(); err != nil {
|
2017-06-10 21:38:09 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-06-11 20:57:57 +02:00
|
|
|
for path, page := range siteMap {
|
2017-06-10 21:38:09 +02:00
|
|
|
if !page.Static {
|
2017-06-11 20:57:57 +02:00
|
|
|
p, err := readFile(page.Path, siteData, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
page = p
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-11 20:57:57 +02:00
|
|
|
// TODO don't do this for js, css, etc. pages
|
2017-06-10 21:38:09 +02:00
|
|
|
if !page.Static && !strings.HasSuffix(path, ".html") {
|
2017-06-11 20:57:57 +02:00
|
|
|
path = filepath.Join(path, "/index.html")
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
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 {
|
2017-06-10 23:51:46 +02:00
|
|
|
// fmt.Println("render", filepath.Join(siteConfig.SourceDir, page.Path), "->", destPath)
|
2017-06-10 21:38:09 +02:00
|
|
|
ioutil.WriteFile(destPath, page.Body, 0644)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|