shithub: hugo

Download patch

ref: 5a66fa3954f8d4329b2a32fe77c74d953a3c6bb7
parent: eb117eb9043a4954079f1845f61cc0bfe2facb37
author: Noah Campbell <[email protected]>
date: Tue Oct 1 10:26:21 EDT 2013

Chain transformers and test cases

Transformers can now be chained together, working on the output of the
previous run.

--- /dev/null
+++ b/transform/chain.go
@@ -1,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
+}
--- /dev/null
+++ b/transform/chain_test.go
@@ -1,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)
+}
--- a/transform/nav.go
+++ b/transform/nav.go
@@ -2,12 +2,12 @@
 
 import (
 	htmltran "code.google.com/p/go-html-transform/html/transform"
-	"io"
 	"fmt"
+	"io"
 )
 
 type NavActive struct {
-	Section string
+	Section  string
 	AttrName string
 }
 
@@ -27,7 +27,10 @@
 		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)
 }
--- a/transform/posttrans_test.go
+++ b/transform/posttrans_test.go
@@ -16,19 +16,27 @@
 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) {
-	tests := []struct {
-		content  string
-		expected string
-	}{
+
+	tr := &AbsURL{
+		BaseURL: "http://base",
+	}
+
+	apply(t, tr, abs_url_tests)
+}
+
+type test struct {
+	content string
+	expected string
+}
+
+var abs_url_tests = []test {
 		{H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF},
 		{H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF},
 		{H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL},
 	}
 
+func apply(t *testing.T, tr Transformer, tests []test) {
 	for _, test := range tests {
-		tr := &AbsURL{
-			BaseURL: "http://base",
-		}
 		out := new(bytes.Buffer)
 		err := tr.Apply(strings.NewReader(test.content), out)
 		if err != nil {