File: main.go

package info (click to toggle)
golang-github-wellington-go-libsass 0.9.2%2Bgit20181130.4ef5b9d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,128 kB
  • sloc: cpp: 28,607; ansic: 839; makefile: 44
file content (53 lines) | stat: -rw-r--r-- 1,140 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main

import (
	"bytes"
	"crypto/rand"
	"fmt"
	"log"
	"os"

	"golang.org/x/net/context"

	"github.com/wellington/go-libsass"
)

// cryptotext is of type libsass.SassFunc. As libsass compiles the
// source Sass, it will look for `crypto()` then call this function.
func cryptotext(ctx context.Context, usv libsass.SassValue) (*libsass.SassValue, error) {

	c := 10
	b := make([]byte, c)
	_, err := rand.Read(b)
	if err != nil {
		return nil, err
	}
	res, err := libsass.Marshal(fmt.Sprintf("'%x'", b))
	return &res, err
}

func main() {
	// Register a custom Sass func crypto
	libsass.RegisterSassFunc("crypto()", cryptotext)

	// Input Sass source
	input := `div { text: crypto(); }`

	buf := bytes.NewBufferString(input)
	// Starts a compiler writing to Stdout and reading from
	// a bytes buffer of the input source
	comp, err := libsass.New(os.Stdout, buf)
	if err != nil {
		log.Fatal(err)
	}

	// Run() kicks off the compiler and instructs libsass how
	// to read our input, handling the output from libsass
	if err := comp.Run(); err != nil {
		log.Fatal(err)
	}

	// Output:
	// div {
	//   text: 'c91db27d5e580ef4292e'; }
}