Debian Codegraph @

source/fileInfo.go

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