forked from piranha/gostatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
211 lines (174 loc) · 3.92 KB
/
config.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// (c) 2012 Alexander Solovyov
// under terms of ISC license
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
)
type Command string
type CommandList []Command
type Rule struct {
Deps []string
Commands CommandList
}
type RuleMap map[string]*Rule
type SiteConfig struct {
Templates []string
Base string
Source string
Output string
Rules RuleMap
Other map[string]string
}
func NewSiteConfig(path string) (*SiteConfig, error) {
source, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
basepath, _ := filepath.Split(path)
cfg := &SiteConfig{
Rules: make(RuleMap),
Other: make(map[string]string),
Base: basepath,
}
indent := 0
level := 0
prefix := regexp.MustCompile("^[ \t]*")
comment := regexp.MustCompile(`[^\\]#`)
var current *Rule
for i, line := range strings.Split(string(source), "\n") {
// check indent
indnew := len(prefix.FindString(line))
switch {
case indnew > indent:
level += 1
case indnew < indent:
level -= 1
}
indent = indnew
// remove useless stuff from line
line = line[indent:]
commentloc := comment.FindIndex([]byte(line))
if commentloc != nil {
line = line[:commentloc[0]]
}
line = strings.Replace(line, "\\#", "#", -1)
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
// is this a constant declaration?
if level == 0 && strings.Index(line, "=") != -1 {
cfg.ParseVariable(basepath, line)
continue
}
// not a constant, then a Rule start?
if level == 0 {
current = cfg.ParseRule(line)
continue
}
if level == 1 {
if current == nil {
return nil, fmt.Errorf("Indent without rules, line %d", i+1)
}
current.ParseCommand(line)
continue
}
return nil, fmt.Errorf("Unhandled situation on line %d: %s",
i+1, line)
}
return cfg, nil
}
// *** Parsing methods
func (cfg *SiteConfig) ParseVariable(base string, line string) {
bits := TrimSplitN(line, "=", 2)
switch bits[0] {
case "TEMPLATES":
cfg.Templates = strings.Split(bits[1], " ")
for i, template := range cfg.Templates {
cfg.Templates[i] = filepath.Join(base, template)
}
case "SOURCE":
cfg.Source = filepath.Join(base, bits[1])
case "OUTPUT":
cfg.Output = filepath.Join(base, bits[1])
default:
cfg.Other[Capitalize(bits[0])] = bits[1]
}
}
func (cfg *SiteConfig) ParseRule(line string) *Rule {
bits := TrimSplitN(line, ":", 2)
deps := NonEmptySplit(bits[1], " ")
rd := &Rule{
Deps: deps,
Commands: make(CommandList, 0),
}
cfg.Rules[bits[0]] = rd
return rd
}
func (rule *Rule) ParseCommand(line string) {
rule.Commands = append(rule.Commands, Command(line))
}
// *** Traversing methods
func (cmd Command) Matches(prefix Command) bool {
return cmd == prefix || strings.HasPrefix(string(cmd), string(prefix)+" ")
}
func (cmd Command) MatchesAny(prefixes CommandList) bool {
for _, prefix := range prefixes {
if cmd.Matches(prefix) {
return true
}
}
return false
}
func (commands CommandList) MatchedIndex(prefix Command) int {
for i, cmd := range commands {
if cmd.Matches(prefix) {
return i
}
}
return -1
}
func (rule Rule) MatchedCommand(prefix Command) *Command {
i := rule.Commands.MatchedIndex(prefix)
if i == -1 {
return nil
}
return &rule.Commands[i]
}
func (rule *Rule) IsDep(page *Page) bool {
for _, dep := range rule.Deps {
matches, err := filepath.Match(dep, page.Source)
if err == nil && matches {
return true
}
}
return false
}
func (rules RuleMap) MatchedRule(path string) (string, *Rule) {
if rules[path] != nil {
return path, rules[path]
}
_, name := filepath.Split(path)
if rules[name] != nil {
return name, rules[name]
}
for pat, rule := range rules {
matched, err := filepath.Match(pat, path)
errhandle(err)
if matched {
return pat, rule
}
}
for pat, rule := range rules {
matched, err := filepath.Match(pat, name)
errhandle(err)
if matched {
return pat, rule
}
}
return "", nil
}