mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-17 21:01:26 +02:00
@@ -237,12 +237,17 @@ func prettifyPath(in string, b filepathPathBridge) string {
|
||||
return b.Join(b.Dir(in), name, "index"+ext)
|
||||
}
|
||||
|
||||
// CommonDir returns the common directory of the given paths.
|
||||
func CommonDir(path1, path2 string) string {
|
||||
// CommonDirPath returns the common directory of the given paths.
|
||||
func CommonDirPath(path1, path2 string) string {
|
||||
if path1 == "" || path2 == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
hadLeadingSlash := strings.HasPrefix(path1, "/") || strings.HasPrefix(path2, "/")
|
||||
|
||||
path1 = TrimLeading(path1)
|
||||
path2 = TrimLeading(path2)
|
||||
|
||||
p1 := strings.Split(path1, "/")
|
||||
p2 := strings.Split(path2, "/")
|
||||
|
||||
@@ -256,7 +261,13 @@ func CommonDir(path1, path2 string) string {
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(common, "/")
|
||||
s := strings.Join(common, "/")
|
||||
|
||||
if hadLeadingSlash && s != "" {
|
||||
s = "/" + s
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Sanitize sanitizes string to be used in Hugo's file paths and URLs, allowing only
|
||||
@@ -384,12 +395,27 @@ func PathEscape(pth string) string {
|
||||
|
||||
// ToSlashTrimLeading is just a filepath.ToSlash with an added / prefix trimmer.
|
||||
func ToSlashTrimLeading(s string) string {
|
||||
return strings.TrimPrefix(filepath.ToSlash(s), "/")
|
||||
return TrimLeading(filepath.ToSlash(s))
|
||||
}
|
||||
|
||||
// TrimLeading trims the leading slash from the given string.
|
||||
func TrimLeading(s string) string {
|
||||
return strings.TrimPrefix(s, "/")
|
||||
}
|
||||
|
||||
// ToSlashTrimTrailing is just a filepath.ToSlash with an added / suffix trimmer.
|
||||
func ToSlashTrimTrailing(s string) string {
|
||||
return strings.TrimSuffix(filepath.ToSlash(s), "/")
|
||||
return TrimTrailing(filepath.ToSlash(s))
|
||||
}
|
||||
|
||||
// TrimTrailing trims the trailing slash from the given string.
|
||||
func TrimTrailing(s string) string {
|
||||
return strings.TrimSuffix(s, "/")
|
||||
}
|
||||
|
||||
// ToSlashTrim trims any leading and trailing slashes from the given string and converts it to a forward slash separated path.
|
||||
func ToSlashTrim(s string) string {
|
||||
return strings.Trim(filepath.ToSlash(s), "/")
|
||||
}
|
||||
|
||||
// ToSlashPreserveLeading converts the path given to a forward slash separated path
|
||||
@@ -397,3 +423,8 @@ func ToSlashTrimTrailing(s string) string {
|
||||
func ToSlashPreserveLeading(s string) string {
|
||||
return "/" + strings.Trim(filepath.ToSlash(s), "/")
|
||||
}
|
||||
|
||||
// IsSameFilePath checks if s1 and s2 are the same file path.
|
||||
func IsSameFilePath(s1, s2 string) bool {
|
||||
return path.Clean(ToSlashTrim(s1)) == path.Clean(ToSlashTrim(s2))
|
||||
}
|
||||
|
@@ -262,3 +262,52 @@ func TestFieldsSlash(t *testing.T) {
|
||||
c.Assert(FieldsSlash("/"), qt.DeepEquals, []string{})
|
||||
c.Assert(FieldsSlash(""), qt.DeepEquals, []string{})
|
||||
}
|
||||
|
||||
func TestCommonDirPath(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
for _, this := range []struct {
|
||||
a, b, expected string
|
||||
}{
|
||||
{"/a/b/c", "/a/b/d", "/a/b"},
|
||||
{"/a/b/c", "a/b/d", "/a/b"},
|
||||
{"a/b/c", "/a/b/d", "/a/b"},
|
||||
{"a/b/c", "a/b/d", "a/b"},
|
||||
{"/a/b/c", "/a/b/c", "/a/b/c"},
|
||||
{"/a/b/c", "/a/b/c/d", "/a/b/c"},
|
||||
{"/a/b/c", "/a/b", "/a/b"},
|
||||
{"/a/b/c", "/a", "/a"},
|
||||
{"/a/b/c", "/d/e/f", ""},
|
||||
} {
|
||||
c.Assert(CommonDirPath(this.a, this.b), qt.Equals, this.expected, qt.Commentf("a: %s b: %s", this.a, this.b))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsSameFilePath(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
for _, this := range []struct {
|
||||
a, b string
|
||||
expected bool
|
||||
}{
|
||||
{"/a/b/c", "/a/b/c", true},
|
||||
{"/a/b/c", "/a/b/c/", true},
|
||||
{"/a/b/c", "/a/b/d", false},
|
||||
{"/a/b/c", "/a/b", false},
|
||||
{"/a/b/c", "/a/b/c/d", false},
|
||||
{"/a/b/c", "/a/b/cd", false},
|
||||
{"/a/b/c", "/a/b/cc", false},
|
||||
{"/a/b/c", "/a/b/c/", true},
|
||||
{"/a/b/c", "/a/b/c//", true},
|
||||
{"/a/b/c", "/a/b/c/.", true},
|
||||
{"/a/b/c", "/a/b/c/./", true},
|
||||
{"/a/b/c", "/a/b/c/./.", true},
|
||||
{"/a/b/c", "/a/b/c/././", true},
|
||||
{"/a/b/c", "/a/b/c/././.", true},
|
||||
{"/a/b/c", "/a/b/c/./././", true},
|
||||
{"/a/b/c", "/a/b/c/./././.", true},
|
||||
{"/a/b/c", "/a/b/c/././././", true},
|
||||
} {
|
||||
c.Assert(IsSameFilePath(filepath.FromSlash(this.a), filepath.FromSlash(this.b)), qt.Equals, this.expected, qt.Commentf("a: %s b: %s", this.a, this.b))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user