summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-04-22 10:26:26 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-04-22 10:57:37 +0200
commit47e7788b3c30de6fb895522096baf2c13598c317 (patch)
treedc95126b8e7685134c5ebf5bf98e7a5f77dd0dcb /tpl
parent51af1d2eadcad89e8c2906c05549352ef69ab016 (diff)
tpl/path: Add path.Ext, path.Dir and path.Base
Diffstat (limited to 'tpl')
-rw-r--r--tpl/path/init.go9
-rw-r--r--tpl/path/path.go48
2 files changed, 55 insertions, 2 deletions
diff --git a/tpl/path/init.go b/tpl/path/init.go
index ebfb59702..518dcad22 100644
--- a/tpl/path/init.go
+++ b/tpl/path/init.go
@@ -18,7 +18,6 @@ import (
"path/filepath"
"github.com/gohugoio/hugo/deps"
- "github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/tpl/internal"
)
@@ -41,11 +40,17 @@ func init() {
},
)
+ testDir := filepath.Join("my", "path")
+ testFile := filepath.Join(testDir, "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`},
+ {fmt.Sprintf(`{{ slice %q "filename.txt" | path.Join }}`, testDir), `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`},
},
)
diff --git a/tpl/path/path.go b/tpl/path/path.go
index d8b00eb39..fabf15018 100644
--- a/tpl/path/path.go
+++ b/tpl/path/path.go
@@ -45,6 +45,54 @@ func (df DirFile) String() string {
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,
// separating it into a directory and file name component.
// If there is no slash in path, Split returns an empty dir and