URI: 
       fileInfo.go - hugo - [fork] hugo port for 9front
  HTML git clone https://git.drkhsh.at/hugo.git
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
   DIR README
   DIR LICENSE
       ---
       fileInfo.go (4484B)
       ---
            1 // Copyright 2021 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
           15 
           16 import (
           17         "path/filepath"
           18         "sync"
           19 
           20         "github.com/bep/gitmap"
           21         "github.com/gohugoio/hugo/common/hashing"
           22         "github.com/gohugoio/hugo/common/hugo"
           23         "github.com/gohugoio/hugo/common/paths"
           24         "github.com/gohugoio/hugo/hugofs/files"
           25 
           26         "github.com/gohugoio/hugo/common/hugio"
           27 
           28         "github.com/gohugoio/hugo/hugofs"
           29 )
           30 
           31 // File describes a source file.
           32 type File struct {
           33         fim hugofs.FileMetaInfo
           34 
           35         uniqueID string
           36         lazyInit sync.Once
           37 }
           38 
           39 // IsContentAdapter returns whether the file represents a content adapter.
           40 // This means that there may be more than one Page associated with this file.
           41 func (fi *File) IsContentAdapter() bool {
           42         return fi.fim.Meta().PathInfo.IsContentData()
           43 }
           44 
           45 // Filename returns a file's absolute path and filename on disk.
           46 func (fi *File) Filename() string { return fi.fim.Meta().Filename }
           47 
           48 // Path gets the relative path including file name and extension.  The directory
           49 // is relative to the content root.
           50 func (fi *File) Path() string { return filepath.Join(fi.p().Dir()[1:], fi.p().Name()) }
           51 
           52 // Dir gets the name of the directory that contains this file.  The directory is
           53 // relative to the content root.
           54 func (fi *File) Dir() string {
           55         return fi.pathToDir(fi.p().Dir())
           56 }
           57 
           58 // Ext returns a file's extension without the leading period (e.g. "md").
           59 func (fi *File) Ext() string { return fi.p().Ext() }
           60 
           61 // Lang returns a file's language (e.g. "sv").
           62 // Deprecated: Use .Page.Language.Lang instead.
           63 func (fi *File) Lang() string {
           64         hugo.Deprecate(".Page.File.Lang", "Use .Page.Language.Lang instead.", "v0.123.0")
           65         return fi.fim.Meta().Lang
           66 }
           67 
           68 // LogicalName returns a file's name and extension (e.g. "page.sv.md").
           69 func (fi *File) LogicalName() string {
           70         return fi.p().Name()
           71 }
           72 
           73 // BaseFileName returns a file's name without extension (e.g. "page.sv").
           74 func (fi *File) BaseFileName() string {
           75         return fi.p().NameNoExt()
           76 }
           77 
           78 // TranslationBaseName returns a file's translation base name without the
           79 // language segment (e.g. "page").
           80 func (fi *File) TranslationBaseName() string { return fi.p().NameNoIdentifier() }
           81 
           82 // ContentBaseName is a either TranslationBaseName or name of containing folder
           83 // if file is a bundle.
           84 func (fi *File) ContentBaseName() string {
           85         return fi.p().BaseNameNoIdentifier()
           86 }
           87 
           88 // Section returns a file's section.
           89 func (fi *File) Section() string {
           90         return fi.p().Section()
           91 }
           92 
           93 // UniqueID returns a file's unique, MD5 hash identifier.
           94 func (fi *File) UniqueID() string {
           95         fi.init()
           96         return fi.uniqueID
           97 }
           98 
           99 // FileInfo returns a file's underlying os.FileInfo.
          100 func (fi *File) FileInfo() hugofs.FileMetaInfo { return fi.fim }
          101 
          102 func (fi *File) String() string { return fi.BaseFileName() }
          103 
          104 // Open implements ReadableFile.
          105 func (fi *File) Open() (hugio.ReadSeekCloser, error) {
          106         f, err := fi.fim.Meta().Open()
          107 
          108         return f, err
          109 }
          110 
          111 func (fi *File) IsZero() bool {
          112         return fi == nil
          113 }
          114 
          115 // We create a lot of these FileInfo objects, but there are parts of it used only
          116 // in some cases that is slightly expensive to construct.
          117 func (fi *File) init() {
          118         fi.lazyInit.Do(func() {
          119                 fi.uniqueID = hashing.MD5FromStringHexEncoded(filepath.ToSlash(fi.Path()))
          120         })
          121 }
          122 
          123 func (fi *File) pathToDir(s string) string {
          124         if s == "" {
          125                 return s
          126         }
          127         return filepath.FromSlash(s[1:] + "/")
          128 }
          129 
          130 func (fi *File) p() *paths.Path {
          131         return fi.fim.Meta().PathInfo.Unnormalized()
          132 }
          133 
          134 var contentPathParser = &paths.PathParser{
          135         IsContentExt: func(ext string) bool {
          136                 return true
          137         },
          138 }
          139 
          140 // Used in tests.
          141 func NewContentFileInfoFrom(path, filename string) *File {
          142         meta := &hugofs.FileMeta{
          143                 Filename: filename,
          144                 PathInfo: contentPathParser.Parse(files.ComponentFolderContent, filepath.ToSlash(path)),
          145         }
          146 
          147         return NewFileInfo(hugofs.NewFileMetaInfo(nil, meta))
          148 }
          149 
          150 func NewFileInfo(fi hugofs.FileMetaInfo) *File {
          151         return &File{
          152                 fim: fi,
          153         }
          154 }
          155 
          156 // GitInfo provides information about a version controlled source file.
          157 type GitInfo = gitmap.GitInfo