2019-04-07 20:43:25 +00:00
|
|
|
// Package goldsmith generates static websites.
|
2015-10-29 09:24:47 +00:00
|
|
|
package goldsmith
|
|
|
|
|
2015-10-29 14:26:43 +00:00
|
|
|
import (
|
2020-08-13 04:03:49 +00:00
|
|
|
"fmt"
|
2018-12-08 19:18:51 +00:00
|
|
|
"hash"
|
|
|
|
"hash/crc32"
|
|
|
|
"sync"
|
2015-10-29 14:26:43 +00:00
|
|
|
)
|
|
|
|
|
2019-04-07 20:43:25 +00:00
|
|
|
// Goldsmith chainable context.
|
2018-12-08 19:18:51 +00:00
|
|
|
type Goldsmith struct {
|
|
|
|
sourceDir string
|
|
|
|
targetDir string
|
|
|
|
|
|
|
|
contexts []*Context
|
|
|
|
contextHasher hash.Hash32
|
|
|
|
|
2021-04-12 02:13:59 +00:00
|
|
|
fileCache *cache
|
2021-04-25 15:56:07 +00:00
|
|
|
filters filterStack
|
|
|
|
clean bool
|
2019-04-07 20:46:08 +00:00
|
|
|
|
2018-12-10 01:00:46 +00:00
|
|
|
errors []error
|
|
|
|
mutex sync.Mutex
|
2016-01-13 03:21:30 +00:00
|
|
|
}
|
2015-12-18 11:30:14 +00:00
|
|
|
|
2019-04-07 20:46:08 +00:00
|
|
|
// Begin starts a chain, reading the files located in the source directory as input.
|
2018-12-08 19:18:51 +00:00
|
|
|
func Begin(sourceDir string) *Goldsmith {
|
2018-12-10 01:00:46 +00:00
|
|
|
goldsmith := &Goldsmith{
|
2018-12-08 19:18:51 +00:00
|
|
|
sourceDir: sourceDir,
|
|
|
|
contextHasher: crc32.NewIEEE(),
|
|
|
|
}
|
|
|
|
|
2021-04-25 06:03:12 +00:00
|
|
|
goldsmith.Chain(&loader{})
|
2018-12-10 01:00:46 +00:00
|
|
|
return goldsmith
|
2015-12-18 04:14:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 20:43:25 +00:00
|
|
|
// Cache enables caching in cacheDir for the remainder of the chain.
|
2018-12-10 01:00:46 +00:00
|
|
|
func (goldsmith *Goldsmith) Cache(cacheDir string) *Goldsmith {
|
|
|
|
goldsmith.fileCache = &cache{cacheDir}
|
|
|
|
return goldsmith
|
2015-10-29 14:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 20:46:08 +00:00
|
|
|
// Clean enables or disables removal of leftover files in the target directory.
|
|
|
|
func (goldsmith *Goldsmith) Clean(clean bool) *Goldsmith {
|
|
|
|
goldsmith.clean = clean
|
|
|
|
return goldsmith
|
|
|
|
}
|
|
|
|
|
2019-04-07 20:43:25 +00:00
|
|
|
// Chain links a plugin instance into the chain.
|
2018-12-10 01:00:46 +00:00
|
|
|
func (goldsmith *Goldsmith) Chain(plugin Plugin) *Goldsmith {
|
|
|
|
goldsmith.contextHasher.Write([]byte(plugin.Name()))
|
2015-11-02 09:07:34 +00:00
|
|
|
|
2018-12-08 19:18:51 +00:00
|
|
|
context := &Context{
|
2021-04-25 15:56:07 +00:00
|
|
|
goldsmith: goldsmith,
|
|
|
|
plugin: plugin,
|
|
|
|
chainHash: goldsmith.contextHasher.Sum32(),
|
|
|
|
filesOut: make(chan *File),
|
2016-06-11 23:24:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 15:56:07 +00:00
|
|
|
context.filtersExt = append(context.filtersExt, goldsmith.filters...)
|
2016-06-12 02:38:17 +00:00
|
|
|
|
2018-12-10 01:00:46 +00:00
|
|
|
if len(goldsmith.contexts) > 0 {
|
2021-04-25 15:56:07 +00:00
|
|
|
context.filesIn = goldsmith.contexts[len(goldsmith.contexts)-1].filesOut
|
2015-11-02 09:23:13 +00:00
|
|
|
}
|
2016-06-11 23:24:06 +00:00
|
|
|
|
2018-12-10 01:00:46 +00:00
|
|
|
goldsmith.contexts = append(goldsmith.contexts, context)
|
|
|
|
return goldsmith
|
2015-11-02 09:23:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 20:43:25 +00:00
|
|
|
// FilterPush pushes a filter instance on the chain's filter stack.
|
2018-12-10 01:00:46 +00:00
|
|
|
func (goldsmith *Goldsmith) FilterPush(filter Filter) *Goldsmith {
|
2021-04-12 02:13:59 +00:00
|
|
|
goldsmith.filters.push(filter)
|
2018-12-10 01:00:46 +00:00
|
|
|
return goldsmith
|
2015-10-31 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 20:43:25 +00:00
|
|
|
// FilterPop pops a filter instance from the chain's filter stack.
|
2018-12-10 01:00:46 +00:00
|
|
|
func (goldsmith *Goldsmith) FilterPop() *Goldsmith {
|
2021-04-12 02:13:59 +00:00
|
|
|
goldsmith.filters.pop()
|
2018-12-10 01:00:46 +00:00
|
|
|
return goldsmith
|
2015-12-18 04:37:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 20:43:25 +00:00
|
|
|
// End stops a chain, writing all recieved files to targetDir as output.
|
2018-12-10 01:00:46 +00:00
|
|
|
func (goldsmith *Goldsmith) End(targetDir string) []error {
|
|
|
|
goldsmith.targetDir = targetDir
|
2018-12-08 19:18:51 +00:00
|
|
|
|
2021-04-25 06:03:12 +00:00
|
|
|
goldsmith.Chain(&saver{
|
2021-04-25 15:56:07 +00:00
|
|
|
clean: goldsmith.clean,
|
2021-04-25 06:03:12 +00:00
|
|
|
})
|
|
|
|
|
2018-12-10 01:00:46 +00:00
|
|
|
for _, context := range goldsmith.contexts {
|
2018-12-08 19:18:51 +00:00
|
|
|
go context.step()
|
2016-08-21 19:54:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 01:00:46 +00:00
|
|
|
return goldsmith.errors
|
2016-01-13 03:21:30 +00:00
|
|
|
}
|
2016-01-10 11:17:35 +00:00
|
|
|
|
2020-08-13 04:03:49 +00:00
|
|
|
func (goldsmith *Goldsmith) fault(name string, file *File, err error) {
|
2018-12-10 01:00:46 +00:00
|
|
|
goldsmith.mutex.Lock()
|
|
|
|
defer goldsmith.mutex.Unlock()
|
2018-12-08 19:18:51 +00:00
|
|
|
|
2020-08-13 04:03:49 +00:00
|
|
|
var faultError error
|
|
|
|
if file == nil {
|
|
|
|
faultError = fmt.Errorf("[%s]: %w", name, err)
|
|
|
|
} else {
|
|
|
|
faultError = fmt.Errorf("[%s@%v]: %w", name, file, err)
|
2018-12-08 19:18:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 01:00:46 +00:00
|
|
|
goldsmith.errors = append(goldsmith.errors, faultError)
|
2018-01-07 23:42:32 +00:00
|
|
|
}
|