goldsmith/goldsmith.go

103 lines
2.3 KiB
Go
Raw Normal View History

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
"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 {
contexts []*Context
2018-12-08 19:18:51 +00:00
2021-05-01 22:12:50 +00:00
cache *cache
2021-04-25 15:57:24 +00:00
filters filterStack
clean bool
index int
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
// 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 {
2023-12-10 17:41:50 +00:00
goldsmith := new(Goldsmith)
goldsmith.Chain(&fileImporter{sourceDir: sourceDir})
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.
func (self *Goldsmith) Cache(cacheDir string) *Goldsmith {
self.cache = &cache{cacheDir}
return self
2015-10-29 14:26:43 +00:00
}
// Clean enables or disables removal of leftover files in the target directory.
func (self *Goldsmith) Clean(clean bool) *Goldsmith {
self.clean = clean
return self
}
2019-04-07 20:43:25 +00:00
// Chain links a plugin instance into the chain.
func (self *Goldsmith) Chain(plugin Plugin) *Goldsmith {
2018-12-08 19:18:51 +00:00
context := &Context{
goldsmith: self,
plugin: plugin,
filtersExt: append(filterStack(nil), self.filters...),
index: self.index,
filesOut: make(chan *File),
2016-06-11 23:24:06 +00:00
}
if len(self.contexts) > 0 {
context.filesIn = self.contexts[len(self.contexts)-1].filesOut
2015-11-02 09:23:13 +00:00
}
2016-06-11 23:24:06 +00:00
self.contexts = append(self.contexts, context)
self.index++
return self
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.
func (self *Goldsmith) FilterPush(filter Filter) *Goldsmith {
self.filters.push(filter, self.index)
self.index++
return self
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.
func (self *Goldsmith) FilterPop() *Goldsmith {
self.filters.pop()
self.index++
return self
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.
func (self *Goldsmith) End(targetDir string) []error {
2023-12-10 17:41:50 +00:00
self.Chain(&fileExporter{targetDir: targetDir, clean: self.clean})
for _, context := range self.contexts {
2018-12-08 19:18:51 +00:00
go context.step()
2016-08-21 19:54:44 +00:00
}
context := self.contexts[len(self.contexts)-1]
2021-05-01 22:12:50 +00:00
for range context.filesOut {
}
2021-04-25 16:29:53 +00:00
return self.errors
2016-01-13 03:21:30 +00:00
}
2016-01-10 11:17:35 +00:00
func (self *Goldsmith) fault(name string, file *File, err error) {
self.mutex.Lock()
defer self.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
}
self.errors = append(self.errors, faultError)
2018-01-07 23:42:32 +00:00
}