mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
86233c00a0
Remove the hugo-nav since it relied on a slow library. The current build reimplements the absurl functionality based on string replace. Discovered that my prior implementation missed the requirement for making absolute paths (/path) absolute with the host, whereas a relative path is left untouched. Updated the test cases to support this if this is reimplemented.
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package transform
|
|
|
|
import (
|
|
htmltran "code.google.com/p/go-html-transform/html/transform"
|
|
"net/url"
|
|
"bytes"
|
|
)
|
|
|
|
func AbsURL(absURL string) (trs []link, err error) {
|
|
var baseURL *url.URL
|
|
|
|
if baseURL, err = url.Parse(absURL); err != nil {
|
|
return
|
|
}
|
|
|
|
var (
|
|
srcdq = []byte(" src=\""+baseURL.String()+"/")
|
|
hrefdq = []byte(" href=\""+baseURL.String()+"/")
|
|
srcsq = []byte(" src='"+baseURL.String()+"/")
|
|
hrefsq = []byte(" href='"+baseURL.String()+"/")
|
|
)
|
|
trs = append(trs, func(content []byte) []byte {
|
|
content = bytes.Replace(content, []byte(" src=\"/"), srcdq, -1)
|
|
content = bytes.Replace(content, []byte(" src='/"), srcsq, -1)
|
|
content = bytes.Replace(content, []byte(" href=\"/"), hrefdq, -1)
|
|
content = bytes.Replace(content, []byte(" href='/"), hrefsq, -1)
|
|
return content
|
|
})
|
|
return
|
|
}
|
|
|
|
type elattr struct {
|
|
tag, attr string
|
|
}
|
|
|
|
func absUrlify(baseURL *url.URL, selectors ...elattr) (trs []*htmltran.Transform, err error) {
|
|
var inURL *url.URL
|
|
|
|
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 {
|
|
mt := htmltran.MustTrans(htmltran.TransformAttrib(el.attr, replace), el.tag)
|
|
trs = append(trs, mt)
|
|
}
|
|
|
|
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 == ""
|
|
}
|