hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

fileInfo.go (2335B)

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