示例#1
0
// Compile reads in and writes the libsass compiled result to out.
// Options and custom functions are applied as specified in Context.
func (ctx *Context) Compile(in io.Reader, out io.Writer) error {

	defer ctx.Reset()
	bs, err := ioutil.ReadAll(in)

	if err != nil {
		return err
	}
	if len(bs) == 0 {
		return errors.New("No input provided")
	}
	src := C.CString(string(bs))

	dc := C.sass_make_data_context(src)
	defer C.sass_delete_data_context(dc)

	options := C.sass_data_context_get_options(dc)
	opts := ctx.Init((*SassOptions)(options))

	// TODO: Manually free options memory without throwing
	// malloc errors
	// defer C.free(unsafe.Pointer(opts))
	C.sass_data_context_set_options(dc, opts)
	cc := C.sass_data_context_get_context(dc)
	compiler := C.sass_make_data_compiler(dc)

	C.sass_compiler_parse(compiler)
	C.sass_compiler_execute(compiler)
	defer C.sass_delete_compiler(compiler)

	cout := C.GoString(C.sass_context_get_output_string(cc))
	io.WriteString(out, cout)

	ctx.Status = int(C.sass_context_get_error_status(cc))
	errJSON := C.sass_context_get_error_json(cc)
	err = ctx.ProcessSassError([]byte(C.GoString(errJSON)))

	if err != nil {
		return err
	}

	if ctx.error() != "" {
		lines := bytes.Split(bs, []byte("\n"))
		var out string
		for i := -7; i < 7; i++ {
			if i+ctx.Errors.Line >= 0 && i+ctx.Errors.Line < len(lines) {
				out += fmt.Sprintf("%s\n", string(lines[i+ctx.Errors.Line]))
			}
		}
		// TODO: this is weird, make something more idiomatic
		return errors.New(ctx.error() + "\n" + out)
	}

	return nil
}
示例#2
0
func (c *Context) FileCompile(path string, out io.Writer) error {
	defer c.Reset()
	cpath := C.CString(path)
	fc := C.sass_make_file_context(cpath)
	defer C.sass_delete_file_context(fc)
	fcopts := C.sass_file_context_get_options(fc)
	goopts := (*SassOptions)(fcopts)
	opts := c.Init(goopts)
	//os.PathListSeparator
	incs := strings.Join(c.IncludePaths, string(os.PathListSeparator))
	C.sass_option_set_include_path(opts, C.CString(incs))
	C.sass_file_context_set_options(fc, opts)
	cc := C.sass_file_context_get_context(fc)
	compiler := C.sass_make_file_compiler(fc)
	C.sass_compiler_parse(compiler)
	c.ResolvedImports = GetImportList(cc)
	C.sass_compiler_execute(compiler)
	defer C.sass_delete_compiler(compiler)
	cout := C.GoString(C.sass_context_get_output_string(cc))
	io.WriteString(out, cout)
	c.Status = int(C.sass_context_get_error_status(cc))
	errJson := C.sass_context_get_error_json(cc)
	err := c.ProcessSassError([]byte(C.GoString(errJson)))
	if err != nil {
		return err
	}
	if c.error() != "" {
		/*lines := bytes.Split(bs, []byte("\n"))
		var out string
		for i := -7; i < 7; i++ {
			if i+c.Errors.Line >= 0 && i+c.Errors.Line < len(lines) {
				out += fmt.Sprintf("%s\n", string(lines[i+c.Errors.Line]))
			}
		}
		// TODO: this is weird, make something more idiomatic*/
		return errors.New(c.error())
	}

	return nil
}
示例#3
0
// SassDeleteCompiler frees memory for the Sass compiler
func SassDeleteCompiler(c SassCompiler) {
	C.sass_delete_compiler(c)
}
示例#4
0
文件: compiler.go 项目: go-gonzo/scss
func (c *compiler) Destroy() error {
	C.sass_delete_compiler((*C.struct_Sass_Compiler)(c))
	//XXX
	return nil
}