goldsmith/goldsmith.go
Alex Yatskov e408fa9335 Squashed commit of the following:
commit 106991bfd59bb95977045399f7b4e5f4e7addc56
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sat Jan 8 11:19:21 2022 -0800

    Rename property functions

commit 07f6033d4e86df257806af16de002ce8fad3d67f
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sat Jan 8 11:16:46 2022 -0800

    Update rewrite

commit ea2dc0b4d223d54832752f0efd3eb46d81d17b50
Author: Alex Yatskov <alex@foosoft.net>
Date:   Fri Jan 7 22:47:17 2022 -0800

    Add more property methods

commit e2f7f8dc7ebc5b668539e8233e323a7c256eced2
Author: Alex Yatskov <alex@foosoft.net>
Date:   Fri Jan 7 22:39:23 2022 -0800

    Add step indexing

commit a7ed2a0b1c95ed9fbb0bbd87c779cf51a92f0b5f
Author: Alex Yatskov <alex@foosoft.net>
Date:   Fri Jan 7 20:50:19 2022 -0800

    Use self

commit 7ecc01e508c06680dcb6b6ecd4265a7f72e2c933
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sun Aug 22 12:33:09 2021 -0700

    Cleanup

commit 87dd28a4c14b88bea061ca913f68e272cca787f9
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sun Aug 22 12:09:41 2021 -0700

    Cleanup

commit 129130128d2a50f119c46f70e7cb70ef421892ff
Merge: 629fce0 e751d70
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sat Aug 21 12:18:09 2021 -0700

    Merge branch 'master' into dev

commit 629fce06a8fca6810ec772f558d9ffabda016d01
Author: Alex Yatskov <alex@foosoft.net>
Date:   Sun Jun 27 19:34:53 2021 -0700

    Abstract metadata
2022-01-08 18:08:36 -08:00

108 lines
2.3 KiB
Go

// 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)
}