コード例 #1
0
ファイル: blah.go プロジェクト: pcasaretto/go-libsass
func run(path string) {

	cheads := libs.SassMakeImporterList(1)

	gofc := libs.SassMakeFileContext(path)
	goopts := libs.SassFileContextGetOptions(gofc)
	libs.SassOptionSetCHeaders(goopts, cheads)

	libs.SassOptionSetOutputStyle(goopts, 2)
	// Set options
	libs.SassFileContextSetOptions(gofc, goopts)

	goctx := libs.SassFileContextGetContext(gofc)
	gocomp := libs.SassMakeFileCompiler(gofc)
	defer libs.SassDeleteCompiler(gocomp)

	libs.SassCompilerParse(gocomp)
	libs.SassCompilerExecute(gocomp)
	gostr := libs.SassContextGetOutputString(goctx)
	fmt.Println(gostr)
	errStatus := libs.SassContextGetErrorStatus(goctx)
	if errStatus > 0 {
		fmt.Println("error:", libs.SassContextGetErrorJSON(goctx))
	}
}
コード例 #2
0
ファイル: context.go プロジェクト: pcasaretto/go-libsass
func (ctx *Context) FileCompile(path string, out io.Writer) error {
	defer ctx.Reset()
	gofc := libs.SassMakeFileContext(path)
	goopts := libs.SassFileContextGetOptions(gofc)
	ctx.Init(goopts)
	//os.PathListSeparator
	incs := strings.Join(ctx.IncludePaths, string(os.PathListSeparator))
	libs.SassOptionSetIncludePath(goopts, incs)
	libs.SassFileContextSetOptions(gofc, goopts)
	gocc := libs.SassFileContextGetContext(gofc)
	gocompiler := libs.SassMakeFileCompiler(gofc)
	libs.SassCompilerParse(gocompiler)
	ctx.ResolvedImports = libs.GetImportList(gocc)
	libs.SassCompilerExecute(gocompiler)
	defer libs.SassDeleteCompiler(gocompiler)

	goout := libs.SassContextGetOutputString(gocc)
	io.WriteString(out, goout)
	ctx.Status = libs.SassContextGetErrorStatus(gocc)
	errJSON := libs.SassContextGetErrorJSON(gocc)
	// Yet another property for storing errors
	err := ctx.ProcessSassError([]byte(errJSON))
	if err != nil {
		return err
	}
	if ctx.Error() != "" {
		// TODO: this is weird, make something more idiomatic*/
		return errors.New(ctx.Error())
	}

	return nil
}
コード例 #3
0
ファイル: context.go プロジェクト: vinhhrv/go-libsass
// Compile reads in and writes the libsass compiled result to out.
// Options and custom functions are applied as specified in Context.
func (ctx *compctx) 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")
	}

	godc := libs.SassMakeDataContext(string(bs))
	goopts := libs.SassDataContextGetOptions(godc)
	libs.SassOptionSetSourceComments(goopts, true)

	ctx.Init(goopts)
	libs.SassDataContextSetOptions(godc, goopts)
	goctx := libs.SassDataContextGetContext(godc)
	ctx.context = goctx
	gocompiler := libs.SassMakeDataCompiler(godc)
	libs.SassCompilerParse(gocompiler)
	libs.SassCompilerExecute(gocompiler)
	if ctx.includeMap {
		libs.SassOptionSetSourceMapEmbed(goopts, true)
	}
	defer libs.SassDeleteCompiler(gocompiler)

	goout := libs.SassContextGetOutputString(goctx)
	io.WriteString(out, goout)

	ctx.Status = libs.SassContextGetErrorStatus(goctx)
	errJSON := libs.SassContextGetErrorJSON(goctx)
	err = ctx.ProcessSassError([]byte(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.err.Line >= 0 && i+ctx.err.Line < len(lines) {
				out += fmt.Sprintf("%s\n", string(lines[i+ctx.err.Line]))
			}
		}
		// TODO: this is weird, make something more idiomatic
		return errors.New(ctx.Error() + "\n" + out)
	}

	return nil
}
コード例 #4
0
func (ctx *compctx) FileCompile(path string, out io.Writer, srcmap io.Writer) error {
	defer ctx.Reset()
	gofc := libs.SassMakeFileContext(path)
	goopts := libs.SassFileContextGetOptions(gofc)
	ctx.Init(goopts)
	//os.PathListSeparator
	incs := strings.Join(ctx.IncludePaths, string(os.PathListSeparator))
	libs.SassOptionSetIncludePath(goopts, incs)

	libs.SassFileContextSetOptions(gofc, goopts)
	gocc := libs.SassFileContextGetContext(gofc)
	ctx.context = gocc
	gocompiler := libs.SassMakeFileCompiler(gofc)
	libs.SassCompilerParse(gocompiler)
	ctx.ResolvedImports = libs.GetImportList(gocc)
	libs.SassCompilerExecute(gocompiler)
	defer libs.SassDeleteCompiler(gocompiler)

	goout := libs.SassContextGetOutputString(gocc)
	if out == nil {
		return errors.New("out writer required")
	}
	_, err := io.WriteString(out, goout)
	if err != nil {
		return err
	}
	ctx.Status = libs.SassContextGetErrorStatus(gocc)
	errJSON := libs.SassContextGetErrorJSON(gocc)
	mapout := libs.SassContextGetSourceMapString(gocc)

	if srcmap != nil && ctx.includeMap && len(mapout) > 0 {
		_, err := io.WriteString(srcmap, mapout)
		if err != nil {
			return err
		}
	}
	// Yet another property for storing errors
	err = ctx.ProcessSassError([]byte(errJSON))
	if err != nil {
		return err
	}
	if ctx.Error() != "" {
		// TODO: this is weird, make something more idiomatic*/
		return errors.New(ctx.Error())
	}

	return nil
}