mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Adding some new methods to helpers (GuessSection, MakeTitle & Filename)
This commit is contained in:
parent
ef2ad4d91f
commit
bff1f1e689
1 changed files with 50 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue