2013-10-01 15:59:29 -04:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
2015-03-18 14:42:46 -04:00
|
|
|
"github.com/spf13/viper"
|
2015-02-16 04:48:15 -05:00
|
|
|
"sync"
|
2013-10-01 15:59:29 -04:00
|
|
|
)
|
|
|
|
|
2015-05-01 06:29:10 -04:00
|
|
|
// to be used in tests; the live site will get its value from Viper.
|
|
|
|
var AbsBaseUrl string
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
var absURLInit sync.Once
|
|
|
|
var ar *absURLReplacer
|
2013-10-01 15:59:29 -04:00
|
|
|
|
2015-03-18 14:42:46 -04:00
|
|
|
func AbsURL() (trs []link, err error) {
|
|
|
|
initAbsURLReplacer()
|
|
|
|
return absURLFromReplacer(ar)
|
2015-02-16 04:48:15 -05:00
|
|
|
}
|
2013-10-01 15:59:29 -04:00
|
|
|
|
2015-03-18 14:42:46 -04:00
|
|
|
func absURLFromURL(URL string) (trs []link, err error) {
|
|
|
|
return absURLFromReplacer(newAbsURLReplacer(URL))
|
|
|
|
}
|
2013-11-09 01:33:00 -05:00
|
|
|
|
2015-03-18 14:42:46 -04:00
|
|
|
func absURLFromReplacer(ar *absURLReplacer) (trs []link, err error) {
|
2015-03-31 18:54:26 -04:00
|
|
|
trs = append(trs, func(ct contentTransformer) {
|
|
|
|
ar.replaceInHTML(ct)
|
2013-11-05 17:28:06 -05:00
|
|
|
})
|
2013-11-01 01:14:11 -04:00
|
|
|
return
|
2013-10-01 15:59:29 -04:00
|
|
|
}
|
|
|
|
|
2015-03-18 14:42:46 -04:00
|
|
|
func AbsURLInXML() (trs []link, err error) {
|
|
|
|
initAbsURLReplacer()
|
|
|
|
return absURLInXMLFromReplacer(ar)
|
|
|
|
}
|
|
|
|
|
|
|
|
func absURLInXMLFromURL(URL string) (trs []link, err error) {
|
|
|
|
return absURLInXMLFromReplacer(newAbsURLReplacer(URL))
|
|
|
|
}
|
2014-12-18 14:59:39 -05:00
|
|
|
|
2015-03-18 14:42:46 -04:00
|
|
|
func absURLInXMLFromReplacer(ar *absURLReplacer) (trs []link, err error) {
|
2015-03-31 18:54:26 -04:00
|
|
|
trs = append(trs, func(ct contentTransformer) {
|
|
|
|
ar.replaceInXML(ct)
|
2014-12-18 14:59:39 -05:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2015-03-18 14:42:46 -04:00
|
|
|
|
|
|
|
func initAbsURLReplacer() {
|
|
|
|
absURLInit.Do(func() {
|
2015-05-01 06:29:10 -04:00
|
|
|
var url string
|
|
|
|
|
|
|
|
if AbsBaseUrl != "" {
|
|
|
|
url = AbsBaseUrl
|
|
|
|
} else {
|
|
|
|
url = viper.GetString("BaseURL")
|
|
|
|
}
|
|
|
|
|
|
|
|
ar = newAbsURLReplacer(url)
|
2015-03-18 14:42:46 -04:00
|
|
|
})
|
|
|
|
}
|