summaryrefslogtreecommitdiffstats
path: root/transform
diff options
context:
space:
mode:
authorNoah Campbell <noahcampbell@gmail.com>2013-10-01 14:26:21 -0700
committerNoah Campbell <noahcampbell@gmail.com>2013-10-08 18:37:50 +0200
commit5a66fa3954f8d4329b2a32fe77c74d953a3c6bb7 (patch)
tree9a21810804e17af8204cc22f220368e801870997 /transform
parenteb117eb9043a4954079f1845f61cc0bfe2facb37 (diff)
Chain transformers and test cases
Transformers can now be chained together, working on the output of the previous run.
Diffstat (limited to 'transform')
-rw-r--r--transform/chain.go29
-rw-r--r--transform/chain_test.go36
-rw-r--r--transform/nav.go9
-rw-r--r--transform/posttrans_test.go22
4 files changed, 86 insertions, 10 deletions
diff --git a/transform/chain.go b/transform/chain.go
new file mode 100644
index 000000000..e408b01d2
--- /dev/null
+++ b/transform/chain.go
@@ -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
+}
diff --git a/transform/chain_test.go b/transform/chain_test.go
new file mode 100644
index 000000000..d122c31f8
--- /dev/null
+++ b/transform/chain_test.go
@@ -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)
+}
diff --git a/transform/nav.go b/transform/nav.go
index b085c39a2..2111ae89a 100644
--- a/transform/nav.go
+++ b/transform/nav.go
@@ -2,12 +2,12 @@ package transform
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 @@ func (n *NavActive) Apply(r io.Reader, w io.Writer) (err error) {
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)
}
diff --git a/transform/posttrans_test.go b/transform/posttrans_test.go
index d35847af3..3cac6e76b 100644
--- a/transform/posttrans_test.go
+++ b/transform/posttrans_test.go
@@ -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>"
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 {