From eb117eb9043a4954079f1845f61cc0bfe2facb37 Mon Sep 17 00:00:00 2001 From: Noah Campbell Date: Tue, 1 Oct 2013 12:59:29 -0700 Subject: [PATCH] Move AbsURL into seperate file --- transform/absurl.go | 59 +++++++++++++++++++++++++++++++++++++++++++++ transform/post.go | 54 ----------------------------------------- 2 files changed, 59 insertions(+), 54 deletions(-) create mode 100644 transform/absurl.go diff --git a/transform/absurl.go b/transform/absurl.go new file mode 100644 index 000000000..6e7eb125b --- /dev/null +++ b/transform/absurl.go @@ -0,0 +1,59 @@ +package transform + +import ( + htmltran "code.google.com/p/go-html-transform/html/transform" + "io" + "net/url" +) + +type AbsURL struct { + BaseURL string +} + +func (t *AbsURL) 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, elattr{"a", "href"}, elattr{"script", "src"}); err != nil { + return + } + + return tr.Render(w) +} + +type elattr struct { + tag, attr string +} + +func (t *AbsURL) absUrlify(tr *htmltran.Transformer, selectors ...elattr) (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 + "?" + } + if fragmentOnly(inURL) { + return in + } + return baseURL.ResolveReference(inURL).String() + } + + for _, el := range selectors { + if err = tr.Apply(htmltran.TransformAttrib(el.attr, replace), el.tag); err != nil { + return + } + } + + return +} + +func fragmentOnly(u *url.URL) bool { + return u.Fragment != "" && u.Scheme == "" && u.Opaque == "" && u.User == nil && u.Host == "" && u.Path == "" && u.Path == "" && u.RawQuery == "" +} diff --git a/transform/post.go b/transform/post.go index c89a7bdf2..a23b48d2e 100644 --- a/transform/post.go +++ b/transform/post.go @@ -1,63 +1,9 @@ package transform import ( - htmltran "code.google.com/p/go-html-transform/html/transform" "io" - "net/url" ) type Transformer interface { Apply(io.Reader, io.Writer) error } - -type AbsURL struct { - BaseURL string -} - -func (t *AbsURL) 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, elattr{"a", "href"}, elattr{"script", "src"}); err != nil { - return - } - - return tr.Render(w) -} - -type elattr struct { - tag, attr string -} - -func (t *AbsURL) absUrlify(tr *htmltran.Transformer, selectors ...elattr) (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 + "?" - } - if fragmentOnly(inURL) { - return in - } - return baseURL.ResolveReference(inURL).String() - } - - for _, el := range selectors { - if err = tr.Apply(htmltran.TransformAttrib(el.attr, replace), el.tag); err != nil { - return - } - } - - return -} - -func fragmentOnly(u *url.URL) bool { - return u.Fragment != "" && u.Scheme == "" && u.Opaque == "" && u.User == nil && u.Host == "" && u.Path == "" && u.Path == "" && u.RawQuery == "" -}