This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
goldsmith.go
107 lines (85 loc) · 2.34 KB
/
goldsmith.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
// Package goldsmith generates static websites.
package goldsmith
import (
"fmt"
"sync"
)
// Goldsmith chainable context.
type Goldsmith struct {
sourceDir string
targetDir string
contexts []*Context
cache *cache
filters filterStack
clean bool
index int
errors []error
mutex sync.Mutex
}
// Begin starts a chain, reading the files located in the source directory as input.
func Begin(sourceDir string) *Goldsmith {
goldsmith := &Goldsmith{sourceDir: sourceDir}
goldsmith.Chain(&loader{})
return goldsmith
}
// Cache enables caching in cacheDir for the remainder of the chain.
func (self *Goldsmith) Cache(cacheDir string) *Goldsmith {
self.cache = &cache{cacheDir}
return self
}
// Clean enables or disables removal of leftover files in the target directory.
func (self *Goldsmith) Clean(clean bool) *Goldsmith {
self.clean = clean
return self
}
// Chain links a plugin instance into the chain.
func (self *Goldsmith) Chain(plugin Plugin) *Goldsmith {
context := &Context{
goldsmith: self,
plugin: plugin,
filtersExt: append(filterStack(nil), self.filters...),
index: self.index,
filesOut: make(chan *File),
}
if len(self.contexts) > 0 {
context.filesIn = self.contexts[len(self.contexts)-1].filesOut
}
self.contexts = append(self.contexts, context)
self.index++
return self
}
// FilterPush pushes a filter instance on the chain's filter stack.
func (self *Goldsmith) FilterPush(filter Filter) *Goldsmith {
self.filters.push(filter, self.index)
self.index++
return self
}
// FilterPop pops a filter instance from the chain's filter stack.
func (self *Goldsmith) FilterPop() *Goldsmith {
self.filters.pop()
self.index++
return self
}
// End stops a chain, writing all recieved files to targetDir as output.
func (self *Goldsmith) End(targetDir string) []error {
self.targetDir = targetDir
self.Chain(&saver{clean: self.clean})
for _, context := range self.contexts {
go context.step()
}
context := self.contexts[len(self.contexts)-1]
for range context.filesOut {
}
return self.errors
}
func (self *Goldsmith) fault(name string, file *File, err error) {
self.mutex.Lock()
defer self.mutex.Unlock()
var faultError error
if file == nil {
faultError = fmt.Errorf("[%s]: %w", name, err)
} else {
faultError = fmt.Errorf("[%s@%v]: %w", name, file, err)
}
self.errors = append(self.errors, faultError)
}