aboutsummaryrefslogtreecommitdiffhomepage
path: root/transform/absurl.go
blob: 89727a705689efbf9f47603303e5c3515bc0b38b (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
package transform

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

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() {
		ar = newAbsURLReplacer(viper.GetString("BaseURL"))
	})
}