github.com/dominikszabo/hugo-ds-clean@v0.47.1/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/helpers"
    20  	"github.com/gohugoio/hugo/source"
    21  )
    22  
    23  // fileInfo implements the File and ReadableFile interface.
    24  var (
    25  	_ source.File         = (*fileInfo)(nil)
    26  	_ source.ReadableFile = (*fileInfo)(nil)
    27  	_ pathLangFile        = (*fileInfo)(nil)
    28  )
    29  
    30  // A partial interface to prevent ambigous compiler error.
    31  type basePather interface {
    32  	Filename() string
    33  	RealName() string
    34  	BaseDir() string
    35  }
    36  
    37  type fileInfo struct {
    38  	bundleTp bundleDirType
    39  
    40  	source.ReadableFile
    41  	basePather
    42  
    43  	overriddenLang string
    44  
    45  	// Set if the content language for this file is disabled.
    46  	disabled bool
    47  }
    48  
    49  func (fi *fileInfo) Lang() string {
    50  	if fi.overriddenLang != "" {
    51  		return fi.overriddenLang
    52  	}
    53  	return fi.ReadableFile.Lang()
    54  }
    55  
    56  func (fi *fileInfo) Filename() string {
    57  	return fi.basePather.Filename()
    58  }
    59  
    60  func (fi *fileInfo) isOwner() bool {
    61  	return fi.bundleTp > bundleNot
    62  }
    63  
    64  func isContentFile(filename string) bool {
    65  	return contentFileExtensionsSet[strings.TrimPrefix(helpers.Ext(filename), ".")]
    66  }
    67  
    68  func (fi *fileInfo) isContentFile() bool {
    69  	return contentFileExtensionsSet[fi.Ext()]
    70  }
    71  
    72  func newFileInfo(sp *source.SourceSpec, baseDir, filename string, fi pathLangFileFi, tp bundleDirType) *fileInfo {
    73  
    74  	baseFi := sp.NewFileInfo(baseDir, filename, tp == bundleLeaf, fi)
    75  	f := &fileInfo{
    76  		bundleTp:     tp,
    77  		ReadableFile: baseFi,
    78  		basePather:   fi,
    79  	}
    80  
    81  	lang := f.Lang()
    82  	f.disabled = lang != "" && sp.DisabledLanguages[lang]
    83  
    84  	return f
    85  
    86  }
    87  
    88  type bundleDirType int
    89  
    90  const (
    91  	bundleNot bundleDirType = iota
    92  
    93  	// All from here are bundles in one form or another.
    94  	bundleLeaf
    95  	bundleBranch
    96  )
    97  
    98  // Returns the given file's name's bundle type and whether it is a content
    99  // file or not.
   100  func classifyBundledFile(name string) (bundleDirType, bool) {
   101  	if !isContentFile(name) {
   102  		return bundleNot, false
   103  	}
   104  	if strings.HasPrefix(name, "_index.") {
   105  		return bundleBranch, true
   106  	}
   107  
   108  	if strings.HasPrefix(name, "index.") {
   109  		return bundleLeaf, true
   110  	}
   111  
   112  	return bundleNot, true
   113  }
   114  
   115  func (b bundleDirType) String() string {
   116  	switch b {
   117  	case bundleNot:
   118  		return "Not a bundle"
   119  	case bundleLeaf:
   120  		return "Regular bundle"
   121  	case bundleBranch:
   122  		return "Branch bundle"
   123  	}
   124  
   125  	return ""
   126  }