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.
This commit is contained in:
Noah Campbell 2013-09-01 09:56:58 -07:00
parent 7919603fb5
commit 4004687fb2
3 changed files with 65 additions and 57 deletions

View file

@ -329,21 +329,11 @@ func (s *Site) setOutFile(p *Page) {
var outfile string var outfile string
if len(strings.TrimSpace(p.Slug)) > 0 { if len(strings.TrimSpace(p.Slug)) > 0 {
// Use Slug if provided outfile = strings.TrimSpace(p.Slug) + "." + p.Extension
if s.Config.UglyUrls {
outfile = strings.TrimSpace(p.Slug) + "." + p.Extension
} else {
outfile = filepath.Join(strings.TrimSpace(p.Slug), "index."+p.Extension)
}
} else { } else {
// Fall back to filename // Fall back to filename
_, t := filepath.Split(p.FileName) _, t := filepath.Split(p.FileName)
if s.Config.UglyUrls { outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
} else {
file, _ := fileExt(strings.TrimSpace(t))
outfile = filepath.Join(file, "index."+p.Extension)
}
} }
p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile) p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile)
@ -467,13 +457,8 @@ func (s *Site) RenderIndexes() error {
n := s.NewNode() n := s.NewNode()
n.Title = strings.Title(k) n.Title = strings.Title(k)
url := helpers.Urlize(plural + "/" + k) url := helpers.Urlize(plural + "/" + k)
plink := url n.Url = url + ".html"
if s.Config.UglyUrls { plink := n.Url
n.Url = url + ".html"
plink = n.Url
} else {
n.Url = url + "/index.html"
}
n.Permalink = permalink(s, plink) n.Permalink = permalink(s, plink)
n.RSSlink = permalink(s, url+".xml") n.RSSlink = permalink(s, url+".xml")
n.Date = o[0].Date n.Date = o[0].Date
@ -486,12 +471,7 @@ func (s *Site) RenderIndexes() error {
} }
var base string var base string
if s.Config.UglyUrls { base = plural + "/" + k
base = plural + "/" + k
} else {
base = plural + "/" + k + "/" + "index"
}
err = s.WritePublic(base+".html", x.Bytes()) err = s.WritePublic(base+".html", x.Bytes())
if err != nil { if err != nil {
return err return err
@ -500,11 +480,7 @@ func (s *Site) RenderIndexes() error {
if a := s.Tmpl.Lookup("rss.xml"); a != nil { if a := s.Tmpl.Lookup("rss.xml"); a != nil {
// XML Feed // XML Feed
y := s.NewXMLBuffer() y := s.NewXMLBuffer()
if s.Config.UglyUrls { n.Url = helpers.Urlize(plural + "/" + k + ".xml")
n.Url = helpers.Urlize(plural + "/" + k + ".xml")
} else {
n.Url = helpers.Urlize(plural + "/" + k + "/" + "index.xml")
}
n.Permalink = permalink(s, n.Url) n.Permalink = permalink(s, n.Url)
s.Tmpl.ExecuteTemplate(y, "rss.xml", n) s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
err = s.WritePublic(base+".xml", y.Bytes()) 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 { if a := s.Tmpl.Lookup("rss.xml"); a != nil {
// XML Feed // XML Feed
if s.Config.UglyUrls { n.Url = Urlize(section + ".xml")
n.Url = helpers.Urlize(section + ".xml")
} else {
n.Url = helpers.Urlize(section + "/" + "index.xml")
}
n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url) n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
y := s.NewXMLBuffer() y := s.NewXMLBuffer()
s.Tmpl.ExecuteTemplate(y, "rss.xml", n) s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
@ -600,7 +572,7 @@ func (s *Site) RenderHomePage() error {
if err != nil { if err != nil {
return err return err
} }
err = s.WritePublic("index.html", x.Bytes()) err = s.WritePublic("/", x.Bytes())
if err != nil { if err != nil {
return err return err
} }
@ -677,25 +649,16 @@ func (s *Site) NewXMLBuffer() *bytes.Buffer {
func (s *Site) WritePublic(path string, content []byte) (err error) { func (s *Site) WritePublic(path string, content []byte) (err error) {
if s.Target != nil { if s.Target == nil {
return s.Target.Publish(path, bytes.NewReader(content)) s.Target = &target.Filesystem{
PublishDir: s.absPublishDir(),
UglyUrls: s.Config.UglyUrls,
}
} }
if s.Config.Verbose { if s.Config.Verbose {
fmt.Println(path) fmt.Println(path)
} }
path, filename := filepath.Split(path) return s.Target.Publish(path, bytes.NewReader(content))
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
} }

View file

@ -3,7 +3,9 @@ package target
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"path" "path"
"path/filepath"
) )
type Publisher interface { type Publisher interface {
@ -17,9 +19,39 @@ type Translator interface {
type Filesystem struct { type Filesystem struct {
UglyUrls bool UglyUrls bool
DefaultExtension string 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) { func (fs *Filesystem) Translate(src string) (dest string, err error) {
if src == "/" {
return "index.html", nil
}
if fs.UglyUrls { if fs.UglyUrls {
return src, nil return src, nil
} }

View file

@ -9,6 +9,8 @@ func TestFileTranslator(t *testing.T) {
content string content string
expected string expected string
}{ }{
{"/", "index.html"},
{"index.html", "index/index.html"},
{"foo", "foo/index.html"}, {"foo", "foo/index.html"},
{"foo.html", "foo/index.html"}, {"foo.html", "foo/index.html"},
{"foo.xhtml", "foo/index.xhtml"}, {"foo.xhtml", "foo/index.xhtml"},
@ -31,14 +33,25 @@ func TestFileTranslator(t *testing.T) {
} }
func TestTranslateUglyUrls(t *testing.T) { func TestTranslateUglyUrls(t *testing.T) {
f := &Filesystem{UglyUrls: true} tests := []struct {
dest, err := f.Translate("foo.html") content string
if err != nil { expected string
t.Fatalf("Translate returned an unexpected err: %s", err) }{
{"foo.html", "foo.html"},
{"/", "index.html"},
{"index.html", "index.html"},
} }
if dest != "foo.html" { for _, test := range tests {
t.Errorf("Translate expected return: %s, got: %s", "foo.html", dest) 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)
}
} }
} }