2017-06-23 15:32:08 +02:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
2017-07-02 03:12:22 +02:00
|
|
|
"path/filepath"
|
2017-06-23 15:32:08 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-07-02 03:12:22 +02:00
|
|
|
// MustRel is like filepath.Rel, but panics if the path cannot be relativized.
|
|
|
|
func MustRel(basepath, targpath string) string {
|
|
|
|
relpath, err := filepath.Rel(basepath, targpath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return relpath
|
|
|
|
}
|
|
|
|
|
2017-06-23 15:32:08 +02:00
|
|
|
// TrimExt returns a path without its extension, if any
|
|
|
|
func TrimExt(name string) string {
|
|
|
|
return name[:len(name)-len(path.Ext(name))]
|
|
|
|
}
|
|
|
|
|
|
|
|
// URLPathClean removes internal // etc. Unlike path.Clean, it
|
|
|
|
// leaves the final "/" intact.
|
2017-06-29 01:00:01 +02:00
|
|
|
func URLPathClean(url string) string {
|
|
|
|
finalSlash := false
|
|
|
|
if strings.HasSuffix(url, "/") && len(url) > 1 {
|
|
|
|
finalSlash = true
|
2017-06-23 15:32:08 +02:00
|
|
|
}
|
2017-06-29 01:00:01 +02:00
|
|
|
cleaned := path.Clean(url)
|
|
|
|
if finalSlash && !strings.HasSuffix(cleaned, "/") {
|
|
|
|
cleaned += "/"
|
|
|
|
}
|
|
|
|
return cleaned
|
2017-06-23 15:32:08 +02:00
|
|
|
}
|