-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
49 lines (41 loc) · 851 Bytes
/
parse.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
package main
import (
"bytes"
"io/ioutil"
"path/filepath"
)
var shebang = []byte("#!")
func Parse(usehtml bool, e, left, right string, fs map[string]interface{}, files ...string) (t template, err error) {
new := htmlOrText[usehtml]
if e != "" {
t = new("", left, right, fs)
if t, err = t.Parse(e); err != nil {
return nil, err
}
}
for _, file := range files {
b, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
if len(b) > 2 && bytes.Equal(b[:3], shebang) {
if i := bytes.IndexAny(b, "\n"); i > 0 && len(b) != i {
b = b[i+1:]
} else {
b = nil
}
}
name := filepath.Base(file)
var tmpl template
if t == nil {
t = new(name, left, right, fs)
tmpl = t
} else {
tmpl = t.New(name)
}
if _, err = tmpl.Parse(string(b)); err != nil {
return nil, err
}
}
return
}