🐛 Fixed resource paths support

This commit is contained in:
Maxim Lebedev 2024-02-03 20:48:34 +06:00
parent e18e265d1a
commit c7ecb8dbaf
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
2 changed files with 45 additions and 26 deletions

View File

@ -41,11 +41,15 @@ func NewPath(path string) Path {
parts := strings.Split(out.baseFileName, ".")
out.Language = NewLanguage(parts[len(parts)-1])
out.translationBaseName = strings.Join(parts[:len(parts)-1], ".")
out.contentBaseName = out.translationBaseName
if len(parts) == 1 {
out.translationBaseName = parts[0]
out.contentBaseName = filepath.Base(out.dir)
} else {
out.contentBaseName = out.translationBaseName
}
switch out.translationBaseName {
default:
out.contentBaseName = out.translationBaseName
case "_index", "index":
out.contentBaseName = filepath.Base(out.dir)
}
@ -62,6 +66,7 @@ func NewPath(path string) Path {
// /news/a.en.md => a.en
// /news/b/index.en.md => index.en
// /news/_index.en.md => _index.en
// /news/b/photo.jpg => photo
func (p Path) BaseFileName() string {
return p.baseFileName
}
@ -71,6 +76,7 @@ func (p Path) BaseFileName() string {
// /news/a.en.md => a
// /news/b/index.en.md => b
// /news/_index.en.md => news
// /news/b/photo.jpg => b
func (p Path) ContentBaseName() string {
return p.contentBaseName
}
@ -80,6 +86,7 @@ func (p Path) ContentBaseName() string {
// /news/a.en.md => news/
// /news/b/index.en.md => news/b/
// /news/_index.en.md => news/
// /news/b/photo.jpg => news/b/
func (p Path) Dir() string {
return p.dir
}
@ -87,6 +94,7 @@ func (p Path) Dir() string {
// Ext returns file extention:
//
// /news/b/index.en.md => md
// /news/b/photo.jpg => jpg
func (p Path) Ext() string {
return p.ext
}
@ -95,11 +103,12 @@ func (p Path) Filename() string {
return p.filename
}
// LogicalName returns fille file name in directory:
// LogicalName returns file name in directory:
//
// /news/a.en.md => a.en.md
// /news/b/index.en.md => index.en.md
// /news/_index.en.md => _index.en.md
// /news/b/photo.jpg => photo.jpg
func (p Path) LogicalName() string {
return p.logicalName
}
@ -113,6 +122,7 @@ func (p Path) Path() string {
// /news/a.en.md => a
// /news/b/index.en.md => index
// /news/_index.en.md => _index
// /news/b/photo.jpg => photo
func (p Path) TranslationBaseName() string {
return p.translationBaseName
}

View File

@ -11,6 +11,7 @@ var (
testRegularPath string = filepath.Join("news", "a.en.md")
testLeafPath string = filepath.Join("news", "b", "index.en.md")
testBranchPath string = filepath.Join("news", "_index.en.md")
testResource string = filepath.Join("news", "b", "photo.jpg")
)
func TestPath_BaseFileName(t *testing.T) {
@ -19,9 +20,10 @@ func TestPath_BaseFileName(t *testing.T) {
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "a.en"},
"leaf": {testLeafPath, "index.en"},
"branch": {testBranchPath, "_index.en"},
"regular": {testRegularPath, "a.en"},
"leaf": {testLeafPath, "index.en"},
"branch": {testBranchPath, "_index.en"},
"resource": {testResource, "photo"},
} {
name, tc := name, tc
@ -41,9 +43,10 @@ func TestPath_ContentBaseName(t *testing.T) {
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "a"},
"leaf": {testLeafPath, "b"},
"branch": {testBranchPath, "news"},
"regular": {testRegularPath, "a"},
"leaf": {testLeafPath, "b"},
"branch": {testBranchPath, "news"},
"resource": {testResource, "b"},
} {
name, tc := name, tc
@ -63,9 +66,10 @@ func TestPath_Dir(t *testing.T) {
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "news/"},
"leaf": {testLeafPath, "news/b/"},
"branch": {testBranchPath, "news/"},
"regular": {testRegularPath, "news/"},
"leaf": {testLeafPath, "news/b/"},
"branch": {testBranchPath, "news/"},
"resource": {testResource, "news/b/"},
} {
name, tc := name, tc
@ -84,18 +88,21 @@ func TestPath_Ext(t *testing.T) {
const expect string = "md"
for name, input := range map[string]string{
"regular": testRegularPath,
"leaf": testLeafPath,
"branch": testBranchPath,
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "md"},
"leaf": {testLeafPath, "md"},
"branch": {testBranchPath, "md"},
"resource": {testResource, "jpg"},
} {
name, input := name, input
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewPath(input).Ext(); actual != expect {
t.Errorf("Ext() = '%s', want '%s'", actual, expect)
if actual := domain.NewPath(tc.input).Ext(); actual != tc.expect {
t.Errorf("Ext() = '%s', want '%s'", actual, tc.expect)
}
})
}
@ -129,9 +136,10 @@ func TestPath_LogicalName(t *testing.T) {
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "a.en.md"},
"leaf": {testLeafPath, "index.en.md"},
"branch": {testBranchPath, "_index.en.md"},
"regular": {testRegularPath, "a.en.md"},
"leaf": {testLeafPath, "index.en.md"},
"branch": {testBranchPath, "_index.en.md"},
"resource": {testResource, "photo.jpg"},
} {
name, tc := name, tc
@ -151,9 +159,10 @@ func TestPath_TranslationBaseName(t *testing.T) {
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "a"},
"leaf": {testLeafPath, "index"},
"branch": {testBranchPath, "_index"},
"regular": {testRegularPath, "a"},
"leaf": {testLeafPath, "index"},
"branch": {testBranchPath, "_index"},
"resource": {testResource, "photo"},
} {
name, tc := name, tc