1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-22 14:41:20 +01:00

Cache sass conversion

This commit is contained in:
Oliver Steele 2017-07-09 18:19:22 -04:00
parent 4dc06356e9
commit cacbf4894c
6 changed files with 41 additions and 20 deletions

View File

@ -105,7 +105,7 @@ These will probably not change:
Disable the cache by setting the environment variable `GOJEKYLL_DISABLE_CACHE=1`.
Disable threading by setting `GOMAXPROCS=1`.
The cache is for calls to Pygments (via the `highlight` tag). For sites, SASS (which is currently not cached) might have more overhead.
SASS conversion and Pygments (`highlight`) are cached.
### Feature Status

View File

@ -1,4 +1,4 @@
package tags
package cache
import (
"crypto/md5" // nolint: gas
@ -18,14 +18,14 @@ func init() {
}
}
// withFileCache looks (header, content) up in a user-specific file cache.
// WithFile looks (header, content) up in a user-specific file cache.
// If found, it writes the file contents. Else it calls fn to write to
// both the writer and the file system.
//
// header and content are distinct parameters to relieve the caller from
// having to concatenate them.
func withFileCache(header string, content string, fn func() (string, error)) (string, error) {
h := md5.New() // nolint: gas
func WithFile(header string, content string, fn func() (string, error)) (string, error) {
h := md5.New() // nolint: gas, noncrypto
io.WriteString(h, content) // nolint: errcheck, gas
io.WriteString(h, "\n") // nolint: errcheck, gas
io.WriteString(h, header) // nolint: errcheck, gas

View File

@ -26,6 +26,7 @@ type Pipeline struct {
config config.Config
liquidEngine liquid.Engine
sassTempDir string
sassHash string
}
// PipelineOptions configures a pipeline.

View File

@ -2,14 +2,16 @@ package pipelines
import (
"bytes"
"crypto/md5" // nolint: gas
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/dchest/cssmin"
"github.com/osteele/gojekyll/cache"
"github.com/osteele/gojekyll/utils"
libsass "github.com/wellington/go-libsass"
@ -28,6 +30,7 @@ func (p *Pipeline) CopySassFileIncludes() error {
p.sassTempDir = dir
}
h := md5.New() // nolint: gas, noncrypto
src := filepath.Join(p.SourceDir(), "_sass")
dst := p.sassTempDir
err := filepath.Walk(src, func(from string, info os.FileInfo, err error) error {
@ -36,11 +39,21 @@ func (p *Pipeline) CopySassFileIncludes() error {
}
rel := utils.MustRel(src, from)
to := filepath.Join(dst, strings.TrimPrefix(rel, "_"))
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
}
return utils.CopyFileContents(to, from, 0644)
})
if os.IsNotExist(err) {
return nil
}
p.sassHash = fmt.Sprintf("%x", h.Sum(nil))
return err
}
@ -50,20 +63,26 @@ func (p *Pipeline) SassIncludePaths() []string {
}
// WriteSass converts a SASS file and writes it to w.
func (p *Pipeline) WriteSass(w io.Writer, b []byte) (err error) {
buf := new(bytes.Buffer)
comp, err := libsass.New(buf, bytes.NewBuffer(b))
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 {
return err
}
err = comp.Option(libsass.IncludePaths(p.SassIncludePaths()))
if err != nil {
log.Fatal(err)
}
if err = comp.Run(); err != nil {
return err
}
b = cssmin.Minify(buf.Bytes())
_, err = w.Write(b)
_, err = w.Write([]byte(s))
return err
}

View File

@ -158,7 +158,7 @@ func (s *Site) initializeRenderingPipeline() (err error) {
}
s.pipeline, err = pipelines.NewPipeline(s.config, options)
if err != nil {
return nil
return err
}
engine := s.pipeline.TemplateEngine()
return s.runHooks(func(p plugins.Plugin) error {

View File

@ -7,6 +7,7 @@ import (
"os/exec"
"strings"
"github.com/osteele/gojekyll/cache"
"github.com/osteele/liquid/render"
)
@ -23,7 +24,7 @@ func highlightTag(ctx render.Context) (string, error) {
if err != nil {
return "", err
}
return withFileCache(fmt.Sprintf("pygments %s", args), s, func() (string, error) {
return cache.WithFile(fmt.Sprintf("pygments %s", args), s, func() (string, error) {
buf := new(bytes.Buffer)
cmd := exec.Command("pygmentize", cargs...) // nolint: gas
cmd.Stdin = strings.NewReader(s)