aboutsummaryrefslogtreecommitdiffhomepage
path: root/transform/absurl.go
blob: 75dbe4db377baf161514f18136f0ca2a77558696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package transform

import (
	"github.com/spf13/viper"
	"sync"
)

// to be used in tests; the live site will get its value from Viper.
var AbsBaseUrl string

var absURLInit sync.Once
var ar *absURLReplacer

func AbsURL() (trs []link, err error) {
	initAbsURLReplacer()
	return absURLFromReplacer(ar)
}

func absURLFromURL(URL string) (trs []link, err error) {
	return absURLFromReplacer(newAbsURLReplacer(URL))
}

func absURLFromReplacer(ar *absURLReplacer) (trs []link, err error) {
	trs = append(trs, func(ct contentTransformer) {
		ar.replaceInHTML(ct)
	})
	return
}

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)
	})
}