diff --git a/common/paths/pathparser.go b/common/paths/pathparser.go index 3fe64612205..ee870434915 100644 --- a/common/paths/pathparser.go +++ b/common/paths/pathparser.go @@ -112,10 +112,11 @@ type Path interface { identity.Identity Component() string Name() string + NameNoExt() string + NameNoIdentifier() string Base() string Dir() string Ext() string - Slice(bottom, top int) string Identifiers() []string Identifier(i int) string IsContent() bool @@ -195,65 +196,28 @@ func (p *pathBase) Name() string { return p.s } -func (p *pathBase) Dir() string { - if p.posBase > 0 { - return p.s[:p.posBase-1] +// Name returns the last element of path withhout any extension. +func (p *pathBase) NameNoExt() string { + if i := p.identifierIndex(0); i != -1 { + return p.s[p.posBase : p.identifiers[i].Low-1] } - return "/" + return p.s[p.posBase:] } -func (p *pathBase) Slice(bottom, top int) string { - if bottom == 0 && top == 0 { - return p.s - } - - if bottom < 0 { - bottom = 0 - } - - if top < 0 { - top = 0 - } - - if bottom > len(p.identifiers)+1 { - bottom = len(p.identifiers) + 1 - } - - if top > len(p.identifiers)+1 { - top = len(p.identifiers) + 1 - } - - // 0 : posBase - // posBase : identifier[0].Low - // identifier[n].Low : identifier[n].High - var low, high int - if bottom == 1 { - low = p.posBase - } else if bottom > 1 { - low = p.identifiers[len(p.identifiers)-bottom+1].Low +func (p *pathBase) NameNoIdentifier() string { + if len(p.identifiers) > 0 { + return p.s[p.posBase : p.identifiers[len(p.identifiers)-1].Low-1] } - - if top == 0 { - high = len(p.s) - } else if top > 0 { - i := top - distance := len(p.identifiers) - i - if distance <= 0 { - if distance == 0 { - high = p.identifiers[len(p.identifiers)-1].Low - 1 - } else { - high = p.posBase - 1 - } - } else { - high = p.identifiers[i].High - } + if i := p.identifierIndex(0); i != -1 { } + return p.s[p.posBase:] +} - if low > high { - return "" +func (p *pathBase) Dir() string { + if p.posBase > 0 { + return p.s[:p.posBase-1] } - - return p.s[low:high] + return "/" } // For content files, Base returns the path without any identifiers (extension, language code etc.). @@ -320,9 +284,18 @@ func (p *pathBase) IsLeafBundle() bool { } func (p *pathBase) identifierAsString(i int) string { - if i < 0 || i >= len(p.identifiers) { + i = p.identifierIndex(i) + if i == -1 { return "" } + id := p.identifiers[i] return p.s[id.Low:id.High] } + +func (p *pathBase) identifierIndex(i int) int { + if i < 0 || i >= len(p.identifiers) { + return -1 + } + return i +} diff --git a/common/paths/pathparser_test.go b/common/paths/pathparser_test.go index 1851c579152..a9af3e00d8d 100644 --- a/common/paths/pathparser_test.go +++ b/common/paths/pathparser_test.go @@ -45,6 +45,8 @@ func TestParse(t *testing.T) { "/A/B.txt", func(c *qt.C, p Path) { c.Assert(p.Name(), qt.Equals, "b.txt") + c.Assert(p.NameNoExt(), qt.Equals, "b") + c.Assert(p.NameNoIdentifier(), qt.Equals, "b") c.Assert(p.Base(), qt.Equals, "/a/b.txt") c.Assert(p.Ext(), qt.Equals, "txt") }, @@ -65,6 +67,7 @@ func TestParse(t *testing.T) { "/a/b", func(c *qt.C, p Path) { c.Assert(p.Name(), qt.Equals, "b") + c.Assert(p.NameNoExt(), qt.Equals, "b") c.Assert(p.Base(), qt.Equals, "/a/b") c.Assert(p.Ext(), qt.Equals, "") }, @@ -83,6 +86,7 @@ func TestParse(t *testing.T) { "/a/b.a.b.c.txt", func(c *qt.C, p Path) { c.Assert(p.Name(), qt.Equals, "b.a.b.c.txt") + c.Assert(p.NameNoIdentifier(), qt.Equals, "b") c.Assert(p.Identifiers(), qt.DeepEquals, []string{"txt", "c", "b", "a"}) c.Assert(p.Base(), qt.Equals, "/a/b.txt") c.Assert(p.Ext(), qt.Equals, "txt") @@ -95,6 +99,8 @@ func TestParse(t *testing.T) { c.Assert(p.Base(), qt.Equals, "/a/b") c.Assert(p.Dir(), qt.Equals, "/a/b") c.Assert(p.Ext(), qt.Equals, "md") + c.Assert(p.NameNoExt(), qt.Equals, "index.no") + c.Assert(p.NameNoIdentifier(), qt.Equals, "index") c.Assert(p.Identifiers(), qt.DeepEquals, []string{"md", "no"}) c.Assert(p.IsLeafBundle(), qt.IsTrue) c.Assert(p.IsBundle(), qt.IsTrue) @@ -123,46 +129,7 @@ func TestParse(t *testing.T) { c.Assert(p.Identifiers(), qt.DeepEquals, []string{"txt", "no"}) }, }, - { - "Slice", - "/a/b/index.txt", - func(c *qt.C, p Path) { - c.Assert(p.Slice(0, 0), qt.Equals, "/a/b/index.txt") - c.Assert(p.Slice(1, 0), qt.Equals, "index.txt") - c.Assert(p.Slice(2, 0), qt.Equals, "txt") - c.Assert(p.Slice(3, 0), qt.Equals, "txt") - c.Assert(p.Slice(4, 0), qt.Equals, "txt") - - c.Assert(p.Slice(0, 1), qt.Equals, "/a/b/index") - c.Assert(p.Slice(0, 2), qt.Equals, "/a/b") - c.Assert(p.Slice(0, 3), qt.Equals, "/a/b") - c.Assert(p.Slice(0, 4), qt.Equals, "/a/b") - - c.Assert(p.Slice(1, 1), qt.Equals, "index") - c.Assert(p.Slice(1, 2), qt.Equals, "") - c.Assert(p.Slice(1, 3), qt.Equals, "") - }, - }, - { - "Slice, language", - "/a/b/index.no.txt", - func(c *qt.C, p Path) { - c.Assert(p.Slice(0, 0), qt.Equals, "/a/b/index.no.txt") - c.Assert(p.Slice(1, 0), qt.Equals, "index.no.txt") - c.Assert(p.Slice(2, 0), qt.Equals, "no.txt") - c.Assert(p.Slice(3, 0), qt.Equals, "txt") - c.Assert(p.Slice(4, 0), qt.Equals, "txt") - c.Assert(p.Slice(0, 1), qt.Equals, "/a/b/index.no") - c.Assert(p.Slice(0, 2), qt.Equals, "/a/b/index") - c.Assert(p.Slice(0, 3), qt.Equals, "/a/b") - c.Assert(p.Slice(0, 4), qt.Equals, "/a/b") - - c.Assert(p.Slice(1, 1), qt.Equals, "index.no") - c.Assert(p.Slice(1, 2), qt.Equals, "index") - c.Assert(p.Slice(1, 3), qt.Equals, "") - }, - }, { "Empty", "", diff --git a/source/fileInfo.go b/source/fileInfo.go index 093b370bfaf..1a824ae9061 100644 --- a/source/fileInfo.go +++ b/source/fileInfo.go @@ -19,7 +19,6 @@ import ( "sync" "github.com/gohugoio/hugo/common/paths" - "github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/hugofs/files" @@ -168,9 +167,7 @@ func (fi *FileInfo) LogicalName() string { // BaseFileName returns a file's name without extension (ie. "page.sv"). func (fi *FileInfo) BaseFileName() string { - htesting.Println("===>l", fi.p().Base()) - - return fi.baseName + return fi.p().NameNoExt() } // TranslationBaseName returns a file's translation base name without the