Пример #1
0
// InlineImage returns a base64 encoded png from the input image
func InlineImage(ctx *libsass.Context, usv libsass.UnionSassValue) libsass.UnionSassValue {
	var (
		name   string
		encode bool
		f      io.Reader
	)
	err := libsass.Unmarshal(usv, &name, &encode)
	if err != nil {
		return libsass.Error(err)
	}

	f, err = os.Open(filepath.Join(ctx.ImageDir, name))
	if err != nil {
		r, err := httpInlineImage(name)
		if err != nil {
			return libsass.Error(err)
		}
		f = r
		if r != nil {
			defer r.Close()
		}
	}

	if err != nil {
		return libsass.Error(err)
	}
	var buf bytes.Buffer

	sw.Inline(f, &buf, encode)
	res, err := libsass.Marshal(buf.String())
	if err != nil {
		return libsass.Error(err)
	}
	return res
}
Пример #2
0
// InlineImage returns a base64 encoded png from the input image
func InlineImage(mainctx context.Context, usv libsass.SassValue) (rsv *libsass.SassValue, err error) {
	var (
		name   string
		encode bool
		f      io.ReadCloser
	)
	comp, err := libsass.CompFromCtx(mainctx)
	if err != nil {
		return nil, err
	}

	err = libsass.Unmarshal(usv, &name, &encode)
	if err != nil {
		return nil, err
	}

	paths := comp.(libsass.Pather)

	// check for valid URL. If true, attempt to resolve image.
	// This is really going to slow down compilation think about
	// writing data to disk instead of inlining.
	u, err := url.Parse(name)
	if err == nil && len(u.Scheme) > 0 {
		f, err = imgResolver.Do(u.String())
	} else {
		f, err = os.Open(filepath.Join(paths.ImgDir(), name))
	}
	if err != nil {
		return nil, err
	}
	defer f.Close()

	var buf bytes.Buffer
	err = sw.Inline(f, &buf, encode)
	if err != nil {
		return nil, err
	}
	res, err := libsass.Marshal(buf.String())
	if err != nil {
		return nil, err
	}
	return &res, nil
}