hugo/transform/absurl.go

59 lines
1.1 KiB
Go
Raw Normal View History

2013-10-01 15:59:29 -04:00
package transform
import (
"github.com/spf13/viper"
"sync"
2013-10-01 15:59:29 -04:00
)
// to be used in tests; the live site will get its value from Viper.
var AbsBaseUrl string
var absURLInit sync.Once
var ar *absURLReplacer
2013-10-01 15:59:29 -04:00
func AbsURL() (trs []link, err error) {
initAbsURLReplacer()
return absURLFromReplacer(ar)
}
2013-10-01 15:59:29 -04:00
func absURLFromURL(URL string) (trs []link, err error) {
return absURLFromReplacer(newAbsURLReplacer(URL))
}
2013-11-09 01:33:00 -05:00
func absURLFromReplacer(ar *absURLReplacer) (trs []link, err error) {
trs = append(trs, func(ct contentTransformer) {
ar.replaceInHTML(ct)
})
return
2013-10-01 15:59:29 -04:00
}
func AbsURLInXML() (trs []link, err error) {
initAbsURLReplacer()
return absURLInXMLFromReplacer(ar)
}
func absURLInXMLFromURL(URL string) (trs []link, err error) {
return absURLInXMLFromReplacer(newAbsURLReplacer(URL))
}
func absURLInXMLFromReplacer(ar *absURLReplacer) (trs []link, err error) {
trs = append(trs, func(ct contentTransformer) {
ar.replaceInXML(ct)
})
return
}
func initAbsURLReplacer() {
absURLInit.Do(func() {
var url string
if AbsBaseUrl != "" {
url = AbsBaseUrl
} else {
url = viper.GetString("BaseURL")
}
ar = newAbsURLReplacer(url)
})
}