Skip to content

Commit

Permalink
tpl/path: Add path.Ext, path.Dir and path.Base
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Apr 22, 2018
1 parent 51af1d2 commit 47e7788
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tpl/path/init.go
Expand Up @@ -18,7 +18,6 @@ import (
"path/filepath" "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"
) )


Expand All @@ -41,11 +40,17 @@ func init() {
}, },
) )


testDir := filepath.Join("my", "path")
testFile := filepath.Join(testDir, "filename.txt")

ns.AddMethodMapping(ctx.Join, ns.AddMethodMapping(ctx.Join,
nil, nil,
[][2]string{ [][2]string{
{fmt.Sprintf(`{{ slice %q "filename.txt" | path.Join }}`, "my"+helpers.FilePathSeparator+"path"), `my/path/filename.txt`}, {fmt.Sprintf(`{{ slice %q "filename.txt" | path.Join }}`, testDir), `my/path/filename.txt`},
{`{{ path.Join "my" "path" "filename.txt" }}`, `my/path/filename.txt`}, {`{{ path.Join "my" "path" "filename.txt" }}`, `my/path/filename.txt`},
{fmt.Sprintf(`{{ %q | path.Ext }}`, testFile), `.txt`},
{fmt.Sprintf(`{{ %q | path.Base }}`, testFile), `filename.txt`},
{fmt.Sprintf(`{{ %q | path.Dir }}`, testFile), `my/path`},
}, },
) )


Expand Down
48 changes: 48 additions & 0 deletions tpl/path/path.go
Expand Up @@ -45,6 +45,54 @@ func (df DirFile) String() string {
return fmt.Sprintf("%s|%s", df.Dir, df.File) return fmt.Sprintf("%s|%s", df.Dir, df.File)
} }


// Ext returns the file name extension used by path.
// The extension is the suffix beginning at the final dot
// in the final slash-separated element of path;
// it is empty if there is no dot.
// The input path is passed into filepath.ToSlash converting any Windows slashes
// to forward slashes.
func (ns *Namespace) Ext(path interface{}) (string, error) {
spath, err := cast.ToStringE(path)
if err != nil {
return "", err
}
spath = filepath.ToSlash(spath)
return _path.Ext(spath), nil
}

// Dir returns all but the last element of path, typically the path's directory.
// After dropping the final element using Split, the path is Cleaned and trailing
// slashes are removed.
// If the path is empty, Dir returns ".".
// If the path consists entirely of slashes followed by non-slash bytes, Dir
// returns a single slash. In any other case, the returned path does not end in a
// slash.
// The input path is passed into filepath.ToSlash converting any Windows slashes
// to forward slashes.
func (ns *Namespace) Dir(path interface{}) (string, error) {
spath, err := cast.ToStringE(path)
if err != nil {
return "", err
}
spath = filepath.ToSlash(spath)
return _path.Dir(spath), nil
}

// Base returns the last element of path.
// Trailing slashes are removed before extracting the last element.
// If the path is empty, Base returns ".".
// If the path consists entirely of slashes, Base returns "/".
// The input path is passed into filepath.ToSlash converting any Windows slashes
// to forward slashes.
func (ns *Namespace) Base(path interface{}) (string, error) {
spath, err := cast.ToStringE(path)
if err != nil {
return "", err
}
spath = filepath.ToSlash(spath)
return _path.Base(spath), nil
}

// Split splits path immediately following the final slash, // Split splits path immediately following the final slash,
// 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
Expand Down

0 comments on commit 47e7788

Please sign in to comment.