2017-06-17 16:51:32 +02:00
|
|
|
package gojekyll
|
2017-06-16 19:47:11 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-06-17 02:11:52 +02:00
|
|
|
"github.com/osteele/gojekyll/helpers"
|
2017-06-17 01:17:22 +02:00
|
|
|
|
2017-06-16 19:47:11 +02:00
|
|
|
libsass "github.com/wellington/go-libsass"
|
|
|
|
)
|
|
|
|
|
2017-06-16 21:14:56 +02:00
|
|
|
// IsSassPath returns a boolean indicating whether the file is a Sass (".sass" or ".scss") file.
|
2017-06-17 02:06:55 +02:00
|
|
|
func (s *Site) IsSassPath(name string) bool {
|
|
|
|
return strings.HasSuffix(name, ".sass") || strings.HasSuffix(name, ".scss")
|
2017-06-16 21:14:56 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 19:47:11 +02:00
|
|
|
// CopySassFileIncludes copies sass partials into a temporary directory,
|
|
|
|
// removing initial underscores.
|
|
|
|
// TODO delete the temp directory when done
|
2017-06-18 22:54:36 +02:00
|
|
|
func (s *Site) CopySassFileIncludes() error {
|
2017-06-16 19:47:11 +02:00
|
|
|
// TODO use libsass.ImportsOption instead?
|
2017-06-16 22:44:09 +02:00
|
|
|
if s.sassTempDir == "" {
|
2017-06-18 22:54:36 +02:00
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "_sass")
|
2017-06-16 19:47:11 +02:00
|
|
|
if err != nil {
|
2017-06-18 22:54:36 +02:00
|
|
|
return err
|
2017-06-16 19:47:11 +02:00
|
|
|
}
|
2017-06-18 22:54:36 +02:00
|
|
|
s.sassTempDir = dir
|
2017-06-16 19:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
src := filepath.Join(s.Source, "_sass")
|
2017-06-16 22:44:09 +02:00
|
|
|
dst := s.sassTempDir
|
2017-06-18 22:54:36 +02:00
|
|
|
return filepath.Walk(src, func(from string, info os.FileInfo, err error) error {
|
2017-06-16 19:47:11 +02:00
|
|
|
if err != nil || info.IsDir() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rel, err := filepath.Rel(src, from)
|
|
|
|
if err != nil {
|
2017-06-18 22:54:36 +02:00
|
|
|
return err
|
2017-06-16 19:47:11 +02:00
|
|
|
}
|
2017-06-16 21:41:03 +02:00
|
|
|
to := filepath.Join(dst, strings.TrimPrefix(rel, "_"))
|
2017-06-17 02:11:52 +02:00
|
|
|
return helpers.CopyFileContents(to, from, 0644)
|
2017-06-16 19:47:11 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SassIncludePaths returns an array of sass include directories.
|
|
|
|
func (s *Site) SassIncludePaths() []string {
|
2017-06-16 22:44:09 +02:00
|
|
|
return []string{s.sassTempDir}
|
2017-06-16 19:47:11 +02:00
|
|
|
}
|
2017-06-19 04:48:33 +02:00
|
|
|
|
|
|
|
func (page *DynamicPage) writeSass(w io.Writer, data []byte) error {
|
|
|
|
comp, err := libsass.New(w, bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = comp.Option(libsass.IncludePaths(page.site.SassIncludePaths()))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return comp.Run()
|
|
|
|
}
|