mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-15 20:44:01 +02:00
Chain transformers and test cases
Transformers can now be chained together, working on the output of the previous run.
This commit is contained in:
29
transform/chain.go
Normal file
29
transform/chain.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"io"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type chain struct {
|
||||
transformers []Transformer
|
||||
}
|
||||
|
||||
func NewChain(trs ...Transformer) Transformer {
|
||||
return &chain{transformers: trs}
|
||||
}
|
||||
|
||||
func (c *chain) Apply(r io.Reader, w io.Writer) (err error) {
|
||||
in := r
|
||||
for _, tr := range c.transformers {
|
||||
out := new(bytes.Buffer)
|
||||
err = tr.Apply(in, out)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
in = bytes.NewBuffer(out.Bytes())
|
||||
}
|
||||
|
||||
_, err = io.Copy(w, in)
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user