sourceSpec.go - hugo - [fork] hugo port for 9front
(HTM) git clone https://git.drkhsh.at/hugo.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
sourceSpec.go (2210B)
---
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // Package source contains the types and functions related to source files.
15 package source
16
17 import (
18 "path/filepath"
19 "runtime"
20
21 "github.com/gohugoio/hugo/hugofs/glob"
22
23 "github.com/spf13/afero"
24
25 "github.com/gohugoio/hugo/helpers"
26 )
27
28 // SourceSpec abstracts language-specific file creation.
29 // TODO(bep) rename to Spec
30 type SourceSpec struct {
31 *helpers.PathSpec
32
33 SourceFs afero.Fs
34
35 shouldInclude func(filename string) bool
36 }
37
38 // NewSourceSpec initializes SourceSpec using languages the given filesystem and PathSpec.
39 func NewSourceSpec(ps *helpers.PathSpec, inclusionFilter *glob.FilenameFilter, fs afero.Fs) *SourceSpec {
40 shouldInclude := func(filename string) bool {
41 if !inclusionFilter.Match(filename, false) {
42 return false
43 }
44 if ps.Cfg.IgnoreFile(filename) {
45 return false
46 }
47
48 return true
49 }
50
51 return &SourceSpec{shouldInclude: shouldInclude, PathSpec: ps, SourceFs: fs}
52 }
53
54 // IgnoreFile returns whether a given file should be ignored.
55 func (s *SourceSpec) IgnoreFile(filename string) bool {
56 if filename == "" {
57 if _, ok := s.SourceFs.(*afero.OsFs); ok {
58 return true
59 }
60 return false
61 }
62
63 base := filepath.Base(filename)
64
65 if len(base) > 0 {
66 first := base[0]
67 last := base[len(base)-1]
68 if first == '.' ||
69 first == '#' ||
70 last == '~' {
71 return true
72 }
73 }
74
75 if !s.shouldInclude(filename) {
76 return true
77 }
78
79 if runtime.GOOS == "windows" {
80 // Also check the forward slash variant if different.
81 unixFilename := filepath.ToSlash(filename)
82 if unixFilename != filename {
83 if !s.shouldInclude(unixFilename) {
84 return true
85 }
86 }
87 }
88
89 return false
90 }