-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplatedescriptor.go
131 lines (100 loc) · 2.76 KB
/
templatedescriptor.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package trinity
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
var (
allCharExceptCloseBrace = "[^}]*"
// Name of define with options
ViewOptionsTemplate = "ViewOptions"
regexpOptionsTepmplate = regexp.MustCompile("{{" + allCharExceptCloseBrace + ViewOptionsTemplate + allCharExceptCloseBrace + "}}")
regexpEnd = regexp.MustCompile("{{\\s*end\\s*}}")
MasterPageOption = "MasterPage"
AdditionalTemplateOption = "AdditionalTemplate"
)
// templateDescriptor stores the options section for a template
type templateDescriptor struct {
templatePath string
additionalTemplates []string
masterPage string
}
// newTemplateDescriptor creates a new templateDescriptor object
func newTemplateDescriptor(viewsFolder string, templatePath string) (*templateDescriptor, error) {
logger.Trace("")
template := new(templateDescriptor)
template.templatePath = templatePath
template.additionalTemplates = make([]string, 0)
err := template.parseOptions(viewsFolder)
if err != nil {
return nil, err
}
return template, nil
}
func (template *templateDescriptor) parseOptions(viewsFolder string) error {
file, err := os.Open(template.templatePath)
if err != nil {
return err
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
optionLines := template.extractOptionLines(string(bytes))
for _, line := range optionLines {
logger.Debugf("line: %s", line)
if strings.HasPrefix(line, "{") {
break
}
vals := strings.Split(line, "=")
if len(vals) != 2 {
continue
}
switch vals[0] {
case MasterPageOption:
{
template.masterPage = filepath.Join(viewsFolder, vals[1])
break
}
case AdditionalTemplateOption:
{
logger.Debugf("add template: %s", vals[1])
template.additionalTemplates = append(template.additionalTemplates, filepath.Join(viewsFolder, vals[1]))
break
}
}
}
return nil
}
func (template *templateDescriptor) extractOptionLines(text string) []string {
logger.Trace("")
emptyResult := make([]string, 0)
indexes := regexpOptionsTepmplate.FindAllStringIndex(text, -1)
logger.Debugf("indexes: %v", indexes)
if len(indexes) == 0 {
return emptyResult
}
indexesEnds := regexpEnd.FindAllStringIndex(text, -1)
logger.Debugf("indexesEnds: %v", indexesEnds)
if len(indexes) == 0 {
return emptyResult
}
contentStartsAt := indexes[0][1]
contentEndsAt := 0
for _, indexEnd := range indexesEnds {
if indexEnd[0] < contentStartsAt {
continue
}
if (contentEndsAt == 0) || (contentEndsAt > indexEnd[0]) {
contentEndsAt = indexEnd[0]
}
}
if contentEndsAt == 0 {
return emptyResult
}
result := make([]string, 0)
for _, line := range strings.Split(text[contentStartsAt:contentEndsAt], "\n") {
result = append(result, strings.TrimSpace(line))
}
return result
}