Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab2de51

Browse files
committed
common/paths: Remove unused code
Identified with: ``` punused "common/paths/**.go" ````
1 parent 72b85d5 commit ab2de51

6 files changed

Lines changed: 15 additions & 348 deletions

File tree

common/paths/path.go

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,6 @@ func AddLeadingAndTrailingSlash(path string) string {
100100
return AddTrailingSlash(AddLeadingSlash(path))
101101
}
102102

103-
// MakeTitle converts the path given to a suitable title, trimming whitespace
104-
// and replacing hyphens with whitespace.
105-
func MakeTitle(inpath string) string {
106-
return strings.Replace(strings.TrimSpace(inpath), "-", " ", -1)
107-
}
108-
109-
// ReplaceExtension takes a path and an extension, strips the old extension
110-
// and returns the path with the new extension.
111-
func ReplaceExtension(path string, newExt string) string {
112-
f, _ := fileAndExt(path, fpb)
113-
return f + "." + newExt
114-
}
115-
116103
func makePathRelative(inPath string, possibleDirectories ...string) (string, error) {
117104
for _, currentPath := range possibleDirectories {
118105
if after, ok := strings.CutPrefix(inPath, currentPath); ok {
@@ -201,25 +188,6 @@ func extractFilename(in, ext, base, pathSeparator string) (name string) {
201188
return
202189
}
203190

204-
// GetRelativePath returns the relative path of a given path.
205-
func GetRelativePath(path, base string) (final string, err error) {
206-
if filepath.IsAbs(path) && base == "" {
207-
return "", errors.New("source: missing base directory")
208-
}
209-
name := filepath.Clean(path)
210-
base = filepath.Clean(base)
211-
212-
name, err = filepath.Rel(base, name)
213-
if err != nil {
214-
return "", err
215-
}
216-
217-
if strings.HasSuffix(filepath.FromSlash(path), FilePathSeparator) && !strings.HasSuffix(name, FilePathSeparator) {
218-
name += FilePathSeparator
219-
}
220-
return name, nil
221-
}
222-
223191
func prettifyPath(in string, b filepathPathBridge) string {
224192
if filepath.Ext(in) == "" {
225193
// /section/name/ -> /section/name/index.html
@@ -237,39 +205,6 @@ func prettifyPath(in string, b filepathPathBridge) string {
237205
return b.Join(b.Dir(in), name, "index"+ext)
238206
}
239207

240-
// CommonDirPath returns the common directory of the given paths.
241-
func CommonDirPath(path1, path2 string) string {
242-
if path1 == "" || path2 == "" {
243-
return ""
244-
}
245-
246-
hadLeadingSlash := strings.HasPrefix(path1, "/") || strings.HasPrefix(path2, "/")
247-
248-
path1 = TrimLeading(path1)
249-
path2 = TrimLeading(path2)
250-
251-
p1 := strings.Split(path1, "/")
252-
p2 := strings.Split(path2, "/")
253-
254-
var common []string
255-
256-
for i := 0; i < len(p1) && i < len(p2); i++ {
257-
if p1[i] == p2[i] {
258-
common = append(common, p1[i])
259-
} else {
260-
break
261-
}
262-
}
263-
264-
s := strings.Join(common, "/")
265-
266-
if hadLeadingSlash && s != "" {
267-
s = "/" + s
268-
}
269-
270-
return s
271-
}
272-
273208
// Sanitize sanitizes string to be used in Hugo's file paths and URLs, allowing only
274209
// a predefined set of special Unicode characters.
275210
//

common/paths/path_test.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,6 @@ import (
2020
qt "github.com/frankban/quicktest"
2121
)
2222

23-
func TestGetRelativePath(t *testing.T) {
24-
tests := []struct {
25-
path string
26-
base string
27-
expect any
28-
}{
29-
{filepath.FromSlash("/a/b"), filepath.FromSlash("/a"), filepath.FromSlash("b")},
30-
{filepath.FromSlash("/a/b/c/"), filepath.FromSlash("/a"), filepath.FromSlash("b/c/")},
31-
{filepath.FromSlash("/c"), filepath.FromSlash("/a/b"), filepath.FromSlash("../../c")},
32-
{filepath.FromSlash("/c"), "", false},
33-
}
34-
for i, this := range tests {
35-
// ultimately a fancy wrapper around filepath.Rel
36-
result, err := GetRelativePath(this.path, this.base)
37-
38-
if b, ok := this.expect.(bool); ok && !b {
39-
if err == nil {
40-
t.Errorf("[%d] GetRelativePath didn't return an expected error", i)
41-
}
42-
} else {
43-
if err != nil {
44-
t.Errorf("[%d] GetRelativePath failed: %s", i, err)
45-
continue
46-
}
47-
if result != this.expect {
48-
t.Errorf("[%d] GetRelativePath got %v but expected %v", i, result, this.expect)
49-
}
50-
}
51-
52-
}
53-
}
54-
5523
func TestMakePathRelative(t *testing.T) {
5624
type test struct {
5725
inPath, path1, path2, output string
@@ -75,54 +43,6 @@ func TestMakePathRelative(t *testing.T) {
7543
}
7644
}
7745

78-
func TestMakeTitle(t *testing.T) {
79-
type test struct {
80-
input, expected string
81-
}
82-
data := []test{
83-
{"Make-Title", "Make Title"},
84-
{"MakeTitle", "MakeTitle"},
85-
{"make_title", "make_title"},
86-
}
87-
for i, d := range data {
88-
output := MakeTitle(d.input)
89-
if d.expected != output {
90-
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
91-
}
92-
}
93-
}
94-
95-
// Replace Extension is probably poorly named, but the intent of the
96-
// function is to accept a path and return only the file name with a
97-
// new extension. It's intentionally designed to strip out the path
98-
// and only provide the name. We should probably rename the function to
99-
// be more explicit at some point.
100-
func TestReplaceExtension(t *testing.T) {
101-
type test struct {
102-
input, newext, expected string
103-
}
104-
data := []test{
105-
// These work according to the above definition
106-
{"/some/random/path/file.xml", "html", "file.html"},
107-
{"/banana.html", "xml", "banana.xml"},
108-
{"./banana.html", "xml", "banana.xml"},
109-
{"banana/pie/index.html", "xml", "index.xml"},
110-
{"../pies/fish/index.html", "xml", "index.xml"},
111-
// but these all fail
112-
{"filename-without-an-ext", "ext", "filename-without-an-ext.ext"},
113-
{"/filename-without-an-ext", "ext", "filename-without-an-ext.ext"},
114-
{"/directory/mydir/", "ext", ".ext"},
115-
{"mydir/", "ext", ".ext"},
116-
}
117-
118-
for i, d := range data {
119-
output := ReplaceExtension(filepath.FromSlash(d.input), d.newext)
120-
if d.expected != output {
121-
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
122-
}
123-
}
124-
}
125-
12646
func TestExtNoDelimiter(t *testing.T) {
12747
c := qt.New(t)
12848
c.Assert(ExtNoDelimiter(filepath.FromSlash("/my/data.json")), qt.Equals, "json")
@@ -263,26 +183,6 @@ func TestFieldsSlash(t *testing.T) {
263183
c.Assert(FieldsSlash(""), qt.DeepEquals, []string{})
264184
}
265185

266-
func TestCommonDirPath(t *testing.T) {
267-
c := qt.New(t)
268-
269-
for _, this := range []struct {
270-
a, b, expected string
271-
}{
272-
{"/a/b/c", "/a/b/d", "/a/b"},
273-
{"/a/b/c", "a/b/d", "/a/b"},
274-
{"a/b/c", "/a/b/d", "/a/b"},
275-
{"a/b/c", "a/b/d", "a/b"},
276-
{"/a/b/c", "/a/b/c", "/a/b/c"},
277-
{"/a/b/c", "/a/b/c/d", "/a/b/c"},
278-
{"/a/b/c", "/a/b", "/a/b"},
279-
{"/a/b/c", "/a", "/a"},
280-
{"/a/b/c", "/d/e/f", ""},
281-
} {
282-
c.Assert(CommonDirPath(this.a, this.b), qt.Equals, this.expected, qt.Commentf("a: %s b: %s", this.a, this.b))
283-
}
284-
}
285-
286186
func TestIsSameFilePath(t *testing.T) {
287187
c := qt.New(t)
288188

common/paths/pathparser.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/gohugoio/hugo/common/types"
2626
"github.com/gohugoio/hugo/hugofs/files"
2727
"github.com/gohugoio/hugo/hugolib/sitesmatrix"
28-
"github.com/gohugoio/hugo/identity"
2928
"github.com/gohugoio/hugo/resources/kinds"
3029
)
3130

@@ -269,13 +268,6 @@ func (pp *PathParser) SitesMatrixFromPath(p *Path) sitesmatrix.VectorStore {
269268
return v
270269
}
271270

272-
// ParseIdentity parses component c with path s into a StringIdentity.
273-
func (pp *PathParser) ParseIdentity(c, s string) identity.StringIdentity {
274-
p := pp.parsePooled(c, s)
275-
defer putPath(p)
276-
return identity.StringIdentity(p.IdentifierBase())
277-
}
278-
279271
// ParseBaseAndBaseNameNoIdentifier parses component c with path s into a base and a base name without any identifier.
280272
func (pp *PathParser) ParseBaseAndBaseNameNoIdentifier(c, s string) (string, string) {
281273
p := pp.parsePooled(c, s)
@@ -795,27 +787,6 @@ func (p *Path) NameNoExt() string {
795787
return p.s[p.posContainerHigh:]
796788
}
797789

798-
// Name returns the last element of path without any language identifier.
799-
func (p *Path) NameNoLang() string {
800-
if len(p.posIdentifierPrefixLanguages) > 0 {
801-
// Prefix language: the wrapper is outside the name boundary, so Name() without
802-
// identifiers is the same as stripping all wrappers. Use the name + extension.
803-
if len(p.identifiersKnown) > 0 {
804-
return p.NameNoIdentifier() + p.s[p.identifiersKnown[0].Low-1:p.identifiersKnown[0].High]
805-
}
806-
return p.Name()
807-
}
808-
i := p.identifierIndex(p.posIdentifierLanguage)
809-
if i == -1 {
810-
return p.Name()
811-
}
812-
id := p.identifiersKnown[i]
813-
if id.Low-1 < p.posContainerHigh {
814-
return p.s[id.High:]
815-
}
816-
return p.s[p.posContainerHigh:id.Low-1] + p.s[id.High:]
817-
}
818-
819790
// BaseNameNoIdentifier returns the logical base name for a resource without any identifier (e.g. no extension).
820791
// For bundles this will be the containing directory's name, e.g. "blog".
821792
func (p *Path) BaseNameNoIdentifier() string {
@@ -946,11 +917,6 @@ func (p *Path) BaseReTyped(typ string) (d string) {
946917
return
947918
}
948919

949-
// BaseNoLeadingSlash returns the base path without the leading slash.
950-
func (p *Path) BaseNoLeadingSlash() string {
951-
return p.Base()[1:]
952-
}
953-
954920
func (p *Path) base(preserveExt, isBundle bool) string {
955921
if len(p.identifiersKnown) == 0 {
956922
return p.norm(p.s)
@@ -1019,24 +985,10 @@ func (p *Path) Langs() []string {
1019985
return p.identifiersAsStrings(p.posIdentifierPrefixLanguages)
1020986
}
1021987

1022-
func (p *Path) Version() string {
1023-
if len(p.posIdentifierVersions) > 0 {
1024-
return p.identifierAsString(p.posIdentifierVersions[0])
1025-
}
1026-
return ""
1027-
}
1028-
1029988
func (p *Path) Versions() []string {
1030989
return p.identifiersAsStrings(p.posIdentifierVersions)
1031990
}
1032991

1033-
func (p *Path) Role() string {
1034-
if len(p.posIdentifierRoles) > 0 {
1035-
return p.identifierAsString(p.posIdentifierRoles[0])
1036-
}
1037-
return ""
1038-
}
1039-
1040992
func (p *Path) Roles() []string {
1041993
return p.identifiersAsStrings(p.posIdentifierRoles)
1042994
}
@@ -1056,10 +1008,6 @@ func (p *Path) Custom() string {
10561008
return strings.TrimSuffix(strings.TrimPrefix(p.identifierAsString(p.posIdentifierCustom), identifierCustomWrapper), identifierCustomWrapper)
10571009
}
10581010

1059-
func (p *Path) Identifier(i int) string {
1060-
return p.identifierAsString(i)
1061-
}
1062-
10631011
func (p *Path) Disabled() bool {
10641012
return p.disabled
10651013
}

0 commit comments

Comments
 (0)