github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/hugolib/fileInfo.go (about) 1 // Copyright 2017-present 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 hugolib 15 16 import ( 17 "strings" 18 19 "github.com/gohugoio/hugo/hugofs/files" 20 21 "github.com/pkg/errors" 22 23 "github.com/gohugoio/hugo/hugofs" 24 25 "github.com/spf13/afero" 26 27 "github.com/gohugoio/hugo/source" 28 ) 29 30 // fileInfo implements the File and ReadableFile interface. 31 var ( 32 _ source.File = (*fileInfo)(nil) 33 ) 34 35 type fileInfo struct { 36 source.File 37 38 overriddenLang string 39 } 40 41 func (fi *fileInfo) Open() (afero.File, error) { 42 f, err := fi.FileInfo().Meta().Open() 43 if err != nil { 44 err = errors.Wrap(err, "fileInfo") 45 } 46 47 return f, err 48 } 49 50 func (fi *fileInfo) Lang() string { 51 if fi.overriddenLang != "" { 52 return fi.overriddenLang 53 } 54 return fi.File.Lang() 55 } 56 57 func (fi *fileInfo) String() string { 58 if fi == nil || fi.File == nil { 59 return "" 60 } 61 return fi.Path() 62 } 63 64 // TODO(bep) rename 65 func newFileInfo(sp *source.SourceSpec, fi hugofs.FileMetaInfo) (*fileInfo, error) { 66 baseFi, err := sp.NewFileInfo(fi) 67 if err != nil { 68 return nil, err 69 } 70 71 f := &fileInfo{ 72 File: baseFi, 73 } 74 75 return f, nil 76 } 77 78 type bundleDirType int 79 80 const ( 81 bundleNot bundleDirType = iota 82 83 // All from here are bundles in one form or another. 84 bundleLeaf 85 bundleBranch 86 ) 87 88 // Returns the given file's name's bundle type and whether it is a content 89 // file or not. 90 func classifyBundledFile(name string) (bundleDirType, bool) { 91 if !files.IsContentFile(name) { 92 return bundleNot, false 93 } 94 if strings.HasPrefix(name, "_index.") { 95 return bundleBranch, true 96 } 97 98 if strings.HasPrefix(name, "index.") { 99 return bundleLeaf, true 100 } 101 102 return bundleNot, true 103 } 104 105 func (b bundleDirType) String() string { 106 switch b { 107 case bundleNot: 108 return "Not a bundle" 109 case bundleLeaf: 110 return "Regular bundle" 111 case bundleBranch: 112 return "Branch bundle" 113 } 114 115 return "" 116 }