-
Notifications
You must be signed in to change notification settings - Fork 0
/
sourceSpec.go
107 lines (88 loc) · 2.38 KB
/
sourceSpec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package source
import (
"fmt"
"github.com/spf13/afero"
"github.com/sunwei/hugo-playground/common/paths"
"github.com/sunwei/hugo-playground/helpers"
"github.com/sunwei/hugo-playground/hugofs"
"github.com/sunwei/hugo-playground/hugofs/glob"
"path/filepath"
"strings"
)
// SourceSpec abstracts language-specific file creation.
// TODO(bep) rename to Spec
type SourceSpec struct {
*helpers.PathSpec
SourceFs afero.Fs
Languages map[string]any
DefaultContentLanguage string
}
// NewSourceSpec initializes SourceSpec using languages the given filesystem and PathSpec.
func NewSourceSpec(ps *helpers.PathSpec, inclusionFilter *glob.FilenameFilter, fs afero.Fs) *SourceSpec {
cfg := ps.Cfg
defaultLang := cfg.GetString("defaultContentLanguage")
languages := cfg.GetStringMap("languages")
return &SourceSpec{
PathSpec: ps,
SourceFs: fs,
Languages: languages,
DefaultContentLanguage: defaultLang,
}
}
// IgnoreFile returns whether a given file should be ignored.
func (s *SourceSpec) IgnoreFile(filename string) bool {
if filename == "" {
if _, ok := s.SourceFs.(*afero.OsFs); ok {
return true
}
return false
}
base := filepath.Base(filename)
if len(base) > 0 {
first := base[0]
last := base[len(base)-1]
if first == '.' ||
first == '#' ||
last == '~' {
return true
}
}
return false
}
func (sp *SourceSpec) NewFileInfo(fi hugofs.FileMetaInfo) (*FileInfo, error) {
m := fi.Meta()
filename := m.Filename
relPath := m.Path
if relPath == "" {
return nil, fmt.Errorf("no Path provided by %v (%T)", m, m.Fs)
}
if filename == "" {
return nil, fmt.Errorf("no Filename provided by %v (%T)", m, m.Fs)
}
relDir := filepath.Dir(relPath)
if relDir == "." {
relDir = ""
}
if !strings.HasSuffix(relDir, helpers.FilePathSeparator) {
relDir = relDir + helpers.FilePathSeparator
}
dir, name := filepath.Split(relPath)
if !strings.HasSuffix(dir, helpers.FilePathSeparator) {
dir = dir + helpers.FilePathSeparator
}
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(name), "."))
baseName := paths.Filename(name)
f := &FileInfo{
sp: sp,
filename: filename,
fi: fi,
ext: ext,
dir: dir,
relDir: relDir, // Dir()
relPath: relPath, // Path()
name: name,
baseName: baseName, // BaseFileName()
classifier: m.Classifier,
}
return f, nil
}