1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 21:17:47 +01:00
gojekyll/helpers.go
Oliver Steele b14c8c5b44 Initial
There was actually a day of work with some ~10 commits preceding this, with history lost due to a bug and a backup system failure.
2017-06-10 15:38:09 -04:00

68 lines
1.2 KiB
Go

package main
import (
"io/ioutil"
"os"
"path/filepath"
)
// alternative to http://left-pad.io
func leftPad(s string, n int) string {
ws := make([]byte, n)
for i := range ws {
ws[i] = ' '
}
return string(ws) + s
}
func postfixWalk(path string, walkFn filepath.WalkFunc) error {
files, err := ioutil.ReadDir(path)
if err != nil {
return err
}
for _, stat := range files {
if stat.IsDir() {
postfixWalk(filepath.Join(path, stat.Name()), walkFn)
}
}
info, err := os.Stat(path)
err = walkFn(path, info, err)
if err != nil {
return err
}
return nil
}
func removeEmptyDirectories(path string) error {
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
stat, err := os.Stat(path)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return nil
}
if stat.IsDir() {
err = os.Remove(path)
// TODO swallow the error if it's because the directory isn't
// empty. This can happen if there's an entry in _config.keepfiles
}
return err
}
return postfixWalk(path, walkFn)
}
func stringArrayToMap(strings []string) map[string]bool {
stringMap := map[string]bool{}
for _, s := range strings {
stringMap[s] = true
}
return stringMap
}