hugo/transform/chain.go

37 lines
521 B
Go
Raw Normal View History

package transform
import (
2013-10-01 22:59:35 -04:00
"io"
2015-01-30 14:39:06 -05:00
bp "github.com/spf13/hugo/bufferpool"
)
type trans func([]byte) []byte
type link trans
type chain []link
func NewChain(trs ...link) chain {
return trs
}
func NewEmptyTransforms() []link {
return make([]link, 0, 20)
}
func (c *chain) Apply(w io.Writer, r io.Reader) (err error) {
2015-01-30 14:39:06 -05:00
buffer := bp.GetBuffer()
defer bp.PutBuffer(buffer)
buffer.ReadFrom(r)
b := buffer.Bytes()
for _, tr := range *c {
b = tr(b)
}
buffer.Reset()
buffer.Write(b)
buffer.WriteTo(w)
return
}