mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-30 22:39:58 +02:00
@@ -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
|
||||
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user