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
This commit is contained in:
bep 2015-05-16 00:11:39 +02:00
parent e522e5f415
commit beaa8b1bca
10 changed files with 181 additions and 110 deletions

View file

@ -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