diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2015-05-16 01:11:39 +0300 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-05-16 01:11:44 +0300 |
commit | beaa8b1bcabd4be25ac26bea39ab9f7290147e67 (patch) | |
tree | 76095c2b6ec9ddf22477c188c87f47e4c6cee8d6 /target | |
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 'target')
-rw-r--r-- | target/file.go | 5 | ||||
-rw-r--r-- | target/page.go | 17 |
2 files changed, 16 insertions, 6 deletions
diff --git a/target/file.go b/target/file.go index ea023d2bb..01037a72b 100644 --- a/target/file.go +++ b/target/file.go @@ -16,6 +16,11 @@ type Translator interface { Translate(string) (string, error) } +// TODO(bep) consider other ways to solve this. +type OptionalTranslator interface { + TranslateRelative(string) (string, error) +} + type Output interface { Publisher Translator diff --git a/target/page.go b/target/page.go index 57e3a300b..98e6dc8d4 100644 --- a/target/page.go +++ b/target/page.go @@ -32,10 +32,18 @@ func (pp *PagePub) Publish(path string, r io.Reader) (err error) { } func (pp *PagePub) Translate(src string) (dest string, err error) { + dir, err := pp.TranslateRelative(src) + if err != nil { + return dir, err + } + if pp.PublishDir != "" { + dir = filepath.Join(pp.PublishDir, dir) + } + return dir, nil +} + +func (pp *PagePub) TranslateRelative(src string) (dest string, err error) { if src == helpers.FilePathSeparator { - if pp.PublishDir != "" { - return filepath.Join(pp.PublishDir, "index.html"), nil - } return "index.html", nil } @@ -43,9 +51,6 @@ func (pp *PagePub) Translate(src string) (dest string, err error) { isRoot := dir == "" ext := pp.extension(filepath.Ext(file)) name := filename(file) - if pp.PublishDir != "" { - dir = filepath.Join(pp.PublishDir, dir) - } if pp.UglyURLs || file == "index.html" || (isRoot && file == "404.html") { return filepath.Join(dir, fmt.Sprintf("%s%s", name, ext)), nil |