shithub: hugo

Download patch

ref: 47e7788b3c30de6fb895522096baf2c13598c317
parent: 51af1d2eadcad89e8c2906c05549352ef69ab016
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Apr 22 06:26:26 EDT 2018

tpl/path: Add path.Ext, path.Dir and path.Base

--- a/tpl/path/init.go
+++ b/tpl/path/init.go
@@ -18,7 +18,6 @@
 	"path/filepath"
 
 	"github.com/gohugoio/hugo/deps"
-	"github.com/gohugoio/hugo/helpers"
 	"github.com/gohugoio/hugo/tpl/internal"
 )
 
@@ -41,11 +40,17 @@
 			},
 		)
 
+		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`},
 			},
 		)
 
--- a/tpl/path/path.go
+++ b/tpl/path/path.go
@@ -45,6 +45,54 @@
 	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