diff options
author | spf13 <steve.francia@gmail.com> | 2014-05-02 09:04:14 +0400 |
---|---|---|
committer | spf13 <steve.francia@gmail.com> | 2014-05-02 09:04:14 +0400 |
commit | bff1f1e6893a365056afd8abb47b81c592c12d55 (patch) | |
tree | 062cd1b42bd0f985fe0a0858aee4a9d2f0341ff1 /helpers | |
parent | ef2ad4d91fa41b364f21d290a12e977cc5cf676f (diff) |
Adding some new methods to helpers (GuessSection, MakeTitle & Filename)
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/path.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/helpers/path.go b/helpers/path.go index e71302617..119974a3c 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -34,6 +34,10 @@ func MakePath(s string) string { return UnicodeSanitize(strings.ToLower(strings.Replace(strings.TrimSpace(s), " ", "-", -1))) } +func MakeTitle(inpath string) string { + return strings.Replace(strings.TrimSpace(inpath), "-", " ", -1) +} + func Sanitize(s string) string { return sanitizeRegexp.ReplaceAllString(s, "") } @@ -88,6 +92,11 @@ func AbsPathify(inPath string) string { return filepath.Clean(filepath.Join(viper.GetString("WorkingDir"), inPath)) } +func Filename(in string) (name string) { + name, _ = FileAndExt(in) + return +} + func FileAndExt(in string) (name string, ext string) { ext = path.Ext(in) base := path.Base(in) @@ -101,6 +110,18 @@ func FileAndExt(in string) (name string, ext string) { return } +func GuessSection(in string) string { + x := strings.Split(in, "/") + x = x[:len(x)-1] + if len(x) == 0 { + return "" + } + if x[0] == "content" { + x = x[1:] + } + return path.Join(x...) +} + func PathPrep(ugly bool, in string) string { if ugly { return Uglify(in) @@ -154,6 +175,35 @@ func FindCWD() (string, error) { return path, nil } +func SafeWriteToDisk(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 { + return + } + } + + exists, err := Exists(inpath) + if err != nil { + return + } + if exists { + return fmt.Errorf("%v already exists", inpath) + } + + file, err := os.Create(inpath) + if err != nil { + return + } + defer file.Close() + + _, err = io.Copy(file, r) + return +} + func WriteToDisk(inpath string, r io.Reader) (err error) { dir, _ := filepath.Split(inpath) ospath := filepath.FromSlash(dir) |