mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Add Translate to target
Translate handles Ugly Urls.
This commit is contained in:
parent
c6ad532b94
commit
7919603fb5
4 changed files with 98 additions and 3 deletions
|
@ -1,8 +1,8 @@
|
|||
package hugolib
|
||||
|
||||
import (
|
||||
"io"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
func (s *Site) ShowPlan(out io.Writer) (err error) {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package hugolib
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func checkShowPlanExpected(t *testing.T, expected, got string) {
|
||||
|
@ -15,7 +15,7 @@ func TestDegenerateNoFiles(t *testing.T) {
|
|||
s := new(Site)
|
||||
out := new(bytes.Buffer)
|
||||
if err := s.ShowPlan(out); err != nil {
|
||||
t.Errorf("ShowPlan unexpectedly returned an error: %s", err)
|
||||
t.Errorf("ShowPlan unexpectedly returned an error: %s", err)
|
||||
}
|
||||
expected := "No source files provided.\n"
|
||||
got := out.String()
|
||||
|
|
|
@ -1,9 +1,53 @@
|
|||
package target
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"path"
|
||||
)
|
||||
|
||||
type Publisher interface {
|
||||
Publish(string, io.Reader) error
|
||||
}
|
||||
|
||||
type Translator interface {
|
||||
Translate(string) (string, error)
|
||||
}
|
||||
|
||||
type Filesystem struct {
|
||||
UglyUrls bool
|
||||
DefaultExtension string
|
||||
}
|
||||
|
||||
func (fs *Filesystem) Translate(src string) (dest string, err error) {
|
||||
if fs.UglyUrls {
|
||||
return src, nil
|
||||
}
|
||||
|
||||
dir, file := path.Split(src)
|
||||
ext := fs.extension(path.Ext(file))
|
||||
name := filename(file)
|
||||
|
||||
return path.Join(dir, name, fmt.Sprintf("index%s", ext)), nil
|
||||
}
|
||||
|
||||
func (fs *Filesystem) extension(ext string) string {
|
||||
if ext != "" {
|
||||
return ext
|
||||
}
|
||||
|
||||
if fs.DefaultExtension != "" {
|
||||
return fs.DefaultExtension
|
||||
}
|
||||
|
||||
return ".html"
|
||||
}
|
||||
|
||||
func filename(f string) string {
|
||||
ext := path.Ext(f)
|
||||
if ext == "" {
|
||||
return f
|
||||
}
|
||||
|
||||
return f[:len(f)-len(ext)]
|
||||
}
|
||||
|
|
51
target/file_test.go
Normal file
51
target/file_test.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package target
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFileTranslator(t *testing.T) {
|
||||
tests := []struct {
|
||||
content string
|
||||
expected string
|
||||
}{
|
||||
{"foo", "foo/index.html"},
|
||||
{"foo.html", "foo/index.html"},
|
||||
{"foo.xhtml", "foo/index.xhtml"},
|
||||
{"section/foo", "section/foo/index.html"},
|
||||
{"section/foo.html", "section/foo/index.html"},
|
||||
{"section/foo.rss", "section/foo/index.rss"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
f := new(Filesystem)
|
||||
dest, err := f.Translate(test.content)
|
||||
if err != nil {
|
||||
t.Fatalf("Translate returned and unexpected err: %s", err)
|
||||
}
|
||||
|
||||
if dest != test.expected {
|
||||
t.Errorf("Tranlate expected return: %s, got: %s", test.expected, dest)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslateUglyUrls(t *testing.T) {
|
||||
f := &Filesystem{UglyUrls: true}
|
||||
dest, err := f.Translate("foo.html")
|
||||
if err != nil {
|
||||
t.Fatalf("Translate returned an unexpected err: %s", err)
|
||||
}
|
||||
|
||||
if dest != "foo.html" {
|
||||
t.Errorf("Translate expected return: %s, got: %s", "foo.html", dest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslateDefaultExtension(t *testing.T) {
|
||||
f := &Filesystem{DefaultExtension: ".foobar"}
|
||||
dest, _ := f.Translate("baz")
|
||||
if dest != "baz/index.foobar" {
|
||||
t.Errorf("Translate expected return: %s, got %s", "baz/index.foobar", dest)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue