127 lines
2.8 KiB
Go
127 lines
2.8 KiB
Go
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))
|
|
|
|
if out.dir[len(out.dir)-1] != '/' {
|
|
out.dir += string(filepath.Separator)
|
|
}
|
|
|
|
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 + ")"
|
|
}
|