-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.go
69 lines (65 loc) · 1.34 KB
/
features.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
package brief
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/scanner"
)
func (dec *Decoder) handleFeature() {
dec.Feature = strings.ToLower(dec.Feature)
switch dec.Feature {
case "include":
switch dec.ScanType {
case scanner.String, scanner.RawString:
dec.trimContentToken()
dec.includeFile(dec.Token)
}
default:
dec.Errorf("unknown brief feature %s", dec.Feature)
}
}
func (dec *Decoder) trimContentToken() {
switch dec.Token[0] {
case '"':
dec.Token = strings.Trim(dec.Token, "\"")
case '`':
dec.Token = strings.Trim(dec.Token, "`")
}
}
func (dec *Decoder) includeFile(filename string) {
if !filepath.IsAbs(filename) {
filename = filepath.Join(dec.Dir, filename)
}
if dec.Debug {
fmt.Println("*** include", filename)
}
file, err := os.Open(filename)
if err != nil {
dec.Error(err.Error())
return
}
defer file.Close()
dir := filepath.Dir(filename)
idec := NewDecoder(file, dec.Text.TabCount, dir)
idec.Debug = dec.Debug
idec.Padding = dec.indent()
nodes, err := idec.Decode()
if err != nil {
dec.Error(err.Error())
return
}
size := len(nodes)
if size == 0 {
if dec.Debug {
fmt.Println("*** include file was empty")
}
return
}
parent := dec.findParent(nodes[size-1].Indent)
if parent != nil {
parent.Body = append(parent.Body, nodes...)
return
}
dec.Roots = append(dec.Roots, nodes...)
}