Adding some new methods to helpers (GuessSection, MakeTitle & Filename)

This commit is contained in:
spf13 2014-05-02 01:04:14 -04:00
parent ef2ad4d91f
commit bff1f1e689

View file

@ -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)