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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hugofs
import (
iofs "io/fs"
"os"
"path"
"runtime"
"sort"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/hugofs/files"
"github.com/spf13/afero"
"golang.org/x/text/unicode/norm"
)
// NewComponentFs creates a new component filesystem.
func NewComponentFs(opts ComponentFsOptions) *componentFs {
if opts.Component == "" {
panic("ComponentFsOptions.PathParser.Component must be set")
}
if opts.Fs == nil {
panic("ComponentFsOptions.Fs must be set")
}
bfs := NewBasePathFs(opts.Fs, opts.Component)
return &componentFs{Fs: bfs, opts: opts}
}
var _ FilesystemUnwrapper = (*componentFs)(nil)
// componentFs is a filesystem that holds one of the Hugo components, e.g. content, layouts etc.
type componentFs struct {
afero.Fs
opts ComponentFsOptions
}
func (fs *componentFs) UnwrapFilesystem() afero.Fs {
return fs.Fs
}
type componentFsDir struct {
*noOpRegularFileOps
DirOnlyOps
name string // the name passed to Open
fs *componentFs
}
// ReadDir reads count entries from this virtual directory and
// sorts the entries according to the component filesystem rules.
func (f *componentFsDir) ReadDir(count int) ([]iofs.DirEntry, error) {
fis, err := f.DirOnlyOps.(iofs.ReadDirFile).ReadDir(-1)
if err != nil {
return nil, err
}
// Filter out any symlinks.
n := 0
for _, fi := range fis {
// IsDir will always be false for symlinks.
keep := fi.IsDir()
if !keep {
// This is unfortunate, but is the only way to determine if it is a symlink.
info, err := fi.Info()
if err != nil {
if herrors.IsNotExist(err) {
continue
}
return nil, err
}
if info.Mode()&os.ModeSymlink == 0 {
keep = true
}
}
if keep {
fis[n] = fi
n++
}
}
fis = fis[:n]
n = 0
for _, fi := range fis {
s := path.Join(f.name, fi.Name())
if _, ok := f.fs.applyMeta(fi, s); ok {
fis[n] = fi
n++
}
}
fis = fis[:n]
sort.Slice(fis, func(i, j int) bool {
fimi, fimj := fis[i].(FileMetaInfo), fis[j].(FileMetaInfo)
if fimi.IsDir() != fimj.IsDir() {
return fimi.IsDir()
}
fimim, fimjm := fimi.Meta(), fimj.Meta()
if fimim.ModuleOrdinal != fimjm.ModuleOrdinal {
switch f.fs.opts.Component {
case files.ComponentFolderI18n:
// The way the language files gets loaded means that
// we need to provide the least important files first (e.g. the theme files).
return fimim.ModuleOrdinal > fimjm.ModuleOrdinal
default:
return fimim.ModuleOrdinal < fimjm.ModuleOrdinal
}
}
pii, pij := fimim.PathInfo, fimjm.PathInfo
if pii != nil {
basei, basej := pii.Base(), pij.Base()
exti, extj := pii.Ext(), pij.Ext()
if f.fs.opts.Component == files.ComponentFolderContent {
// Pull bundles to the top.
if pii.IsBundle() != pij.IsBundle() {
return pii.IsBundle()
}
}
if exti != extj {
// This pulls .md above .html.
return exti > extj
}
if basei != basej {
return basei < basej
}
}
if fimim.Weight != fimjm.Weight {
return fimim.Weight > fimjm.Weight
}
return fimi.Name() < fimj.Name()
})
return fis, nil
}
func (f *componentFsDir) Stat() (iofs.FileInfo, error) {
fi, err := f.DirOnlyOps.Stat()
if err != nil {
return nil, err
}
fim, _ := f.fs.applyMeta(fi, f.name)
return fim, nil
}
func (fs *componentFs) Stat(name string) (os.FileInfo, error) {
fi, err := fs.Fs.Stat(name)
if err != nil {
return nil, err
}
fim, _ := fs.applyMeta(fi, name)
return fim, nil
}
func (fs *componentFs) applyMeta(fi FileNameIsDir, name string) (FileMetaInfo, bool) {
if runtime.GOOS == "darwin" {
name = norm.NFC.String(name)
}
fim := fi.(FileMetaInfo)
meta := fim.Meta()
pi := fs.opts.PathParser.Parse(fs.opts.Component, name)
if pi.Disabled() {
return fim, false
}
if meta.Lang != "" {
if isLangDisabled := fs.opts.PathParser.IsLangDisabled; isLangDisabled != nil && isLangDisabled(meta.Lang) {
return fim, false
}
}
meta.PathInfo = pi
if !fim.IsDir() {
if fileLang := meta.PathInfo.Lang(); fileLang != "" {
// A valid lang set in filename.
// Give priority to myfile.sv.txt inside the sv filesystem.
meta.Weight++
meta.Lang = fileLang
}
}
if meta.Lang == "" {
meta.Lang = fs.opts.DefaultContentLanguage
}
langIdx, found := fs.opts.PathParser.LanguageIndex[meta.Lang]
if !found {
panic("no language found for " + meta
|