moving writeToDisk to helpers to make it more accessible

This commit is contained in:
spf13 2014-05-01 13:13:11 -04:00
parent f271faea06
commit 58f8b43fee
3 changed files with 29 additions and 26 deletions

View file

@ -15,12 +15,14 @@ package helpers
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"unicode" "unicode"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -151,3 +153,24 @@ func FindCWD() (string, error) {
return path, nil return path, nil
} }
func WriteToDisk(inpath string, r io.Reader) (err error) {
dir, _ := filepath.Split(inpath)
ospath := filepath.FromSlash(dir)
if ospath != "" {
err = os.MkdirAll(ospath, 0777) // rwx, rw, r
if err != nil {
panic(err)
}
}
file, err := os.Create(inpath)
if err != nil {
return
}
defer file.Close()
_, err = io.Copy(file, r)
return
}

View file

@ -3,9 +3,9 @@ package target
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"path" "path"
"path/filepath"
"github.com/spf13/hugo/helpers"
) )
type Publisher interface { type Publisher interface {
@ -34,28 +34,7 @@ func (fs *Filesystem) Publish(path string, r io.Reader) (err error) {
return return
} }
return writeToDisk(translated, r) return helpers.WriteToDisk(translated, r)
}
func writeToDisk(translated string, r io.Reader) (err error) {
path, _ := filepath.Split(translated)
ospath := filepath.FromSlash(path)
if ospath != "" {
err = os.MkdirAll(ospath, 0777) // rwx, rw, r
if err != nil {
panic(err)
}
}
file, err := os.Create(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) {

View file

@ -2,10 +2,11 @@ package target
import ( import (
"bytes" "bytes"
"github.com/spf13/hugo/helpers"
"html/template" "html/template"
"path" "path"
"strings" "strings"
"github.com/spf13/hugo/helpers"
) )
const ALIAS = "<!DOCTYPE html><html><head><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" /></head></html>" const ALIAS = "<!DOCTYPE html><html><head><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" /></head></html>"
@ -67,5 +68,5 @@ func (h *HTMLRedirectAlias) Publish(path string, permalink template.HTML) (err e
return return
} }
return writeToDisk(path, buffer) return helpers.WriteToDisk(path, buffer)
} }