mirror of
https://github.com/gohugoio/hugo.git
synced 2024-12-25 01:52:23 +00:00
9af47f07d3
50% speedup. Fix #91 to run the benchmark: go test -test.run=NONE -bench=".*" -test.benchmem=true ./transform/ > new.txt to compare the results: /usr/local/go/misc/benchcmp baseline.txt new.txt Speedup and memory improvements benchmark old ns/op new ns/op delta BenchmarkChain 101219 50453 -50.15% BenchmarkTransform 51625 45531 -11.80% benchmark old allocs new allocs delta BenchmarkChain 222 103 -53.60% BenchmarkTransform 135 106 -21.48% benchmark old bytes new bytes delta BenchmarkChain 23919 10998 -54.02% BenchmarkTransform 11858 10665 -10.06%
48 lines
1 KiB
Go
48 lines
1 KiB
Go
package transform
|
|
|
|
import (
|
|
htmltran "code.google.com/p/go-html-transform/html/transform"
|
|
"net/url"
|
|
)
|
|
|
|
func AbsURL(absURL string) (trs []*htmltran.Transform, err error) {
|
|
var baseURL *url.URL
|
|
|
|
if baseURL, err = url.Parse(absURL); err != nil {
|
|
return
|
|
}
|
|
|
|
if trs, err = absUrlify(baseURL, elattr{"a", "href"}, elattr{"script", "src"}); err != nil {
|
|
return
|
|
}
|
|
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 == ""
|
|
}
|