mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05: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:
parent
eb117eb904
commit
5a66fa3954
4 changed files with 86 additions and 10 deletions
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
|
||||||
|
}
|
36
transform/chain_test.go
Normal file
36
transform/chain_test.go
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
package transform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestChainZeroTransformers(t *testing.T) {
|
||||||
|
tr := NewChain()
|
||||||
|
in := new(bytes.Buffer)
|
||||||
|
out := new(bytes.Buffer)
|
||||||
|
if err := tr.Apply(in, out); err != nil {
|
||||||
|
t.Errorf("A zero transformer chain returned an error.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChainOneTransformer(t *testing.T) {
|
||||||
|
tr := NewChain(&AbsURL{BaseURL: "http://base"})
|
||||||
|
apply(t, tr, abs_url_tests)
|
||||||
|
}
|
||||||
|
|
||||||
|
const H5_JS_CONTENT_ABS_URL_WITH_NAV = "<!DOCTYPE html><html><head><script src=\"/foobar.js\"></script></head><body><nav><ul><li hugo-nav=\"section_0\"></li><li hugo-nav=\"section_1\"></li></ul></nav><article>content <a href=\"/foobar\">foobar</a>. Follow up</article></body></html>"
|
||||||
|
|
||||||
|
const CORRECT_OUTPUT_SRC_HREF_WITH_NAV = "<!DOCTYPE html><html><head><script src=\"http://two/foobar.js\"></script></head><body><nav><ul><li hugo-nav=\"section_0\"></li><li hugo-nav=\"section_1\" class=\"active\"></li></ul></nav><article>content <a href=\"http://two/foobar\">foobar</a>. Follow up</article></body></html>"
|
||||||
|
|
||||||
|
var two_chain_tests = []test {
|
||||||
|
{H5_JS_CONTENT_ABS_URL_WITH_NAV, CORRECT_OUTPUT_SRC_HREF_WITH_NAV},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChainTwoTransformer(t *testing.T) {
|
||||||
|
tr := NewChain(
|
||||||
|
&AbsURL{BaseURL: "http://two"},
|
||||||
|
&NavActive{Section: "section_1"},
|
||||||
|
)
|
||||||
|
apply(t, tr, two_chain_tests)
|
||||||
|
}
|
|
@ -2,8 +2,8 @@ package transform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
htmltran "code.google.com/p/go-html-transform/html/transform"
|
htmltran "code.google.com/p/go-html-transform/html/transform"
|
||||||
"io"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NavActive struct {
|
type NavActive struct {
|
||||||
|
@ -27,7 +27,10 @@ func (n *NavActive) Apply(r io.Reader, w io.Writer) (err error) {
|
||||||
n.AttrName = "hugo-nav"
|
n.AttrName = "hugo-nav"
|
||||||
}
|
}
|
||||||
|
|
||||||
tr.Apply(htmltran.ModifyAttrib("class", "active"), fmt.Sprintf("li[%s=%s]", n.AttrName, n.Section))
|
err = tr.Apply(htmltran.ModifyAttrib("class", "active"), fmt.Sprintf("li[%s=%s]", n.AttrName, n.Section))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return tr.Render(w)
|
return tr.Render(w)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,19 +16,27 @@ const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"http://u
|
||||||
const CORRECT_OUTPUT_SRC_HREF = "<!DOCTYPE html><html><head><script src=\"http://base/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"http://base/foobar\">foobar</a>. Follow up</article></body></html>"
|
const CORRECT_OUTPUT_SRC_HREF = "<!DOCTYPE html><html><head><script src=\"http://base/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"http://base/foobar\">foobar</a>. Follow up</article></body></html>"
|
||||||
|
|
||||||
func TestAbsUrlify(t *testing.T) {
|
func TestAbsUrlify(t *testing.T) {
|
||||||
tests := []struct {
|
|
||||||
|
tr := &AbsURL{
|
||||||
|
BaseURL: "http://base",
|
||||||
|
}
|
||||||
|
|
||||||
|
apply(t, tr, abs_url_tests)
|
||||||
|
}
|
||||||
|
|
||||||
|
type test struct {
|
||||||
content string
|
content string
|
||||||
expected string
|
expected string
|
||||||
}{
|
}
|
||||||
|
|
||||||
|
var abs_url_tests = []test {
|
||||||
{H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF},
|
{H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF},
|
||||||
{H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF},
|
{H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF},
|
||||||
{H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL},
|
{H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func apply(t *testing.T, tr Transformer, tests []test) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
tr := &AbsURL{
|
|
||||||
BaseURL: "http://base",
|
|
||||||
}
|
|
||||||
out := new(bytes.Buffer)
|
out := new(bytes.Buffer)
|
||||||
err := tr.Apply(strings.NewReader(test.content), out)
|
err := tr.Apply(strings.NewReader(test.content), out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue