2013-10-01 19:59:29 +00:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
2013-11-09 06:33:00 +00:00
|
|
|
"bytes"
|
2013-10-01 19:59:29 +00:00
|
|
|
"net/url"
|
2013-11-09 06:33:00 +00:00
|
|
|
"strings"
|
2013-10-01 19:59:29 +00:00
|
|
|
)
|
|
|
|
|
2013-11-05 22:28:06 +00:00
|
|
|
func AbsURL(absURL string) (trs []link, err error) {
|
2013-11-01 05:14:11 +00:00
|
|
|
var baseURL *url.URL
|
2013-10-01 19:59:29 +00:00
|
|
|
|
2013-11-01 05:14:11 +00:00
|
|
|
if baseURL, err = url.Parse(absURL); err != nil {
|
2013-10-01 19:59:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-09 06:33:00 +00:00
|
|
|
base := strings.TrimRight(baseURL.String(), "/")
|
|
|
|
|
2013-11-05 22:28:06 +00:00
|
|
|
var (
|
2013-11-09 06:33:00 +00:00
|
|
|
srcdq = []byte(" src=\"" + base + "/")
|
|
|
|
hrefdq = []byte(" href=\"" + base + "/")
|
|
|
|
srcsq = []byte(" src='" + base + "/")
|
|
|
|
hrefsq = []byte(" href='" + base + "/")
|
2013-11-05 22:28:06 +00:00
|
|
|
)
|
|
|
|
trs = append(trs, func(content []byte) []byte {
|
2013-11-09 14:35:09 +00:00
|
|
|
content = guardReplace(content, []byte(" src=\"//"), []byte(" src=\"/"), srcdq)
|
|
|
|
content = guardReplace(content, []byte(" src='//"), []byte(" src='/"), srcsq)
|
|
|
|
content = guardReplace(content, []byte(" href=\"//"), []byte(" href=\"/"), hrefdq)
|
|
|
|
content = guardReplace(content, []byte(" href='//"), []byte(" href='/"), hrefsq)
|
2013-11-05 22:28:06 +00:00
|
|
|
return content
|
|
|
|
})
|
2013-11-01 05:14:11 +00:00
|
|
|
return
|
2013-10-01 19:59:29 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 19:59:39 +00:00
|
|
|
func AbsURLInXML(absURL string) (trs []link, err error) {
|
|
|
|
var baseURL *url.URL
|
|
|
|
|
|
|
|
if baseURL, err = url.Parse(absURL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
base := strings.TrimRight(baseURL.String(), "/")
|
|
|
|
|
|
|
|
var (
|
|
|
|
srcedq = []byte(" src="" + base + "/")
|
|
|
|
hrefedq = []byte(" href="" + base + "/")
|
|
|
|
srcesq = []byte(" src='" + base + "/")
|
|
|
|
hrefesq = []byte(" href='" + base + "/")
|
|
|
|
)
|
|
|
|
trs = append(trs, func(content []byte) []byte {
|
|
|
|
content = guardReplace(content, []byte(" src="//"), []byte(" src="/"), srcedq)
|
|
|
|
content = guardReplace(content, []byte(" src='//"), []byte(" src='/"), srcesq)
|
|
|
|
content = guardReplace(content, []byte(" href="//"), []byte(" href="/"), hrefedq)
|
|
|
|
content = guardReplace(content, []byte(" href='//"), []byte(" href='/"), hrefesq)
|
|
|
|
return content
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-09 14:35:09 +00:00
|
|
|
func guardReplace(content, guard, match, replace []byte) []byte {
|
2013-11-24 21:48:57 +00:00
|
|
|
if !bytes.Contains(content, guard) {
|
|
|
|
content = bytes.Replace(content, match, replace, -1)
|
|
|
|
}
|
|
|
|
return content
|
2013-11-09 14:35:09 +00:00
|
|
|
}
|