func SpriteMap(mainctx context.Context, usv libsass.SassValue) (*libsass.SassValue, error) {
	var glob string
	var spacing libs.SassNumber
	err := libsass.Unmarshal(usv, &glob, &spacing)
	if err != nil {
		return nil, err
	}
	comp, err := libsass.CompFromCtx(mainctx)
	if err != nil {
		return nil, err
	}
	paths := comp.(libsass.Pather)
	imgs := sw.New(&sw.Options{
		ImageDir:  paths.ImgDir(),
		BuildDir:  paths.BuildDir(),
		GenImgDir: paths.ImgBuildDir(),
		Padding:   int(spacing.Value),
	})
	if cglob, err := strconv.Unquote(glob); err == nil {
		glob = cglob
	}
	key := glob + strconv.FormatInt(int64(spacing.Value), 10)
	loadctx := comp.Payload()
	sprites := payload.Sprite(loadctx)
	err = imgs.Decode(glob)
	if err != nil {
		return nil, err
	}
	_, err = imgs.Export()
	if err != nil {
		return nil, err
	}
	res, err := libsass.Marshal(key)
	if err != nil {
		return nil, err
	}
	sprites.Set(key, imgs)
	return &res, nil
}
SpriteMap returns a sprite from the passed glob and sprite
parameters.
package handlers
import (
	"errors"
	"fmt"
	"net/url"
	"path/filepath"
	"strconv"
	"strings"
	"golang.org/x/net/context"
	libsass "github.com/wellington/go-libsass"
	"github.com/wellington/go-libsass/libs"
	sw "github.com/wellington/spritewell"
	"github.com/wellington/wellington/payload"
)
func init() {
	libsass.RegisterSassFunc("sprite($map, $name, $offsetX: 0px, $offsetY: 0px)", Sprite)
	libsass.RegisterSassFunc("sprite-map($glob, $spacing: 0px)", SpriteMap)
	libsass.RegisterSassFunc("sprite-file($map, $name)", SpriteFile)
	libsass.RegisterSassFunc("sprite-position($map, $file)", SpritePosition)
	libsass.RegisterSassFunc("sprite-names($map)", SpriteNames)
}
func SpritePosition(mainctx context.Context, usv libsass.SassValue) (rsv *libsass.SassValue, err error) {
	comp, err := libsass.CompFromCtx(mainctx)
	if err != nil {
		return
	}
	var glob, name string
	err = libsass.Unmarshal(usv, &glob, &name)
	if err != nil {
		return
	}
	loadctx := comp.Payload()
	if loadctx == nil {
		err = ErrPayloadNil
		return
	}
	sprites := payload.Sprite(loadctx)
	if sprites == nil {
		err = errors.New("Sprites missing")
		return
	}
	imgs := sprites.Get(glob)
	if imgs == nil {
		err = fmt.Errorf(
			"Variable not found matching glob: %s sprite:%s", glob, name)
		return
	}
	if imgs.Lookup(name) == -1 {
		err = fmt.Errorf("image %s not found\n"+
			"   try one of these: %v", name, imgs.Paths())
		return
	}
	pos := imgs.GetPack(imgs.Lookup(name))
	if err != nil {
		return
	}
	x := libs.SassNumber{Unit: "px", Value: float64(-pos.X)}
	y := libs.SassNumber{Unit: "px", Value: float64(-pos.Y)}
	str, err := libsass.Marshal(
		[]libs.SassNumber{x, y},
	)
	return &str, err
}
func SpriteFile(ctx context.Context, usv libsass.SassValue) (rsv *libsass.SassValue, err error) {
	var glob, name string
	err = libsass.Unmarshal(usv, &glob, &name)
	if err != nil {
		return nil, err
	}
	infs := []interface{}{glob, name}
	res, err := libsass.Marshal(infs)
	return &res, err
}
func fileName(path string) string {
	return strings.TrimSuffix(path, filepath.Ext(path))
}
func SpriteNames(ctx context.Context, usv libsass.SassValue) (*libsass.SassValue, error) {
	comp, err := libsass.CompFromCtx(ctx)
	if err != nil {
		return nil, err
	}
	sprites := payload.Sprite(comp.Payload())
	var glob string
	if err := libsass.Unmarshal(usv, &glob); err != nil {
		return nil, err
	}
	imgs := sprites.Get(glob)
	if imgs == nil {
		return nil, fmt.Errorf("sprite-map (%s) not found", glob)
	}
	paths := imgs.Paths()
	for i := range paths {
		paths[i] = fileName(paths[i])
	}
	lst, err := libsass.Marshal(paths)
	return &lst, err
}
func Sprite(ctx context.Context, usv libsass.SassValue) (rsv *libsass.SassValue, err error) {
	comp, err := libsass.CompFromCtx(ctx)
	if err != nil {
		return nil, err
	}
	pather := comp.(libsass.Pather)
	var glob, name string
	var offsetX, offsetY libs.SassNumber
	err = libsass.Unmarshal(usv, &glob, &name, &offsetX, &offsetY)
	if err != nil {
		if err == libsass.ErrSassNumberNoUnit {
			err := fmt.Errorf(
				"Please specify unit for offset ie. (2px)")
			return nil, err
		}
		return nil, err
	}
	loadctx := comp.Payload()
	sprites := payload.Sprite(loadctx)
	imgs := sprites.Get(glob)
	if imgs == nil {
		err := fmt.Errorf(
			"Variable not found matching glob: %s sprite:%s", glob, name)
		return nil, err
	}
	path, err := imgs.OutputPath()
	if err != nil {
		return nil, err
	}
	buildDir := pather.BuildDir()
	genImgDir := pather.ImgBuildDir()
	httpPath := pather.HTTPPath()
	if len(httpPath) == 0 {
		ctxPath, err := filepath.Rel(buildDir, genImgDir)
		if err != nil {
			return nil, err
		}
		path = strings.Join([]string{ctxPath, filepath.Base(path)}, "/")
	} else {
		u, err := url.Parse(httpPath)
		if err != nil {
			return nil, err
		}
		u.Path = strings.Join([]string{u.Path, "build", filepath.Base(path)}, "/")
		path = u.String()
	}
	if err != nil {
		return nil, err
	}
	if imgs.Lookup(name) == -1 {
		return nil, fmt.Errorf("image %s not found\n"+
			"   try one of these: %v", name, imgs.Paths())
	}
	pos := imgs.GetPack(imgs.Lookup(name))
	if err != nil {
		return nil, err
	}
	x := libs.SassNumber{Unit: "px", Value: float64(-pos.X)}
	x = x.Add(offsetX)
	y := libs.SassNumber{Unit: "px", Value: float64(-pos.Y)}
	y = y.Add(offsetY)
	str, err := libsass.Marshal(
		fmt.Sprintf(`url("%s") %s %s`,
			path, x, y,
		))
	if err != nil {
		return nil, err
	}
	return &str, nil
}