2013-09-17 20:04:28 +00:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
2013-09-18 16:15:46 +00:00
|
|
|
htmltran "code.google.com/p/go-html-transform/html/transform"
|
2013-09-17 20:04:28 +00:00
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-09-17 21:16:06 +00:00
|
|
|
if err = t.absUrlify(tr, elattr{"a", "href"}, elattr{"script", "src"}); err != nil {
|
2013-09-17 20:04:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return tr.Render(w)
|
|
|
|
}
|
|
|
|
|
2013-09-17 21:16:06 +00:00
|
|
|
type elattr struct {
|
|
|
|
tag, attr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transformer) absUrlify(tr *htmltran.Transformer, selectors ...elattr) (err error) {
|
2013-09-17 20:04:28 +00:00
|
|
|
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 + "?"
|
|
|
|
}
|
2013-09-18 22:48:36 +00:00
|
|
|
if fragmentOnly(inURL) {
|
|
|
|
return in
|
|
|
|
}
|
2013-09-17 20:04:28 +00:00
|
|
|
return baseURL.ResolveReference(inURL).String()
|
|
|
|
}
|
|
|
|
|
2013-09-17 21:16:06 +00:00
|
|
|
for _, el := range selectors {
|
|
|
|
if err = tr.Apply(htmltran.TransformAttrib(el.attr, replace), el.tag); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-09-17 20:04:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 21:16:06 +00:00
|
|
|
return
|
2013-09-17 20:04:28 +00:00
|
|
|
}
|
2013-09-18 22:48:36 +00:00
|
|
|
|
|
|
|
func fragmentOnly(u *url.URL) bool {
|
|
|
|
return u.Fragment != "" && u.Scheme == "" && u.Opaque == "" && u.User == nil && u.Host == "" && u.Path == "" && u.Path == "" && u.RawQuery == ""
|
|
|
|
}
|