Add a transform step
This allows for the manipulation of the DOM. This is expected to be applied after the templates are rendered.
This commit is contained in:
parent
c75da346e1
commit
c6fe87b14e
2 changed files with 87 additions and 0 deletions
46
transform/post.go
Normal file
46
transform/post.go
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package transform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/url"
|
||||||
|
htmltran "code.google.com/p/go-html-transform/html/transform"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Transformer struct {
|
||||||
|
BaseURL string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Transformer) Apply(r io.Reader, w io.Writer) (err error) {
|
||||||
|
var tr *htmltran.Transformer
|
||||||
|
|
||||||
|
if tr, err = htmltran.NewFromReader(r); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = t.absUrlify(tr); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return tr.Render(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Transformer) absUrlify(tr *htmltran.Transformer) (err error) {
|
||||||
|
var baseURL, inURL *url.URL
|
||||||
|
|
||||||
|
if baseURL, err = url.Parse(t.BaseURL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
replace := func(in string) string {
|
||||||
|
if inURL, err = url.Parse(in); err != nil {
|
||||||
|
return in + "?"
|
||||||
|
}
|
||||||
|
return baseURL.ResolveReference(inURL).String()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = tr.Apply(htmltran.TransformAttrib("src", replace), "script"); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return tr.Apply(htmltran.TransformAttrib("href", replace), "a")
|
||||||
|
}
|
41
transform/posttrans_test.go
Normal file
41
transform/posttrans_test.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package transform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"strings"
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
|
const H5_JS_CONTENT_DOUBLE_QUOTE = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href='/foobar'>foobar</a>. Follow up</article></body></html>"
|
||||||
|
const H5_JS_CONTENT_SINGLE_QUOTE = "<!DOCTYPE html><html><head><script src='foobar.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='/foobar'>foobar</a>. Follow up</article></body></html>"
|
||||||
|
const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"http://user@host:10234/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/foobar\">foobar</a>. Follow up</article></body></html>"
|
||||||
|
// URL doesn't recognize authorities. BUG?
|
||||||
|
//const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"//host/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/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) {
|
||||||
|
tests := []struct {
|
||||||
|
content string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{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},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
tr := &Transformer{
|
||||||
|
BaseURL: "http://base",
|
||||||
|
}
|
||||||
|
out := new(bytes.Buffer)
|
||||||
|
err := tr.Apply(strings.NewReader(test.content), out)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %s", err)
|
||||||
|
}
|
||||||
|
if test.expected != string(out.Bytes()) {
|
||||||
|
t.Errorf("Expected:\n%s\nGot:\n%s", test.expected, string(out.Bytes()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue