1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 06:24:38 +01:00
gojekyll/site/data.go

46 lines
953 B
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package site
2017-06-29 17:00:59 +02:00
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
2017-07-09 22:17:20 +02:00
"github.com/osteele/gojekyll/utils"
2017-06-29 17:00:59 +02:00
)
func (s *Site) readDataFiles() error {
s.data = map[string]interface{}{}
dataDir := filepath.Join(s.SourceDir(), s.config.DataDir)
2017-06-29 17:00:59 +02:00
files, err := ioutil.ReadDir(dataDir)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
for _, f := range files {
if f.IsDir() {
break
}
filename := filepath.Join(dataDir, f.Name())
switch filepath.Ext(f.Name()) {
case ".csv", ".json":
return fmt.Errorf("unimplemented reading %s", filepath.Ext(f.Name()))
case ".yaml", ".yml":
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
var d interface{} // map or slice
2017-07-09 22:17:20 +02:00
err = utils.UnmarshalYAMLInterface(b, &d)
2017-06-29 17:00:59 +02:00
if err != nil {
2017-07-09 22:17:20 +02:00
return utils.PathError(err, "read YAML", filename)
2017-06-29 17:00:59 +02:00
}
2017-07-09 22:17:20 +02:00
basename := utils.TrimExt(filepath.Base(f.Name()))
2017-06-29 17:00:59 +02:00
s.data[basename] = d
}
}
return nil
}