forked from wellington/wellington
/
import.go
81 lines (71 loc) · 1.54 KB
/
import.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package wellington
import (
"bufio"
"bytes"
"io"
"os"
"strings"
libsass "github.com/wellington/go-libsass"
)
type nopCloser struct {
io.Reader
}
func (n nopCloser) Close() error { return nil }
// readSass retrives a file from path. If found, it converts Sass
// to Scss or returns found Scss;
func readSass(path string) (io.ReadCloser, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
return ToScssReader(file)
}
// ToScssReader ...
func ToScssReader(r io.Reader) (io.ReadCloser, error) {
var (
buf bytes.Buffer
)
tr := io.TeeReader(r, &buf)
if IsSass(tr) {
var ibuf bytes.Buffer
libsass.ToScss(io.MultiReader(&buf, r), &ibuf)
return nopCloser{&ibuf}, nil
}
mr := io.MultiReader(&buf, r)
return nopCloser{mr}, nil
}
// IsSass determines if the given reader is Sass (not Scss).
// This is predicted by the presence of semicolons
func IsSass(r io.Reader) bool {
scanner := bufio.NewScanner(r)
var cmt bool
var last string
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())
last = text
// FIXME: This is not a suitable way to detect comments
if strings.HasPrefix(text, "/*") {
cmt = true
}
if strings.HasSuffix(text, "*/") {
cmt = false
continue
}
if cmt {
continue
}
if strings.HasSuffix(text, "{") ||
strings.HasSuffix(text, "}") {
return false
}
if strings.HasSuffix(text, ";") {
return false
}
}
// If type is still undecided and file ends with comment, assume
// this is a scss file
if strings.HasSuffix(last, "*/") {
return false
}
return true
}