1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-23 05:11:31 +01:00

89 lines
2.1 KiB
Go
Raw Normal View History

2017-06-24 14:00:19 -04:00
package pipelines
2017-06-16 13:47:11 -04:00
import (
"bytes"
2017-07-09 18:19:22 -04:00
"crypto/md5" // nolint: gas
"fmt"
2017-06-16 13:47:11 -04:00
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
2017-07-09 15:21:11 -04:00
"github.com/dchest/cssmin"
2017-07-09 18:19:22 -04:00
"github.com/osteele/gojekyll/cache"
2017-07-09 16:17:20 -04:00
"github.com/osteele/gojekyll/utils"
2017-06-16 19:17:22 -04:00
2017-06-16 13:47:11 -04:00
libsass "github.com/wellington/go-libsass"
)
// CopySassFileIncludes copies sass partials into a temporary directory,
// removing initial underscores.
// TODO delete the temp directory when done
2017-06-24 13:30:01 -04:00
func (p *Pipeline) CopySassFileIncludes() error {
2017-06-16 13:47:11 -04:00
// TODO use libsass.ImportsOption instead?
2017-06-24 13:30:01 -04:00
if p.sassTempDir == "" {
2017-06-18 16:54:36 -04:00
dir, err := ioutil.TempDir(os.TempDir(), "_sass")
2017-06-16 13:47:11 -04:00
if err != nil {
2017-06-18 16:54:36 -04:00
return err
2017-06-16 13:47:11 -04:00
}
2017-06-24 13:30:01 -04:00
p.sassTempDir = dir
2017-06-16 13:47:11 -04:00
}
2017-07-09 18:19:22 -04:00
h := md5.New() // nolint: gas, noncrypto
2017-06-30 21:06:12 -04:00
src := filepath.Join(p.SourceDir(), "_sass")
2017-06-24 13:30:01 -04:00
dst := p.sassTempDir
2017-06-21 19:17:44 -04:00
err := filepath.Walk(src, func(from string, info os.FileInfo, err error) error {
2017-06-16 13:47:11 -04:00
if err != nil || info.IsDir() {
return err
}
2017-07-09 16:17:20 -04:00
rel := utils.MustRel(src, from)
2017-06-16 15:41:03 -04:00
to := filepath.Join(dst, strings.TrimPrefix(rel, "_"))
2017-07-09 18:19:22 -04:00
in, err := os.Open(from)
if err != nil {
return err
}
defer in.Close() // nolint: errcheck
_, err = io.Copy(h, in)
if err != nil {
return err
}
2017-07-09 16:17:20 -04:00
return utils.CopyFileContents(to, from, 0644)
2017-06-16 13:47:11 -04:00
})
2017-06-21 19:17:44 -04:00
if os.IsNotExist(err) {
return nil
}
2017-07-09 18:19:22 -04:00
p.sassHash = fmt.Sprintf("%x", h.Sum(nil))
2017-06-21 19:17:44 -04:00
return err
2017-06-16 13:47:11 -04:00
}
// SassIncludePaths returns an array of sass include directories.
2017-06-24 13:30:01 -04:00
func (p *Pipeline) SassIncludePaths() []string {
return []string{p.sassTempDir}
2017-06-16 13:47:11 -04:00
}
// WriteSass converts a SASS file and writes it to w.
2017-07-09 18:19:22 -04:00
func (p *Pipeline) WriteSass(w io.Writer, b []byte) error {
s, err := cache.WithFile(fmt.Sprintf("sass: %s", p.sassHash), string(b), func() (s string, err error) {
buf := new(bytes.Buffer)
comp, err := libsass.New(buf, bytes.NewBuffer(b))
if err != nil {
return "", err
}
err = comp.Option(libsass.IncludePaths(p.SassIncludePaths()))
if err != nil {
return "", err
}
if err = comp.Run(); err != nil {
return "", err
}
b = cssmin.Minify(buf.Bytes())
return string(b), nil
})
if err != nil {
2017-07-09 15:21:11 -04:00
return err
}
2017-07-15 21:25:35 -04:00
_, err = io.WriteString(w, s)
2017-07-09 15:21:11 -04:00
return err
}