🚚 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 {
|
type Page struct {
|
||||||
Language Language
|
Language Language
|
||||||
Params map[string]any
|
Params map[string]any
|
||||||
File File
|
File Path
|
||||||
Description string
|
Description string
|
||||||
Title string
|
Title string
|
||||||
Content []byte
|
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 (
|
var (
|
||||||
testRegularFile string = filepath.Join("news", "a.en.md")
|
testRegularPath string = filepath.Join("news", "a.en.md")
|
||||||
testLeafFile string = filepath.Join("news", "b", "index.en.md")
|
testLeafPath string = filepath.Join("news", "b", "index.en.md")
|
||||||
testBranchFile string = filepath.Join("news", "_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()
|
t.Parallel()
|
||||||
|
|
||||||
for name, tc := range map[string]struct {
|
for name, tc := range map[string]struct {
|
||||||
input, expect string
|
input, expect string
|
||||||
}{
|
}{
|
||||||
"regular": {testRegularFile, "a.en"},
|
"regular": {testRegularPath, "a.en"},
|
||||||
"leaf": {testLeafFile, "index.en"},
|
"leaf": {testLeafPath, "index.en"},
|
||||||
"branch": {testBranchFile, "_index.en"},
|
"branch": {testBranchPath, "_index.en"},
|
||||||
} {
|
} {
|
||||||
name, tc := name, tc
|
name, tc := name, tc
|
||||||
|
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
t.Parallel()
|
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)
|
t.Errorf("BaseFileName() = '%s', want '%s'", actual, tc.expect)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFile_ContentBaseName(t *testing.T) {
|
func TestPath_ContentBaseName(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
for name, tc := range map[string]struct {
|
for name, tc := range map[string]struct {
|
||||||
input, expect string
|
input, expect string
|
||||||
}{
|
}{
|
||||||
"regular": {testRegularFile, "a"},
|
"regular": {testRegularPath, "a"},
|
||||||
"leaf": {testLeafFile, "b"},
|
"leaf": {testLeafPath, "b"},
|
||||||
"branch": {testBranchFile, "news"},
|
"branch": {testBranchPath, "news"},
|
||||||
} {
|
} {
|
||||||
name, tc := name, tc
|
name, tc := name, tc
|
||||||
|
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
t.Parallel()
|
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)
|
t.Errorf("ContentBaseName() = '%s', want '%s'", actual, tc.expect)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFile_Dir(t *testing.T) {
|
func TestPath_Dir(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
for name, tc := range map[string]struct {
|
for name, tc := range map[string]struct {
|
||||||
input, expect string
|
input, expect string
|
||||||
}{
|
}{
|
||||||
"regular": {testRegularFile, "news/"},
|
"regular": {testRegularPath, "news/"},
|
||||||
"leaf": {testLeafFile, "news/b/"},
|
"leaf": {testLeafPath, "news/b/"},
|
||||||
"branch": {testBranchFile, "news/"},
|
"branch": {testBranchPath, "news/"},
|
||||||
} {
|
} {
|
||||||
name, tc := name, tc
|
name, tc := name, tc
|
||||||
|
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
t.Parallel()
|
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)
|
t.Errorf("Dir() = '%s', want '%s'", actual, tc.expect)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFile_Ext(t *testing.T) {
|
func TestPath_Ext(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
const expect string = "md"
|
const expect string = "md"
|
||||||
|
|
||||||
for name, input := range map[string]string{
|
for name, input := range map[string]string{
|
||||||
"regular": testRegularFile,
|
"regular": testRegularPath,
|
||||||
"leaf": testLeafFile,
|
"leaf": testLeafPath,
|
||||||
"branch": testBranchFile,
|
"branch": testBranchPath,
|
||||||
} {
|
} {
|
||||||
name, input := name, input
|
name, input := name, input
|
||||||
|
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
t.Parallel()
|
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)
|
t.Errorf("Ext() = '%s', want '%s'", actual, expect)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFile_Language(t *testing.T) {
|
func TestPath_Language(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
var expect domain.Language = domain.NewLanguage("en")
|
var expect domain.Language = domain.NewLanguage("en")
|
||||||
|
|
||||||
for name, input := range map[string]string{
|
for name, input := range map[string]string{
|
||||||
"regular": testRegularFile,
|
"regular": testRegularPath,
|
||||||
"leaf": testLeafFile,
|
"leaf": testLeafPath,
|
||||||
"branch": testBranchFile,
|
"branch": testBranchPath,
|
||||||
} {
|
} {
|
||||||
name, input := name, input
|
name, input := name, input
|
||||||
|
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
t.Parallel()
|
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)
|
t.Errorf("Language() = '%s', want '%s'", actual, expect)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFile_LogicalName(t *testing.T) {
|
func TestPath_LogicalName(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
for name, tc := range map[string]struct {
|
for name, tc := range map[string]struct {
|
||||||
input, expect string
|
input, expect string
|
||||||
}{
|
}{
|
||||||
"regular": {testRegularFile, "a.en.md"},
|
"regular": {testRegularPath, "a.en.md"},
|
||||||
"leaf": {testLeafFile, "index.en.md"},
|
"leaf": {testLeafPath, "index.en.md"},
|
||||||
"branch": {testBranchFile, "_index.en.md"},
|
"branch": {testBranchPath, "_index.en.md"},
|
||||||
} {
|
} {
|
||||||
name, tc := name, tc
|
name, tc := name, tc
|
||||||
|
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
t.Parallel()
|
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)
|
t.Errorf("LogicalName() = '%s', want '%s'", actual, tc.expect)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFile_TranslationBaseName(t *testing.T) {
|
func TestPath_TranslationBaseName(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
for name, tc := range map[string]struct {
|
for name, tc := range map[string]struct {
|
||||||
input, expect string
|
input, expect string
|
||||||
}{
|
}{
|
||||||
"regular": {testRegularFile, "a"},
|
"regular": {testRegularPath, "a"},
|
||||||
"leaf": {testLeafFile, "index"},
|
"leaf": {testLeafPath, "index"},
|
||||||
"branch": {testBranchFile, "_index"},
|
"branch": {testBranchPath, "_index"},
|
||||||
} {
|
} {
|
||||||
name, tc := name, tc
|
name, tc := name, tc
|
||||||
|
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
t.Parallel()
|
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)
|
t.Errorf("TranslationBaseName() = '%s', want '%s'", actual, tc.expect)
|
||||||
}
|
}
|
||||||
})
|
})
|
|
@ -15,7 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resource struct {
|
type Resource struct {
|
||||||
File File
|
File Path
|
||||||
|
|
||||||
modTime time.Time
|
modTime time.Time
|
||||||
params map[string]any // TODO(toby3d): set from Page configuration
|
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 {
|
func NewResource(modTime time.Time, r io.Reader, key string) *Resource {
|
||||||
mediaType, _, _ := mime.ParseMediaType(mime.TypeByExtension(path.Ext(key)))
|
mediaType, _, _ := mime.ParseMediaType(mime.TypeByExtension(path.Ext(key)))
|
||||||
out := &Resource{
|
out := &Resource{
|
||||||
File: NewFile(key),
|
File: NewPath(key),
|
||||||
modTime: modTime,
|
modTime: modTime,
|
||||||
key: key,
|
key: key,
|
||||||
name: key, // TODO(toby3d): set from Page configuration
|
name: key, // TODO(toby3d): set from Page configuration
|
||||||
|
|
|
@ -15,7 +15,7 @@ type Site struct {
|
||||||
BaseURL *url.URL
|
BaseURL *url.URL
|
||||||
Params map[string]any
|
Params map[string]any
|
||||||
TimeZone *time.Location
|
TimeZone *time.Location
|
||||||
File File
|
File Path
|
||||||
Title string
|
Title string
|
||||||
Resources Resources
|
Resources Resources
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ func TestSite(tb testing.TB) *Site {
|
||||||
Languages: []Language{en, ru},
|
Languages: []Language{en, ru},
|
||||||
BaseURL: &url.URL{Scheme: "http", Host: "127.0.0.1:3000", Path: "/"},
|
BaseURL: &url.URL{Scheme: "http", Host: "127.0.0.1:3000", Path: "/"},
|
||||||
TimeZone: time.UTC,
|
TimeZone: time.UTC,
|
||||||
File: NewFile(filepath.Join("content", "index.en.md")),
|
File: NewPath(filepath.Join("content", "index.en.md")),
|
||||||
Title: "Testing",
|
Title: "Testing",
|
||||||
Resources: make([]*Resource, 0),
|
Resources: make([]*Resource, 0),
|
||||||
Params: map[string]any{
|
Params: map[string]any{
|
||||||
|
|
|
@ -57,7 +57,7 @@ func (repo *fileSystemPageRepository) Get(ctx context.Context, lang domain.Langu
|
||||||
}
|
}
|
||||||
|
|
||||||
return &domain.Page{
|
return &domain.Page{
|
||||||
File: domain.NewFile(target),
|
File: domain.NewPath(target),
|
||||||
Language: lang,
|
Language: lang,
|
||||||
Title: data.Title,
|
Title: data.Title,
|
||||||
Content: data.Content,
|
Content: data.Content,
|
||||||
|
|
|
@ -71,7 +71,7 @@ func (repo *fileSystemSiteRepository) Get(ctx context.Context, lang domain.Langu
|
||||||
}
|
}
|
||||||
|
|
||||||
return &domain.Site{
|
return &domain.Site{
|
||||||
File: domain.NewFile(target),
|
File: domain.NewPath(target),
|
||||||
DefaultLanguage: data.DefaultLanguage.Language,
|
DefaultLanguage: data.DefaultLanguage.Language,
|
||||||
Language: lang,
|
Language: lang,
|
||||||
Title: data.Title,
|
Title: data.Title,
|
||||||
|
|
Loading…
Reference in New Issue