ref: 4004687fb2da9228203fec39b914ba534c934966
parent: 7919603fb58bbbec2cdef2e46ec2e3c3571c46c1
author: Noah Campbell <[email protected]>
date: Sun Sep 1 05:56:58 EDT 2013
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.
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -329,21 +329,11 @@
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 @@
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 @@
}
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 @@
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 @@
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 @@
if err != nil {
return err
}
- err = s.WritePublic("index.html", x.Bytes())
+ err = s.WritePublic("/", x.Bytes())
if err != nil {
return err
}
@@ -677,8 +649,11 @@
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 {
@@ -685,17 +660,5 @@
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))
}
--- a/target/file.go
+++ b/target/file.go
@@ -3,7 +3,9 @@
import (
"fmt"
"io"
+ "os"
"path"
+ "path/filepath"
)
type Publisher interface {
@@ -17,9 +19,39 @@
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
}
--- a/target/file_test.go
+++ b/target/file_test.go
@@ -9,6 +9,8 @@
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 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)
+ }
}
}