forked from piranha/gostatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.go
309 lines (255 loc) · 5.75 KB
/
page.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// (c) 2012 Alexander Solovyov
// under terms of ISC license
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
)
const (
StateUnknown = iota
StateChanged
StateUnchanged
StateIgnored
)
type Page struct {
PageHeader
Site *Site `json:"-"`
Rule *Rule
Pattern string
Deps PageSlice `json:"-"`
Source string
Path string
ModTime time.Time
processed bool
state int
content string
wasread bool // if content was read already
}
type PageSlice []*Page
func NewPage(site *Site, path string) *Page {
stat, err := os.Stat(path)
errhandle(err)
relpath, err := filepath.Rel(site.Source, path)
errhandle(err)
pattern, rule := site.Rules.MatchedRule(relpath)
page := &Page{
Site: site,
Rule: rule,
Pattern: pattern,
Source: relpath,
Path: relpath,
ModTime: stat.ModTime(),
}
page.peek()
debug("Found page: %s; rule: %v\n",
page.Source, page.Rule)
return page
}
func (page *Page) Content() string {
if !page.wasread {
content, err := ioutil.ReadFile(page.FullPath())
errhandle(err)
page.SetContent(string(content))
page.wasread = true
}
return page.content
}
func (page *Page) SetContent(content string) {
page.content = content
}
func (page *Page) FullPath() string {
return filepath.Join(page.Site.Source, page.Source)
}
func (page *Page) OutputPath() string {
return filepath.Join(page.Site.Output, page.Path)
}
func (page *Page) Url() string {
url := strings.Replace(page.Path, string(filepath.Separator), "/", -1)
if url == "index.html" {
return ""
}
if strings.HasSuffix(url, "/index.html") {
return strings.TrimSuffix(url, "/index.html") + "/"
}
return url
}
func (page *Page) UrlTo(other *Page) string {
return page.Rel(other.Url())
}
func (page *Page) Rel(path string) string {
root := strings.Repeat("../", strings.Count(page.Url(), "/"))
if path[0] == '/' {
return root + path[1:]
}
return root + path
}
func (page *Page) Is(path string) bool {
return page.Url() == path || page.Path == path
}
// Peek is used to run those processors which should be done before others can
// find out about us. Two actual examples include 'config' and 'rename'
// processors right now.
func (page *Page) peek() {
if page.Rule == nil {
return
}
for _, name := range PreProcessors {
cmd := page.Rule.MatchedCommand(name)
if cmd != nil {
ProcessCommand(page, cmd)
}
}
}
func (page *Page) findDeps() {
if page.Rule == nil {
return
}
deps := make(PageSlice, 0)
for _, other := range page.Site.Pages {
if other != page && page.Rule.IsDep(other) {
deps = append(deps, other)
}
}
page.Deps = deps
}
func (page *Page) Changed() bool {
if opts.Force {
return true
}
if page.state == StateUnknown {
page.state = StateUnchanged
dest, err := os.Stat(page.OutputPath())
if err != nil || dest.ModTime().Before(page.ModTime) {
page.state = StateChanged
} else {
for _, dep := range page.Deps {
if dep.Changed() {
page.state = StateChanged
}
}
}
}
return page.state == StateChanged
}
func (page *Page) Process() *Page {
if page.processed || page.Rule == nil {
return page
}
page.processed = true
if page.Rule.Commands != nil {
for _, cmd := range page.Rule.Commands {
if !cmd.MatchesAny(PreProcessors) {
ProcessCommand(page, &cmd)
}
}
}
return page
}
func (page *Page) WriteTo(writer io.Writer) (n int64, err error) {
if page.Rule == nil {
return 0, nil
}
if !page.processed {
page.Process()
}
nint, err := writer.Write([]byte(page.Content()))
return int64(nint), err
}
func (page *Page) Render() (n int64, err error) {
if page.Rule == nil {
return CopyFile(page.FullPath(), page.OutputPath())
}
file, err := os.Create(page.OutputPath())
if err != nil {
return 0, err
}
defer file.Close()
return page.WriteTo(file)
}
func (page *Page) UrlMatches(regex string) bool {
re, err := regexp.Compile(regex)
if err != nil {
errhandle(fmt.Errorf("Incorrect regex given to Page.UrlMatches: '%s' ", regex))
}
return re.Match([]byte(page.Url()))
}
// PageSlice manipulation
func (pages PageSlice) Get(i int) *Page { return pages[i] }
func (pages PageSlice) First() *Page { return pages.Get(0) }
func (pages PageSlice) Last() *Page { return pages.Get(len(pages) - 1) }
func (pages PageSlice) Slice(from int, to int) PageSlice {
length := len(pages)
if from > length {
from = length
}
if to > length {
to = length
}
return pages[from:to]
}
func (pages PageSlice) Len() int {
return len(pages)
}
func (pages PageSlice) Less(i, j int) bool {
left := pages.Get(i)
right := pages.Get(j)
if left.Date.Unix() == right.Date.Unix() {
return left.ModTime.Unix() < right.ModTime.Unix()
}
return left.Date.Unix() > right.Date.Unix()
}
func (pages PageSlice) Swap(i, j int) {
pages[i], pages[j] = pages[j], pages[i]
}
func (pages PageSlice) Sort() {
sort.Sort(pages)
}
func (pages PageSlice) Children(root string) *PageSlice {
children := make(PageSlice, 0)
for _, page := range pages {
if strings.HasPrefix(page.Url(), root) && page.Url() != root {
children = append(children, page)
}
}
return &children
}
func (pages PageSlice) WithTag(tag string) *PageSlice {
tagged := make(PageSlice, 0)
for _, page := range pages {
if page.Tags != nil && SliceStringIndexOf(page.Tags, tag) != -1 {
tagged = append(tagged, page)
}
}
return &tagged
}
func (pages PageSlice) HasPage(check func(page *Page) bool) bool {
for _, page := range pages {
if check(page) {
return true
}
}
return false
}
func (pages PageSlice) BySource(s string) *Page {
for _, page := range pages {
if page.Source == s {
return page
}
}
return nil
}
func (pages PageSlice) ByPath(s string) *Page {
for _, page := range pages {
if page.Path == s {
return page
}
}
return nil
}