diff options
author | Noah Campbell <noahcampbell@gmail.com> | 2013-09-01 20:56:58 +0400 |
---|---|---|
committer | Noah Campbell <noahcampbell@gmail.com> | 2013-09-04 07:01:55 +0400 |
commit | 4004687fb2da9228203fec39b914ba534c934966 (patch) | |
tree | 6d285d3248933a751215fbd39ce2da86df75b93a /target | |
parent | 7919603fb58bbbec2cdef2e46ec2e3c3571c46c1 (diff) |
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 (limited to 'target')
-rw-r--r-- | target/file.go | 32 | ||||
-rw-r--r-- | target/file_test.go | 25 |
2 files changed, 51 insertions, 6 deletions
diff --git a/target/file.go b/target/file.go index ac7ab105c..8e3423168 100644 --- 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 index 1c31f5c6a..14e47ee6f 100644 --- 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) + } } } |