mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Rename interface contentRewriter to contentTransformer
Is is a much better name.
This commit is contained in:
parent
efb564775a
commit
66cf3bdc77
5 changed files with 26 additions and 26 deletions
|
@ -18,7 +18,7 @@ func absURLFromURL(URL string) (trs []link, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func absURLFromReplacer(ar *absURLReplacer) (trs []link, err error) {
|
func absURLFromReplacer(ar *absURLReplacer) (trs []link, err error) {
|
||||||
trs = append(trs, func(rw contentRewriter) {
|
trs = append(trs, func(rw contentTransformer) {
|
||||||
ar.replaceInHTML(rw)
|
ar.replaceInHTML(rw)
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
@ -34,7 +34,7 @@ func absURLInXMLFromURL(URL string) (trs []link, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func absURLInXMLFromReplacer(ar *absURLReplacer) (trs []link, err error) {
|
func absURLInXMLFromReplacer(ar *absURLReplacer) (trs []link, err error) {
|
||||||
trs = append(trs, func(rw contentRewriter) {
|
trs = append(trs, func(rw contentTransformer) {
|
||||||
ar.replaceInXML(rw)
|
ar.replaceInXML(rw)
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
|
|
@ -176,11 +176,11 @@ func (l *contentlexer) replace() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func doReplace(rw contentRewriter, matchers []absURLMatcher) {
|
func doReplace(ct contentTransformer, matchers []absURLMatcher) {
|
||||||
|
|
||||||
lexer := &contentlexer{
|
lexer := &contentlexer{
|
||||||
content: rw.Content(),
|
content: ct.Content(),
|
||||||
w: rw,
|
w: ct,
|
||||||
prefixLookup: &prefixes{pr: mainPrefixRunes},
|
prefixLookup: &prefixes{pr: mainPrefixRunes},
|
||||||
matchers: matchers}
|
matchers: matchers}
|
||||||
|
|
||||||
|
@ -226,10 +226,10 @@ func newAbsURLReplacer(baseURL string) *absURLReplacer {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (au *absURLReplacer) replaceInHTML(rw contentRewriter) {
|
func (au *absURLReplacer) replaceInHTML(ct contentTransformer) {
|
||||||
doReplace(rw, au.htmlMatchers)
|
doReplace(ct, au.htmlMatchers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (au *absURLReplacer) replaceInXML(rw contentRewriter) {
|
func (au *absURLReplacer) replaceInXML(ct contentTransformer) {
|
||||||
doReplace(rw, au.xmlMatchers)
|
doReplace(ct, au.xmlMatchers)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
type trans func(rw contentRewriter)
|
type trans func(rw contentTransformer)
|
||||||
|
|
||||||
type link trans
|
type link trans
|
||||||
|
|
||||||
|
@ -20,14 +20,14 @@ func NewEmptyTransforms() []link {
|
||||||
return make([]link, 0, 20)
|
return make([]link, 0, 20)
|
||||||
}
|
}
|
||||||
|
|
||||||
// contentRewriter is an interface that enables rotation
|
// contentTransformer is an interface that enables rotation
|
||||||
// of pooled buffers in the transformer chain.
|
// of pooled buffers in the transformer chain.
|
||||||
type contentRewriter interface {
|
type contentTransformer interface {
|
||||||
Content() []byte
|
Content() []byte
|
||||||
io.Writer
|
io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements contentRewriter
|
// Implements contentTransformer
|
||||||
// Content is read from the from-buffer,
|
// Content is read from the from-buffer,
|
||||||
// and rewritten to to the to-buffer.
|
// and rewritten to to the to-buffer.
|
||||||
type fromToBuffer struct {
|
type fromToBuffer struct {
|
||||||
|
|
|
@ -56,18 +56,18 @@ func TestChainZeroTransformers(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChaingMultipleTransformers(t *testing.T) {
|
func TestChaingMultipleTransformers(t *testing.T) {
|
||||||
f1 := func(rw contentRewriter) {
|
f1 := func(ct contentTransformer) {
|
||||||
rw.Write(bytes.Replace(rw.Content(), []byte("f1"), []byte("f1r"), -1))
|
ct.Write(bytes.Replace(ct.Content(), []byte("f1"), []byte("f1r"), -1))
|
||||||
}
|
}
|
||||||
f2 := func(rw contentRewriter) {
|
f2 := func(ct contentTransformer) {
|
||||||
rw.Write(bytes.Replace(rw.Content(), []byte("f2"), []byte("f2r"), -1))
|
ct.Write(bytes.Replace(ct.Content(), []byte("f2"), []byte("f2r"), -1))
|
||||||
}
|
}
|
||||||
f3 := func(rw contentRewriter) {
|
f3 := func(ct contentTransformer) {
|
||||||
rw.Write(bytes.Replace(rw.Content(), []byte("f3"), []byte("f3r"), -1))
|
ct.Write(bytes.Replace(ct.Content(), []byte("f3"), []byte("f3r"), -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
f4 := func(rw contentRewriter) {
|
f4 := func(ct contentTransformer) {
|
||||||
rw.Write(bytes.Replace(rw.Content(), []byte("f4"), []byte("f4r"), -1))
|
ct.Write(bytes.Replace(ct.Content(), []byte("f4"), []byte("f4r"), -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
tr := NewChain(f1, f2, f3, f4)
|
tr := NewChain(f1, f2, f3, f4)
|
||||||
|
|
|
@ -5,19 +5,19 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LiveReloadInject(rw contentRewriter) {
|
func LiveReloadInject(ct contentTransformer) {
|
||||||
match := []byte("</body>")
|
match := []byte("</body>")
|
||||||
port := viper.GetString("port")
|
port := viper.GetString("port")
|
||||||
replace := []byte(`<script>document.write('<script src="http://'
|
replace := []byte(`<script>document.write('<script src="http://'
|
||||||
+ (location.host || 'localhost').split(':')[0]
|
+ (location.host || 'localhost').split(':')[0]
|
||||||
+ ':` + port + `/livereload.js?mindelay=10"></'
|
+ ':` + port + `/livereload.js?mindelay=10"></'
|
||||||
+ 'script>')</script></body>`)
|
+ 'script>')</script></body>`)
|
||||||
newcontent := bytes.Replace(rw.Content(), match, replace, -1)
|
newcontent := bytes.Replace(ct.Content(), match, replace, -1)
|
||||||
|
|
||||||
if len(newcontent) == len(rw.Content()) {
|
if len(newcontent) == len(ct.Content()) {
|
||||||
match := []byte("</BODY>")
|
match := []byte("</BODY>")
|
||||||
newcontent = bytes.Replace(rw.Content(), match, replace, -1)
|
newcontent = bytes.Replace(ct.Content(), match, replace, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
rw.Write(newcontent)
|
ct.Write(newcontent)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue