]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/path: Add path.BaseName function
authorJoe Mooring <joe.mooring@veriphor.com>
Sat, 4 Jun 2022 20:40:32 +0000 (13:40 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 6 Jun 2022 07:36:16 +0000 (09:36 +0200)
Closes #9973

docs/content/en/functions/path.Base.md
docs/content/en/functions/path.BaseName.md [new file with mode: 0644]
docs/content/en/functions/path.Clean.md
docs/content/en/functions/path.Dir.md
docs/content/en/functions/path.Ext.md
docs/content/en/functions/path.Join.md
docs/content/en/functions/path.Split.md
tpl/path/path.go
tpl/path/path_test.go

index c44518a727b30ea6fa4ed8bcebf6903e0b007f1e..a6bfc2bd5da59665d0652b8eef731ed8862601b0 100644 (file)
@@ -12,7 +12,7 @@ keywords: [path, base]
 signature: ["path.Base PATH"]
 workson: []
 hugoversion: "0.40"
-relatedfuncs: [path.Dir, path.Ext, path.Split]
+relatedfuncs: [path.BaseName, path.Clean, path.Dir, path.Ext, path.Join, path.Split]
 deprecated: false
 ---
 
diff --git a/docs/content/en/functions/path.BaseName.md b/docs/content/en/functions/path.BaseName.md
new file mode 100644 (file)
index 0000000..6a5b9f0
--- /dev/null
@@ -0,0 +1,24 @@
+---
+title: path.BaseName
+description: BaseName returns the last element of a path, removing the extension if present.
+date: 2022-06-04
+categories: [functions]
+menu:
+  docs:
+    parent: "functions"
+keywords: [path, base]
+signature: ["path.BaseName PATH"]
+relatedfuncs: [path.Base, path.Clean, path.Dir, path.Ext, path.Join, path.Split]
+deprecated: false
+---
+
+If `PATH` is empty, `.` is returned.
+
+**Note:** On Windows, `PATH` is converted to slash (`/`) separators.
+
+```go-html-template
+{{ path.BaseName "a/news.html" }} → "news"
+{{ path.BaseName "news.html" }} → "news"
+{{ path.BaseName "a/b/c" }} → "c"
+{{ path.BaseName "/x/y/z/" }} → "z"
+```
index 03ffbd83648d876018124aac8e48ea9c507be3c6..852de65fd5dd2c9de42b139b61ce0c3018337d2b 100644 (file)
@@ -8,8 +8,9 @@ categories: [functions]
 menu:
   docs:
     parent: "functions"
-keywords: [path]
+keywords: [path, clean]
 signature: ["path.Clean PATH"]
+relatedfuncs: [path.Base, path.BaseName, path.Dir, path.Ext, path.Join, path.Split]
 ---
 
 `path.Clean` replaces path separators with slashes (`/`) and removes extraneous separators, including trailing separators.
index 851a3dc837580237056c4c15c36d57f8a0388738..161a0daa825b015c9d1c141dfd4db7e24b677346 100644 (file)
@@ -12,7 +12,7 @@ keywords: [path, dir]
 signature: ["path.Dir PATH"]
 workson: []
 hugoversion: "0.40"
-relatedfuncs: [path.Base, path.Ext, path.Split]
+relatedfuncs: [path.Base, path.BaseName, path.Clean, path.Ext, path.Join, path.Split]
 deprecated: false
 ---
 
index 73db7ab38862fda1ae997a9228203441e0b40afb..8c6fe907c3275f3c105066c3691bbfe0bc38e49b 100644 (file)
@@ -12,7 +12,7 @@ keywords: [path, ext, extension]
 signature: ["path.Ext PATH"]
 workson: []
 hugoversion: "0.40"
-relatedfuncs: [path.Base, path.Dir, path.Split]
+relatedfuncs: [path.Base, path.BaseName, path.Clean, path.Dir, path.Join, path.Split]
 deprecated: false
 ---
 
index 96ce863226486cab434d464d0c5e7abd6e4f801d..579e8667e049ec604d6719eac8830b23459fbfff 100644 (file)
@@ -12,7 +12,7 @@ keywords: [path, join]
 signature: ["path.Join ELEMENT..."]
 workson: []
 hugoversion: "0.39"
-relatedfuncs: [path.Split]
+relatedfuncs: [path.Base, path.BaseName, path.Clean, path.Dir, path.Ext, path.Split]
 deprecated: false
 ---
 
index 9a09fbe4ab1e6fe6fd2d56be6ddabe9925f06421..7737b77d39762b0ae1ff9e202f5991b67340a2ea 100644 (file)
@@ -12,7 +12,7 @@ keywords: [path, split]
 signature: ["path.Split PATH"]
 workson: []
 hugoversion: "0.39"
-relatedfuncs: [path.Split]
+relatedfuncs: [path.Base, path.BaseName, path.Clean, path.Dir, path.Ext, path.Join]
 deprecated: false
 ---
 
index d334dd90660a33c87202bf0231781512d703a6eb..378b97e0335a3e5793369f49d3cc4e2542d940cd 100644 (file)
@@ -18,6 +18,7 @@ import (
        "fmt"
        _path "path"
        "path/filepath"
+       "strings"
 
        "github.com/gohugoio/hugo/deps"
        "github.com/spf13/cast"
@@ -94,6 +95,21 @@ func (ns *Namespace) Base(path any) (string, error) {
        return _path.Base(spath), nil
 }
 
+// BaseName returns the last element of path, removing the extension if present.
+// 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) BaseName(path any) (string, error) {
+       spath, err := cast.ToStringE(path)
+       if err != nil {
+               return "", err
+       }
+       spath = filepath.ToSlash(spath)
+       return strings.TrimSuffix(_path.Base(spath), _path.Ext(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
index c9f8469e74e9ee122b4de05b587334334497df1f..599d8367a9747cca7eecbe029a9bc66d6649b25f 100644 (file)
@@ -56,6 +56,36 @@ func TestBase(t *testing.T) {
        }
 }
 
+func TestBaseName(t *testing.T) {
+       t.Parallel()
+       c := qt.New(t)
+
+       for _, test := range []struct {
+               path   any
+               expect any
+       }{
+               {filepath.FromSlash(`foo/bar.txt`), `bar`},
+               {filepath.FromSlash(`foo/bar/txt `), `txt `},
+               {filepath.FromSlash(`foo/bar.t`), `bar`},
+               {`foo.bar.txt`, `foo.bar`},
+               {`.x`, ``},
+               {``, `.`},
+               // errors
+               {tstNoStringer{}, false},
+       } {
+
+               result, err := ns.BaseName(test.path)
+
+               if b, ok := test.expect.(bool); ok && !b {
+                       c.Assert(err, qt.Not(qt.IsNil))
+                       continue
+               }
+
+               c.Assert(err, qt.IsNil)
+               c.Assert(result, qt.Equals, test.expect)
+       }
+}
+
 func TestDir(t *testing.T) {
        t.Parallel()
        c := qt.New(t)