mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
beaa8b1bca
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
77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
package target
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"io"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/hugo/helpers"
|
|
"github.com/spf13/hugo/hugofs"
|
|
)
|
|
|
|
type PagePublisher interface {
|
|
Translator
|
|
Publish(string, template.HTML) error
|
|
}
|
|
|
|
type PagePub struct {
|
|
UglyURLs bool
|
|
DefaultExtension string
|
|
PublishDir string
|
|
}
|
|
|
|
func (pp *PagePub) Publish(path string, r io.Reader) (err error) {
|
|
|
|
translated, err := pp.Translate(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
return helpers.WriteToDisk(translated, r, hugofs.DestinationFS)
|
|
}
|
|
|
|
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 {
|
|
return "index.html", nil
|
|
}
|
|
|
|
dir, file := filepath.Split(src)
|
|
isRoot := dir == ""
|
|
ext := pp.extension(filepath.Ext(file))
|
|
name := filename(file)
|
|
|
|
if pp.UglyURLs || file == "index.html" || (isRoot && file == "404.html") {
|
|
return filepath.Join(dir, fmt.Sprintf("%s%s", name, ext)), nil
|
|
}
|
|
|
|
return filepath.Join(dir, name, fmt.Sprintf("index%s", ext)), nil
|
|
}
|
|
|
|
func (pp *PagePub) extension(ext string) string {
|
|
switch ext {
|
|
case ".md", ".rst": // TODO make this list configurable. page.go has the list of markup types.
|
|
return ".html"
|
|
}
|
|
|
|
if ext != "" {
|
|
return ext
|
|
}
|
|
|
|
if pp.DefaultExtension != "" {
|
|
return pp.DefaultExtension
|
|
}
|
|
|
|
return ".html"
|
|
}
|