2013-10-01 17:26:21 -04:00
|
|
|
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"
|
2013-10-01 17:26:21 -04:00
|
|
|
)
|
|
|
|
|
2013-11-05 17:28:06 -05:00
|
|
|
type trans func([]byte) []byte
|
2013-10-01 17:26:21 -04:00
|
|
|
|
2013-11-05 17:28:06 -05:00
|
|
|
type link trans
|
|
|
|
|
|
|
|
type chain []link
|
|
|
|
|
|
|
|
func NewChain(trs ...link) chain {
|
2013-11-01 01:14:11 -04:00
|
|
|
return trs
|
2013-10-01 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
2014-01-03 18:36:53 -05:00
|
|
|
func NewEmptyTransforms() []link {
|
|
|
|
return make([]link, 0, 20)
|
|
|
|
}
|
|
|
|
|
2013-10-01 17:42:08 -04:00
|
|
|
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)
|
2013-11-01 01:14:11 -04:00
|
|
|
|
2013-11-05 17:28:06 -05:00
|
|
|
buffer.ReadFrom(r)
|
|
|
|
b := buffer.Bytes()
|
|
|
|
for _, tr := range *c {
|
|
|
|
b = tr(b)
|
2013-10-01 17:26:21 -04:00
|
|
|
}
|
2013-11-05 17:28:06 -05:00
|
|
|
buffer.Reset()
|
|
|
|
buffer.Write(b)
|
|
|
|
buffer.WriteTo(w)
|
|
|
|
return
|
2013-10-01 17:26:21 -04:00
|
|
|
}
|