diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2015-05-16 00:11:39 +0200 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-05-16 00:11:44 +0200 |
commit | beaa8b1bcabd4be25ac26bea39ab9f7290147e67 (patch) | |
tree | 76095c2b6ec9ddf22477c188c87f47e4c6cee8d6 /transform/chain_test.go | |
parent | e522e5f4154cb6a5d960aeb8920fa3e433641cf6 (diff) |
Add support for URLs relative to context root
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative.
And will do so with speed.
So:
In `/post/myblogpost.html`:
`/mycss.css` becomes `../mycss.css`
The same in `/index.html` will become:
`./mycss.css` etc.
Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`).
The speediness is about the same as before:
```
benchmark old ns/op new ns/op delta
BenchmarkAbsURL 17462 18164 +4.02%
BenchmarkAbsURLSrcset 18842 19632 +4.19%
BenchmarkXMLAbsURLSrcset 18643 19313 +3.59%
BenchmarkXMLAbsURL 9283 9656 +4.02%
benchmark old allocs new allocs delta
BenchmarkAbsURL 24 28 +16.67%
BenchmarkAbsURLSrcset 29 32 +10.34%
BenchmarkXMLAbsURLSrcset 27 30 +11.11%
BenchmarkXMLAbsURL 12 14 +16.67%
benchmark old bytes new bytes delta
BenchmarkAbsURL 3154 3404 +7.93%
BenchmarkAbsURLSrcset 2376 2573 +8.29%
BenchmarkXMLAbsURLSrcset 2569 2763 +7.55%
BenchmarkXMLAbsURL 1888 1998 +5.83%
```
Fixes #1104
Fixes #622
Fixes #937
Fixes #157
Diffstat (limited to 'transform/chain_test.go')
-rw-r--r-- | transform/chain_test.go | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/transform/chain_test.go b/transform/chain_test.go index cd6dfcde9..808c20121 100644 --- a/transform/chain_test.go +++ b/transform/chain_test.go @@ -3,6 +3,7 @@ package transform import ( "bytes" "github.com/spf13/hugo/helpers" + "path/filepath" "strings" "testing" ) @@ -62,6 +63,11 @@ schemaless: <img srcset='//img.jpg' src='//basic.jpg'> schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST ` +const REL_PATH_VARIATIONS = `PRE. a href="/img/small.jpg" POST.` +const REL_PATH_VARIATIONS_CORRECT = `PRE. a href="../../img/small.jpg" POST.` + +const testBaseURL = "http://base/" + var abs_url_bench_tests = []test{ {H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_DQ}, {H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_SQ}, @@ -85,11 +91,13 @@ var srcset_xml_tests = []test{ {SRCSET_XML_SINGLE_QUOTE, SRCSET_XML_SINGLE_QUOTE_CORRECT}, {SRCSET_XML_VARIATIONS, SRCSET_XML_VARIATIONS_CORRECT}} +var relurl_tests = []test{{REL_PATH_VARIATIONS, REL_PATH_VARIATIONS_CORRECT}} + func TestChainZeroTransformers(t *testing.T) { tr := NewChain() in := new(bytes.Buffer) out := new(bytes.Buffer) - if err := tr.Apply(in, out); err != nil { + if err := tr.Apply(in, out, []byte("")); err != nil { t.Errorf("A zero transformer chain returned an error.") } } @@ -112,7 +120,7 @@ func TestChaingMultipleTransformers(t *testing.T) { tr := NewChain(f1, f2, f3, f4) out := new(bytes.Buffer) - if err := tr.Apply(out, helpers.StringToReader("Test: f4 f3 f1 f2 f1 The End.")); err != nil { + if err := tr.Apply(out, helpers.StringToReader("Test: f4 f3 f1 f2 f1 The End."), []byte("")); err != nil { t.Errorf("Multi transformer chain returned an error: %s", err) } @@ -124,8 +132,7 @@ func TestChaingMultipleTransformers(t *testing.T) { } func BenchmarkAbsURL(b *testing.B) { - absURL, _ := absURLFromURL("http://base") - tr := NewChain(absURL...) + tr := NewChain(AbsURL) b.ResetTimer() for i := 0; i < b.N; i++ { @@ -134,8 +141,7 @@ func BenchmarkAbsURL(b *testing.B) { } func BenchmarkAbsURLSrcset(b *testing.B) { - absURL, _ := absURLFromURL("http://base") - tr := NewChain(absURL...) + tr := NewChain(AbsURL) b.ResetTimer() for i := 0; i < b.N; i++ { @@ -144,8 +150,7 @@ func BenchmarkAbsURLSrcset(b *testing.B) { } func BenchmarkXMLAbsURLSrcset(b *testing.B) { - absXMLURL, _ := absURLInXMLFromURL("http://base") - tr := NewChain(absXMLURL...) + tr := NewChain(AbsURLInXML) b.ResetTimer() for i := 0; i < b.N; i++ { @@ -154,31 +159,33 @@ func BenchmarkXMLAbsURLSrcset(b *testing.B) { } func TestAbsURL(t *testing.T) { - absURL, _ := absURLFromURL("http://base") - tr := NewChain(absURL...) + tr := NewChain(AbsURL) apply(t.Errorf, tr, abs_url_tests) } +func TestRelativeURL(t *testing.T) { + tr := NewChain(AbsURL) + + applyWithPath(t.Errorf, tr, relurl_tests, helpers.GetDottedRelativePath(filepath.FromSlash("/post/sub/"))) + +} + func TestAbsURLSrcSet(t *testing.T) { - absURL, _ := absURLFromURL("http://base") - tr := NewChain(absURL...) + tr := NewChain(AbsURL) apply(t.Errorf, tr, srcset_tests) } func TestAbsXMLURLSrcSet(t *testing.T) { - absURLInXML, _ := absURLInXMLFromURL("http://base") - tr := NewChain(absURLInXML...) + tr := NewChain(AbsURLInXML) apply(t.Errorf, tr, srcset_xml_tests) - } func BenchmarkXMLAbsURL(b *testing.B) { - absURLInXML, _ := absURLInXMLFromURL("http://base") - tr := NewChain(absURLInXML...) + tr := NewChain(AbsURLInXML) b.ResetTimer() for i := 0; i < b.N; i++ { @@ -187,17 +194,17 @@ func BenchmarkXMLAbsURL(b *testing.B) { } func TestXMLAbsURL(t *testing.T) { - absURLInXML, _ := absURLInXMLFromURL("http://base") - tr := NewChain(absURLInXML...) + tr := NewChain(AbsURLInXML) apply(t.Errorf, tr, xml_abs_url_tests) } type errorf func(string, ...interface{}) -func apply(ef errorf, tr chain, tests []test) { +func applyWithPath(ef errorf, tr chain, tests []test, path string) { for _, test := range tests { out := new(bytes.Buffer) - err := tr.Apply(out, strings.NewReader(test.content)) + var err error + err = tr.Apply(out, strings.NewReader(test.content), []byte(path)) if err != nil { ef("Unexpected error: %s", err) } @@ -207,6 +214,10 @@ func apply(ef errorf, tr chain, tests []test) { } } +func apply(ef errorf, tr chain, tests []test) { + applyWithPath(ef, tr, tests, testBaseURL) +} + type test struct { content string expected string |