2017-06-24 20:00:19 +02:00
|
|
|
package pipelines
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CopySassFileIncludes copies sass partials into a temporary directory,
|
|
|
|
// removing initial underscores.
|
|
|
|
// TODO delete the temp directory when done
|
2017-06-24 19:30:01 +02:00
|
|
|
func (p *Pipeline) CopySassFileIncludes() error {
|
2017-06-16 19:47:11 +02:00
|
|
|
// TODO use libsass.ImportsOption instead?
|
2017-06-24 19:30:01 +02:00
|
|
|
if p.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-24 19:30:01 +02:00
|
|
|
p.sassTempDir = dir
|
2017-06-16 19:47:11 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 13:41:14 +02:00
|
|
|
src := filepath.Join(p.SourceDir, "_sass")
|
2017-06-24 19:30:01 +02:00
|
|
|
dst := p.sassTempDir
|
2017-06-22 01:17:44 +02:00
|
|
|
err := 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
|
|
|
})
|
2017-06-22 01:17:44 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2017-06-16 19:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SassIncludePaths returns an array of sass include directories.
|
2017-06-24 19:30:01 +02:00
|
|
|
func (p *Pipeline) SassIncludePaths() []string {
|
|
|
|
return []string{p.sassTempDir}
|
2017-06-16 19:47:11 +02:00
|
|
|
}
|
2017-06-19 04:48:33 +02:00
|
|
|
|
2017-06-22 15:36:39 +02:00
|
|
|
// WriteSass converts a SASS file and writes it to w.
|
2017-06-24 19:30:01 +02:00
|
|
|
func (p *Pipeline) WriteSass(w io.Writer, b []byte) error {
|
2017-06-22 15:36:39 +02:00
|
|
|
comp, err := libsass.New(w, bytes.NewBuffer(b))
|
2017-06-19 04:48:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-24 19:30:01 +02:00
|
|
|
err = comp.Option(libsass.IncludePaths(p.SassIncludePaths()))
|
2017-06-19 04:48:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return comp.Run()
|
|
|
|
}
|