goldsmith/goldsmith.go

75 lines
1.8 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
2024-03-04 00:50:03 +00:00
// Goldsmith chainable context.
type Goldsmith struct {
CacheDir string
Clean bool
2024-03-04 02:13:42 +00:00
chain *chainState
2024-03-04 00:50:03 +00:00
}
// 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 {
2024-03-04 05:30:32 +00:00
self.chain = &chainState{}
if len(self.CacheDir) > 0 {
self.chain.cache = &cache{self.CacheDir}
}
2015-10-29 14:26:43 +00:00
self.Chain(&fileImporter{sourceDir: sourceDir})
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{
2024-03-04 02:13:42 +00:00
chain: self.chain,
plugin: plugin,
2024-03-04 02:13:42 +00:00
filtersExt: append(filterStack(nil), self.chain.filters...),
index: self.chain.index,
filesOut: make(chan *File),
2016-06-11 23:24:06 +00:00
}
2024-03-04 02:13:42 +00:00
if len(self.chain.contexts) > 0 {
context.filesIn = self.chain.contexts[len(self.chain.contexts)-1].filesOut
2015-11-02 09:23:13 +00:00
}
2016-06-11 23:24:06 +00:00
2024-03-04 02:13:42 +00:00
self.chain.contexts = append(self.chain.contexts, context)
self.chain.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 02:13:42 +00:00
self.chain.filters.push(filter, self.chain.index)
self.chain.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 02:13:42 +00:00
self.chain.filters.pop()
self.chain.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 {
self.Chain(&fileExporter{targetDir: targetDir, clean: self.Clean})
2024-03-04 02:13:42 +00:00
for _, context := range self.chain.contexts {
2018-12-08 19:18:51 +00:00
go context.step()
2016-08-21 19:54:44 +00:00
}
2024-03-04 02:13:42 +00:00
context := self.chain.contexts[len(self.chain.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 02:13:42 +00:00
errors := self.chain.errors
self.chain = nil
2024-03-04 00:50:03 +00:00
return errors
2016-01-13 03:21:30 +00:00
}