commit 4004687fb2da9228203fec39b914ba534c934966
parent 7919603fb58bbbec2cdef2e46ec2e3c3571c46c1
Author: Noah Campbell <noahcampbell@gmail.com>
Date: Sun, 1 Sep 2013 09:56:58 -0700
Move to target.Filesystem
Moving the ugly urls logic to the target. There is still UglyUrl logic
in page for the permlink but this is dealing with the generate of urls.
Diffstat:
3 files changed, 65 insertions(+), 57 deletions(-)
diff --git a/hugolib/site.go b/hugolib/site.go
@@ -329,21 +329,11 @@ func (s *Site) setOutFile(p *Page) {
var outfile string
if len(strings.TrimSpace(p.Slug)) > 0 {
- // Use Slug if provided
- if s.Config.UglyUrls {
- outfile = strings.TrimSpace(p.Slug) + "." + p.Extension
- } else {
- outfile = filepath.Join(strings.TrimSpace(p.Slug), "index."+p.Extension)
- }
+ outfile = strings.TrimSpace(p.Slug) + "." + p.Extension
} else {
// Fall back to filename
_, t := filepath.Split(p.FileName)
- if s.Config.UglyUrls {
- outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
- } else {
- file, _ := fileExt(strings.TrimSpace(t))
- outfile = filepath.Join(file, "index."+p.Extension)
- }
+ outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
}
p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile)
@@ -467,13 +457,8 @@ func (s *Site) RenderIndexes() error {
n := s.NewNode()
n.Title = strings.Title(k)
url := helpers.Urlize(plural + "/" + k)
- plink := url
- if s.Config.UglyUrls {
- n.Url = url + ".html"
- plink = n.Url
- } else {
- n.Url = url + "/index.html"
- }
+ n.Url = url + ".html"
+ plink := n.Url
n.Permalink = permalink(s, plink)
n.RSSlink = permalink(s, url+".xml")
n.Date = o[0].Date
@@ -486,12 +471,7 @@ func (s *Site) RenderIndexes() error {
}
var base string
- if s.Config.UglyUrls {
- base = plural + "/" + k
- } else {
- base = plural + "/" + k + "/" + "index"
- }
-
+ base = plural + "/" + k
err = s.WritePublic(base+".html", x.Bytes())
if err != nil {
return err
@@ -500,11 +480,7 @@ func (s *Site) RenderIndexes() error {
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
// XML Feed
y := s.NewXMLBuffer()
- if s.Config.UglyUrls {
- n.Url = helpers.Urlize(plural + "/" + k + ".xml")
- } else {
- n.Url = helpers.Urlize(plural + "/" + k + "/" + "index.xml")
- }
+ n.Url = helpers.Urlize(plural + "/" + k + ".xml")
n.Permalink = permalink(s, n.Url)
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
err = s.WritePublic(base+".xml", y.Bytes())
@@ -567,11 +543,7 @@ func (s *Site) RenderLists() error {
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
// XML Feed
- if s.Config.UglyUrls {
- n.Url = helpers.Urlize(section + ".xml")
- } else {
- n.Url = helpers.Urlize(section + "/" + "index.xml")
- }
+ n.Url = Urlize(section + ".xml")
n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
y := s.NewXMLBuffer()
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
@@ -600,7 +572,7 @@ func (s *Site) RenderHomePage() error {
if err != nil {
return err
}
- err = s.WritePublic("index.html", x.Bytes())
+ err = s.WritePublic("/", x.Bytes())
if err != nil {
return err
}
@@ -677,25 +649,16 @@ func (s *Site) NewXMLBuffer() *bytes.Buffer {
func (s *Site) WritePublic(path string, content []byte) (err error) {
- if s.Target != nil {
- return s.Target.Publish(path, bytes.NewReader(content))
+ if s.Target == nil {
+ s.Target = &target.Filesystem{
+ PublishDir: s.absPublishDir(),
+ UglyUrls: s.Config.UglyUrls,
+ }
}
if s.Config.Verbose {
fmt.Println(path)
}
- path, filename := filepath.Split(path)
-
- path = filepath.FromSlash(s.Config.GetAbsPath(filepath.Join(s.Config.PublishDir, path)))
- err = mkdirIf(path)
- if err != nil {
- return
- }
-
- file, _ := os.Create(filepath.Join(path, filename))
- defer file.Close()
-
- _, err = file.Write(content)
- return
+ return s.Target.Publish(path, bytes.NewReader(content))
}
diff --git a/target/file.go b/target/file.go
@@ -3,7 +3,9 @@ package target
import (
"fmt"
"io"
+ "os"
"path"
+ "path/filepath"
)
type Publisher interface {
@@ -17,9 +19,39 @@ type Translator interface {
type Filesystem struct {
UglyUrls bool
DefaultExtension string
+ PublishDir string
+}
+
+func (fs *Filesystem) Publish(path string, r io.Reader) (err error) {
+
+ translated, err := fs.Translate(path)
+ if err != nil {
+ return
+ }
+
+ path, _ = filepath.Split(translated)
+ dest := filepath.Join(fs.PublishDir, path)
+ ospath := filepath.FromSlash(dest)
+
+ err = os.MkdirAll(ospath, 0764) // rwx, rw, r
+ if err != nil {
+ return
+ }
+
+ file, err := os.Create(filepath.Join(fs.PublishDir, translated))
+ if err != nil {
+ return
+ }
+ defer file.Close()
+
+ _, err = io.Copy(file, r)
+ return
}
func (fs *Filesystem) Translate(src string) (dest string, err error) {
+ if src == "/" {
+ return "index.html", nil
+ }
if fs.UglyUrls {
return src, nil
}
diff --git a/target/file_test.go b/target/file_test.go
@@ -9,6 +9,8 @@ func TestFileTranslator(t *testing.T) {
content string
expected string
}{
+ {"/", "index.html"},
+ {"index.html", "index/index.html"},
{"foo", "foo/index.html"},
{"foo.html", "foo/index.html"},
{"foo.xhtml", "foo/index.xhtml"},
@@ -31,14 +33,25 @@ func TestFileTranslator(t *testing.T) {
}
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)
+ tests := []struct {
+ content string
+ expected string
+ }{
+ {"foo.html", "foo.html"},
+ {"/", "index.html"},
+ {"index.html", "index.html"},
}
- if dest != "foo.html" {
- t.Errorf("Translate expected return: %s, got: %s", "foo.html", dest)
+ for _, test := range tests {
+ f := &Filesystem{UglyUrls: true}
+ dest, err := f.Translate(test.content)
+ if err != nil {
+ t.Fatalf("Translate returned an unexpected err: %s", err)
+ }
+
+ if dest != test.expected {
+ t.Errorf("Translate expected return: %s, got: %s", test.expected, dest)
+ }
}
}