goldsmith/goldsmith.go

110 lines
2.5 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
)
2024-03-04 00:50:03 +00:00
type chainState 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
2024-03-04 00:50:03 +00:00
// Goldsmith chainable context.
type Goldsmith struct {
state *chainState
}
// Begin starts a chain, reading the files located in the source directory as input.
2024-03-04 00:50:03 +00:00
func (self *Goldsmith) Begin(sourceDir string) *Goldsmith {
self.state = new(chainState)
self.Chain(&fileImporter{sourceDir: sourceDir})
return self
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 {
2024-03-04 00:50:03 +00:00
self.state.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 {
2024-03-04 00:50:03 +00:00
self.state.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 {
context := &Context{
goldsmith: self,
plugin: plugin,
2024-03-04 00:50:03 +00:00
filtersExt: append(filterStack(nil), self.state.filters...),
index: self.state.index,
2024-02-20 00:26:41 +00:00
filesOut: make(chan File),
2016-06-11 23:24:06 +00:00
}
2024-03-04 00:50:03 +00:00
if len(self.state.contexts) > 0 {
context.filesIn = self.state.contexts[len(self.state.contexts)-1].filesOut
2015-11-02 09:23:13 +00:00
}
2016-06-11 23:24:06 +00:00
2024-03-04 00:50:03 +00:00
self.state.contexts = append(self.state.contexts, context)
self.state.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 {
2024-03-04 00:50:03 +00:00
self.state.filters.push(filter, self.state.index)
self.state.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 {
2024-03-04 00:50:03 +00:00
self.state.filters.pop()
self.state.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 {
2024-03-04 00:50:03 +00:00
self.Chain(&fileExporter{targetDir: targetDir, clean: self.state.clean})
for _, context := range self.state.contexts {
2018-12-08 19:18:51 +00:00
go context.step()
2016-08-21 19:54:44 +00:00
}
2024-03-04 00:50:03 +00:00
context := self.state.contexts[len(self.state.contexts)-1]
2021-05-01 22:12:50 +00:00
for range context.filesOut {
}
2021-04-25 16:29:53 +00:00
2024-03-04 00:50:03 +00:00
errors := self.state.errors
self.state = nil
return errors
2016-01-13 03:21:30 +00:00
}
2016-01-10 11:17:35 +00:00
2024-02-20 00:26:41 +00:00
func (self *Goldsmith) fault(name string, file File, err error) {
2024-03-04 00:50:03 +00:00
self.state.mutex.Lock()
defer self.state.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
}
2024-03-04 00:50:03 +00:00
self.state.errors = append(self.state.errors, faultError)
2018-01-07 23:42:32 +00:00
}