diff options
Diffstat (limited to 'hugofs')
-rw-r--r-- | hugofs/glob/glob.go | 33 | ||||
-rw-r--r-- | hugofs/glob/glob_test.go | 26 |
2 files changed, 44 insertions, 15 deletions
diff --git a/hugofs/glob/glob.go b/hugofs/glob/glob.go index 18d8d44cb..124a3d50e 100644 --- a/hugofs/glob/glob.go +++ b/hugofs/glob/glob.go @@ -51,7 +51,7 @@ func GetGlob(pattern string) (glob.Glob, error) { } func NormalizePath(p string) string { - return strings.Trim(filepath.ToSlash(strings.ToLower(p)), "/.") + return strings.Trim(path.Clean(filepath.ToSlash(strings.ToLower(p))), "/.") } // ResolveRootDir takes a normalized path on the form "assets/**.json" and @@ -60,14 +60,7 @@ func ResolveRootDir(p string) string { parts := strings.Split(path.Dir(p), "/") var roots []string for _, part := range parts { - isSpecial := false - for i := 0; i < len(part); i++ { - if syntax.Special(part[i]) { - isSpecial = true - break - } - } - if isSpecial { + if HasGlobChar(part) { break } roots = append(roots, part) @@ -79,3 +72,25 @@ func ResolveRootDir(p string) string { return strings.Join(roots, "/") } + +// FilterGlobParts removes any string with glob wildcard. +func FilterGlobParts(a []string) []string { + b := a[:0] + for _, x := range a { + if !HasGlobChar(x) { + b = append(b, x) + } + } + return b +} + +// HasGlobChar returns whether s contains any glob wildcards. +func HasGlobChar(s string) bool { + for i := 0; i < len(s); i++ { + if syntax.Special(s[i]) { + return true + } + } + return false + +} diff --git a/hugofs/glob/glob_test.go b/hugofs/glob/glob_test.go index 2b1c74107..cca8e4e0f 100644 --- a/hugofs/glob/glob_test.go +++ b/hugofs/glob/glob_test.go @@ -24,8 +24,8 @@ func TestResolveRootDir(t *testing.T) { c := qt.New(t) for _, test := range []struct { - in string - expect string + input string + expected string }{ {"data/foo.json", "data"}, {"a/b/**/foo.json", "a/b"}, @@ -33,7 +33,21 @@ func TestResolveRootDir(t *testing.T) { {"a/b[a-c]/foo.json", "a"}, } { - c.Assert(ResolveRootDir(test.in), qt.Equals, test.expect) + c.Assert(ResolveRootDir(test.input), qt.Equals, test.expected) + } +} + +func TestFilterGlobParts(t *testing.T) { + c := qt.New(t) + + for _, test := range []struct { + input []string + expected []string + }{ + {[]string{"a", "*", "c"}, []string{"a", "c"}}, + } { + + c.Assert(FilterGlobParts(test.input), qt.DeepEquals, test.expected) } } @@ -41,8 +55,8 @@ func TestNormalizePath(t *testing.T) { c := qt.New(t) for _, test := range []struct { - in string - expect string + input string + expected string }{ {filepath.FromSlash("data/FOO.json"), "data/foo.json"}, {filepath.FromSlash("/data/FOO.json"), "data/foo.json"}, @@ -50,7 +64,7 @@ func TestNormalizePath(t *testing.T) { {"//", ""}, } { - c.Assert(NormalizePath(test.in), qt.Equals, test.expect) + c.Assert(NormalizePath(test.input), qt.Equals, test.expected) } } |