tpl/path: Add path.Join

This commit is contained in:
Bjørn Erik Pedersen 2018-04-15 21:06:57 +02:00
parent 01b72eb592
commit 880ca19f20
2 changed files with 46 additions and 0 deletions

View file

@ -14,7 +14,11 @@
package path package path
import ( import (
"fmt"
"path/filepath"
"github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/tpl/internal" "github.com/gohugoio/hugo/tpl/internal"
) )
@ -33,6 +37,15 @@ func init() {
nil, nil,
[][2]string{ [][2]string{
{`{{ "/my/path/filename.txt" | path.Split }}`, `/my/path/|filename.txt`}, {`{{ "/my/path/filename.txt" | path.Split }}`, `/my/path/|filename.txt`},
{fmt.Sprintf(`{{ %q | path.Split }}`, filepath.FromSlash("/my/path/filename.txt")), `/my/path/|filename.txt`},
},
)
ns.AddMethodMapping(ctx.Join,
nil,
[][2]string{
{fmt.Sprintf(`{{ slice %q "filename.txt" | path.Join }}`, "my"+helpers.FilePathSeparator+"path"), `my/path/filename.txt`},
{`{{ path.Join "my" "path" "filename.txt" }}`, `my/path/filename.txt`},
}, },
) )

View file

@ -16,6 +16,7 @@ package path
import ( import (
"fmt" "fmt"
_path "path" _path "path"
"path/filepath"
"github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/deps"
"github.com/spf13/cast" "github.com/spf13/cast"
@ -48,13 +49,45 @@ func (df DirFile) String() string {
// separating it into a directory and file name component. // separating it into a directory and file name component.
// If there is no slash in path, Split returns an empty dir and // If there is no slash in path, Split returns an empty dir and
// file set to path. // file set to path.
// The input path is passed into filepath.ToSlash converting any Windows slashes
// to forward slashes.
// The returned values have the property that path = dir+file. // The returned values have the property that path = dir+file.
func (ns *Namespace) Split(path interface{}) (DirFile, error) { func (ns *Namespace) Split(path interface{}) (DirFile, error) {
spath, err := cast.ToStringE(path) spath, err := cast.ToStringE(path)
if err != nil { if err != nil {
return DirFile{}, err return DirFile{}, err
} }
spath = filepath.ToSlash(spath)
dir, file := _path.Split(spath) dir, file := _path.Split(spath)
return DirFile{Dir: dir, File: file}, nil return DirFile{Dir: dir, File: file}, nil
} }
// Join joins any number of path elements into a single path, adding a
// separating slash if necessary. All the input
// path elements are passed into filepath.ToSlash converting any Windows slashes
// to forward slashes.
// The result is Cleaned; in particular,
// all empty strings are ignored.
func (ns *Namespace) Join(elements ...interface{}) (string, error) {
var pathElements []string
for _, elem := range elements {
switch v := elem.(type) {
case []interface{}:
for _, e := range v {
elemStr, err := cast.ToStringE(e)
if err != nil {
return "", err
}
pathElements = append(pathElements, filepath.ToSlash(elemStr))
}
default:
elemStr, err := cast.ToStringE(elem)
if err != nil {
return "", err
}
pathElements = append(pathElements, filepath.ToSlash(elemStr))
}
}
return _path.Join(pathElements...), nil
}