mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Move AbsURL into seperate file
This commit is contained in:
parent
f0211b84a1
commit
eb117eb904
2 changed files with 59 additions and 54 deletions
59
transform/absurl.go
Normal file
59
transform/absurl.go
Normal file
|
@ -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 == ""
|
||||
}
|
|
@ -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 == ""
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue