🚚 Renamed File domain into Path
This commit is contained in:
parent
665af5ebfc
commit
733a3e4e8b
|
@ -1,93 +0,0 @@
|
|||
package domain
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
Language Language
|
||||
baseFileName string
|
||||
contentBaseName string
|
||||
dir string
|
||||
ext string
|
||||
filename string
|
||||
logicalName string
|
||||
path string
|
||||
translationBaseName string
|
||||
uniqueId string
|
||||
}
|
||||
|
||||
func NewFile(path string) File {
|
||||
out := File{
|
||||
Language: LanguageUnd,
|
||||
baseFileName: "",
|
||||
contentBaseName: "",
|
||||
dir: filepath.Dir(path) + string(filepath.Separator),
|
||||
ext: strings.TrimPrefix(filepath.Ext(path), "."),
|
||||
filename: path,
|
||||
logicalName: filepath.Base(path),
|
||||
path: path,
|
||||
translationBaseName: "",
|
||||
uniqueId: "",
|
||||
}
|
||||
out.baseFileName = strings.TrimSuffix(out.logicalName, filepath.Ext(out.logicalName))
|
||||
|
||||
parts := strings.Split(out.baseFileName, ".")
|
||||
out.Language = NewLanguage(parts[len(parts)-1])
|
||||
out.translationBaseName = strings.Join(parts[:len(parts)-1], ".")
|
||||
out.contentBaseName = out.translationBaseName
|
||||
|
||||
switch out.translationBaseName {
|
||||
default:
|
||||
out.contentBaseName = out.translationBaseName
|
||||
case "_index", "index":
|
||||
out.contentBaseName = filepath.Base(out.dir)
|
||||
}
|
||||
|
||||
hash := md5.New()
|
||||
_, _ = hash.Write([]byte(out.path))
|
||||
out.uniqueId = string(hash.Sum(nil))
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// BaseFileName returns file name without extention.
|
||||
func (f File) BaseFileName() string {
|
||||
return f.baseFileName
|
||||
}
|
||||
|
||||
func (f File) ContentBaseName() string {
|
||||
return f.contentBaseName
|
||||
}
|
||||
|
||||
// Dir returns directory path.
|
||||
func (f File) Dir() string {
|
||||
return f.dir
|
||||
}
|
||||
|
||||
// Ext returns file extention.
|
||||
func (f File) Ext() string {
|
||||
return f.ext
|
||||
}
|
||||
|
||||
func (f File) Filename() string {
|
||||
return f.filename
|
||||
}
|
||||
|
||||
func (f File) LogicalName() string {
|
||||
return f.logicalName
|
||||
}
|
||||
|
||||
func (f File) Path() string {
|
||||
return f.path
|
||||
}
|
||||
|
||||
func (f File) TranslationBaseName() string {
|
||||
return f.translationBaseName
|
||||
}
|
||||
|
||||
func (f File) UniqueID() string {
|
||||
return f.uniqueId
|
||||
}
|
|
@ -3,7 +3,7 @@ package domain
|
|||
type Page struct {
|
||||
Language Language
|
||||
Params map[string]any
|
||||
File File
|
||||
File Path
|
||||
Description string
|
||||
Title string
|
||||
Content []byte
|
||||
|
|
|
@ -0,0 +1,122 @@
|
|||
package domain
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Path struct {
|
||||
Language Language
|
||||
baseFileName string
|
||||
contentBaseName string
|
||||
dir string
|
||||
ext string
|
||||
filename string
|
||||
logicalName string
|
||||
path string
|
||||
translationBaseName string
|
||||
uniqueId string
|
||||
}
|
||||
|
||||
func NewPath(path string) Path {
|
||||
out := Path{
|
||||
Language: LanguageUnd,
|
||||
baseFileName: "",
|
||||
contentBaseName: "",
|
||||
dir: filepath.Dir(path) + "/",
|
||||
ext: strings.TrimPrefix(filepath.Ext(path), "."),
|
||||
filename: path,
|
||||
logicalName: filepath.Base(path),
|
||||
path: path,
|
||||
translationBaseName: "",
|
||||
uniqueId: "",
|
||||
}
|
||||
out.baseFileName = strings.TrimSuffix(out.logicalName, filepath.Ext(out.logicalName))
|
||||
|
||||
parts := strings.Split(out.baseFileName, ".")
|
||||
out.Language = NewLanguage(parts[len(parts)-1])
|
||||
out.translationBaseName = strings.Join(parts[:len(parts)-1], ".")
|
||||
out.contentBaseName = out.translationBaseName
|
||||
|
||||
switch out.translationBaseName {
|
||||
default:
|
||||
out.contentBaseName = out.translationBaseName
|
||||
case "_index", "index":
|
||||
out.contentBaseName = filepath.Base(out.dir)
|
||||
}
|
||||
|
||||
hash := md5.New()
|
||||
_, _ = hash.Write([]byte(out.path))
|
||||
out.uniqueId = string(hash.Sum(nil))
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// BaseFileName returns file name without extention:
|
||||
//
|
||||
// /news/a.en.md => a.en
|
||||
// /news/b/index.en.md => index.en
|
||||
// /news/_index.en.md => _index.en
|
||||
func (p Path) BaseFileName() string {
|
||||
return p.baseFileName
|
||||
}
|
||||
|
||||
// ContentBaseName returns file or folder name based of index location:
|
||||
//
|
||||
// /news/a.en.md => a
|
||||
// /news/b/index.en.md => b
|
||||
// /news/_index.en.md => news
|
||||
func (p Path) ContentBaseName() string {
|
||||
return p.contentBaseName
|
||||
}
|
||||
|
||||
// Dir returns directory path:
|
||||
//
|
||||
// /news/a.en.md => news/
|
||||
// /news/b/index.en.md => news/b/
|
||||
// /news/_index.en.md => news/
|
||||
func (p Path) Dir() string {
|
||||
return p.dir
|
||||
}
|
||||
|
||||
// Ext returns file extention:
|
||||
//
|
||||
// /news/b/index.en.md => md
|
||||
func (p Path) Ext() string {
|
||||
return p.ext
|
||||
}
|
||||
|
||||
func (p Path) Filename() string {
|
||||
return p.filename
|
||||
}
|
||||
|
||||
// LogicalName returns fille 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
|
||||
func (p Path) LogicalName() string {
|
||||
return p.logicalName
|
||||
}
|
||||
|
||||
func (p Path) Path() string {
|
||||
return p.path
|
||||
}
|
||||
|
||||
// TranslationBaseName returns file name without language code and extention:
|
||||
//
|
||||
// /news/a.en.md => a
|
||||
// /news/b/index.en.md => index
|
||||
// /news/_index.en.md => _index
|
||||
func (p Path) TranslationBaseName() string {
|
||||
return p.translationBaseName
|
||||
}
|
||||
|
||||
func (p Path) UniqueID() string {
|
||||
return p.uniqueId
|
||||
}
|
||||
|
||||
func (p Path) GoString() string {
|
||||
return "domain.Path(" + p.path + ")"
|
||||
}
|
|
@ -8,159 +8,159 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
testRegularFile string = filepath.Join("news", "a.en.md")
|
||||
testLeafFile string = filepath.Join("news", "b", "index.en.md")
|
||||
testBranchFile string = filepath.Join("news", "_index.en.md")
|
||||
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")
|
||||
)
|
||||
|
||||
func TestFile_BaseFileName(t *testing.T) {
|
||||
func TestPath_BaseFileName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for name, tc := range map[string]struct {
|
||||
input, expect string
|
||||
}{
|
||||
"regular": {testRegularFile, "a.en"},
|
||||
"leaf": {testLeafFile, "index.en"},
|
||||
"branch": {testBranchFile, "_index.en"},
|
||||
"regular": {testRegularPath, "a.en"},
|
||||
"leaf": {testLeafPath, "index.en"},
|
||||
"branch": {testBranchPath, "_index.en"},
|
||||
} {
|
||||
name, tc := name, tc
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if actual := domain.NewFile(tc.input).BaseFileName(); actual != tc.expect {
|
||||
if actual := domain.NewPath(tc.input).BaseFileName(); actual != tc.expect {
|
||||
t.Errorf("BaseFileName() = '%s', want '%s'", actual, tc.expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFile_ContentBaseName(t *testing.T) {
|
||||
func TestPath_ContentBaseName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for name, tc := range map[string]struct {
|
||||
input, expect string
|
||||
}{
|
||||
"regular": {testRegularFile, "a"},
|
||||
"leaf": {testLeafFile, "b"},
|
||||
"branch": {testBranchFile, "news"},
|
||||
"regular": {testRegularPath, "a"},
|
||||
"leaf": {testLeafPath, "b"},
|
||||
"branch": {testBranchPath, "news"},
|
||||
} {
|
||||
name, tc := name, tc
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if actual := domain.NewFile(tc.input).ContentBaseName(); actual != tc.expect {
|
||||
if actual := domain.NewPath(tc.input).ContentBaseName(); actual != tc.expect {
|
||||
t.Errorf("ContentBaseName() = '%s', want '%s'", actual, tc.expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFile_Dir(t *testing.T) {
|
||||
func TestPath_Dir(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for name, tc := range map[string]struct {
|
||||
input, expect string
|
||||
}{
|
||||
"regular": {testRegularFile, "news/"},
|
||||
"leaf": {testLeafFile, "news/b/"},
|
||||
"branch": {testBranchFile, "news/"},
|
||||
"regular": {testRegularPath, "news/"},
|
||||
"leaf": {testLeafPath, "news/b/"},
|
||||
"branch": {testBranchPath, "news/"},
|
||||
} {
|
||||
name, tc := name, tc
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if actual := domain.NewFile(tc.input).Dir(); actual != tc.expect {
|
||||
if actual := domain.NewPath(tc.input).Dir(); actual != tc.expect {
|
||||
t.Errorf("Dir() = '%s', want '%s'", actual, tc.expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFile_Ext(t *testing.T) {
|
||||
func TestPath_Ext(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const expect string = "md"
|
||||
|
||||
for name, input := range map[string]string{
|
||||
"regular": testRegularFile,
|
||||
"leaf": testLeafFile,
|
||||
"branch": testBranchFile,
|
||||
"regular": testRegularPath,
|
||||
"leaf": testLeafPath,
|
||||
"branch": testBranchPath,
|
||||
} {
|
||||
name, input := name, input
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if actual := domain.NewFile(input).Ext(); actual != expect {
|
||||
if actual := domain.NewPath(input).Ext(); actual != expect {
|
||||
t.Errorf("Ext() = '%s', want '%s'", actual, expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFile_Language(t *testing.T) {
|
||||
func TestPath_Language(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var expect domain.Language = domain.NewLanguage("en")
|
||||
|
||||
for name, input := range map[string]string{
|
||||
"regular": testRegularFile,
|
||||
"leaf": testLeafFile,
|
||||
"branch": testBranchFile,
|
||||
"regular": testRegularPath,
|
||||
"leaf": testLeafPath,
|
||||
"branch": testBranchPath,
|
||||
} {
|
||||
name, input := name, input
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if actual := domain.NewFile(input).Language; actual != expect {
|
||||
if actual := domain.NewPath(input).Language; actual != expect {
|
||||
t.Errorf("Language() = '%s', want '%s'", actual, expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFile_LogicalName(t *testing.T) {
|
||||
func TestPath_LogicalName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for name, tc := range map[string]struct {
|
||||
input, expect string
|
||||
}{
|
||||
"regular": {testRegularFile, "a.en.md"},
|
||||
"leaf": {testLeafFile, "index.en.md"},
|
||||
"branch": {testBranchFile, "_index.en.md"},
|
||||
"regular": {testRegularPath, "a.en.md"},
|
||||
"leaf": {testLeafPath, "index.en.md"},
|
||||
"branch": {testBranchPath, "_index.en.md"},
|
||||
} {
|
||||
name, tc := name, tc
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if actual := domain.NewFile(tc.input).LogicalName(); actual != tc.expect {
|
||||
if actual := domain.NewPath(tc.input).LogicalName(); actual != tc.expect {
|
||||
t.Errorf("LogicalName() = '%s', want '%s'", actual, tc.expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFile_TranslationBaseName(t *testing.T) {
|
||||
func TestPath_TranslationBaseName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for name, tc := range map[string]struct {
|
||||
input, expect string
|
||||
}{
|
||||
"regular": {testRegularFile, "a"},
|
||||
"leaf": {testLeafFile, "index"},
|
||||
"branch": {testBranchFile, "_index"},
|
||||
"regular": {testRegularPath, "a"},
|
||||
"leaf": {testLeafPath, "index"},
|
||||
"branch": {testBranchPath, "_index"},
|
||||
} {
|
||||
name, tc := name, tc
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if actual := domain.NewFile(tc.input).TranslationBaseName(); actual != tc.expect {
|
||||
if actual := domain.NewPath(tc.input).TranslationBaseName(); actual != tc.expect {
|
||||
t.Errorf("TranslationBaseName() = '%s', want '%s'", actual, tc.expect)
|
||||
}
|
||||
})
|
|
@ -15,7 +15,7 @@ import (
|
|||
)
|
||||
|
||||
type Resource struct {
|
||||
File File
|
||||
File Path
|
||||
|
||||
modTime time.Time
|
||||
params map[string]any // TODO(toby3d): set from Page configuration
|
||||
|
@ -30,7 +30,7 @@ type Resource struct {
|
|||
func NewResource(modTime time.Time, r io.Reader, key string) *Resource {
|
||||
mediaType, _, _ := mime.ParseMediaType(mime.TypeByExtension(path.Ext(key)))
|
||||
out := &Resource{
|
||||
File: NewFile(key),
|
||||
File: NewPath(key),
|
||||
modTime: modTime,
|
||||
key: key,
|
||||
name: key, // TODO(toby3d): set from Page configuration
|
||||
|
|
|
@ -15,7 +15,7 @@ type Site struct {
|
|||
BaseURL *url.URL
|
||||
Params map[string]any
|
||||
TimeZone *time.Location
|
||||
File File
|
||||
File Path
|
||||
Title string
|
||||
Resources Resources
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ func TestSite(tb testing.TB) *Site {
|
|||
Languages: []Language{en, ru},
|
||||
BaseURL: &url.URL{Scheme: "http", Host: "127.0.0.1:3000", Path: "/"},
|
||||
TimeZone: time.UTC,
|
||||
File: NewFile(filepath.Join("content", "index.en.md")),
|
||||
File: NewPath(filepath.Join("content", "index.en.md")),
|
||||
Title: "Testing",
|
||||
Resources: make([]*Resource, 0),
|
||||
Params: map[string]any{
|
||||
|
|
|
@ -57,7 +57,7 @@ func (repo *fileSystemPageRepository) Get(ctx context.Context, lang domain.Langu
|
|||
}
|
||||
|
||||
return &domain.Page{
|
||||
File: domain.NewFile(target),
|
||||
File: domain.NewPath(target),
|
||||
Language: lang,
|
||||
Title: data.Title,
|
||||
Content: data.Content,
|
||||
|
|
|
@ -71,7 +71,7 @@ func (repo *fileSystemSiteRepository) Get(ctx context.Context, lang domain.Langu
|
|||
}
|
||||
|
||||
return &domain.Site{
|
||||
File: domain.NewFile(target),
|
||||
File: domain.NewPath(target),
|
||||
DefaultLanguage: data.DefaultLanguage.Language,
|
||||
Language: lang,
|
||||
Title: data.Title,
|
||||
|
|
Loading…
Reference in New Issue