1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-23 00:01:14 +01:00
gojekyll/sass.go

73 lines
1.7 KiB
Go
Raw Normal View History

2017-06-16 13:47:11 -04:00
package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
2017-06-16 20:11:52 -04:00
"github.com/osteele/gojekyll/helpers"
2017-06-16 19:17:22 -04:00
2017-06-16 13:47:11 -04:00
libsass "github.com/wellington/go-libsass"
)
// IsSassPath returns a boolean indicating whether the file is a Sass (".sass" or ".scss") file.
2017-06-16 20:06:55 -04:00
func (s *Site) IsSassPath(name string) bool {
return strings.HasSuffix(name, ".sass") || strings.HasSuffix(name, ".scss")
}
2017-06-16 13:47:11 -04:00
func (p *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(p.site.SassIncludePaths()))
2017-06-16 13:47:11 -04:00
if err != nil {
log.Fatal(err)
}
return comp.Run()
}
// CopySassFileIncludes copies sass partials into a temporary directory,
// removing initial underscores.
// TODO delete the temp directory when done
func (s *Site) CopySassFileIncludes() {
// TODO use libsass.ImportsOption instead?
if s.sassTempDir == "" {
2017-06-16 13:47:11 -04:00
d, err := ioutil.TempDir(os.TempDir(), "_sass")
if err != nil {
panic(err)
}
s.sassTempDir = d
2017-06-16 13:47:11 -04:00
}
src := filepath.Join(s.Source, "_sass")
dst := s.sassTempDir
2017-06-16 13:47:11 -04:00
err := filepath.Walk(src, func(from string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
rel, err := filepath.Rel(src, from)
if err != nil {
panic(err)
}
2017-06-16 15:41:03 -04:00
to := filepath.Join(dst, strings.TrimPrefix(rel, "_"))
2017-06-16 20:11:52 -04:00
return helpers.CopyFileContents(to, from, 0644)
2017-06-16 13:47:11 -04:00
})
if err != nil {
panic(err)
}
}
// SassIncludePaths returns an array of sass include directories.
func (s *Site) SassIncludePaths() []string {
if s.sassTempDir == "" {
s.CopySassFileIncludes()
2017-06-16 13:47:11 -04:00
}
s.CopySassFileIncludes()
return []string{s.sassTempDir}
2017-06-16 13:47:11 -04:00
}