Debian Codegraph @

hugofs/files/classifier.go

raw
1 // Copyright 2019 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 files
15
16 import (
17 "os"
18 "path/filepath"
19 "sort"
20 "strings"
21 )
22
23 const (
24 // When merging, we check for this file first, and then package.json.
25 FilenamePackageHugoJSON = "package.hugo.json"
26 // The NPM package file.
27 FilenamePackageJSON = "package.json"
28
29 FilenameHugoStatsJSON = "hugo_stats.json"
30 FilenamePackageMetaJSON = "hugo_packagemeta.json"
31 )
32
33 // FolderPackagesHugoAutoGen is the auto-generated npm workspace directory for Hugo module deps.
34 var FolderPackagesHugoAutoGen = filepath.Join("packages", "hugoautogen")
35
36 func IsGoTmplExt(ext string) bool {
37 return ext == "gotmpl"
38 }
39
40 // Supported data file extensions for _content.* files.
41 func IsContentDataExt(ext string) bool {
42 return IsGoTmplExt(ext)
43 }
44
45 const (
46 ComponentFolderArchetypes = "archetypes"
47 ComponentFolderStatic = "static"
48 ComponentFolderLayouts = "layouts"
49 ComponentFolderContent = "content"
50 ComponentFolderData = "data"
51 ComponentFolderAssets = "assets"
52 ComponentFolderI18n = "i18n"
53
54 FolderResources = "resources"
55 FolderJSConfig = "_jsconfig" // Mounted below /assets with postcss.config.js etc.
56
57 NameContentData = "_content"
58 )
59
60 var (
61 JsConfigFolderMountPrefix = filepath.Join(ComponentFolderAssets, FolderJSConfig)
62
63 ComponentFolders = []string{
64 ComponentFolderArchetypes,
65 ComponentFolderStatic,
66 ComponentFolderLayouts,
67 ComponentFolderContent,
68 ComponentFolderData,
69 ComponentFolderAssets,
70 ComponentFolderI18n,
71 }
72
73 componentFoldersSet = make(map[string]bool)
74 )
75
76 func init() {
77 sort.Strings(ComponentFolders)
78 for _, f := range ComponentFolders {
79 componentFoldersSet[f] = true
80 }
81 }
82
83 // ResolveComponentFolder returns "content" from "content/blog/foo.md" etc.
84 func ResolveComponentFolder(filename string) string {
85 filename = strings.TrimPrefix(filename, string(os.PathSeparator))
86 for _, cf := range ComponentFolders {
87 if strings.HasPrefix(filename, cf) {
88 return cf
89 }
90 }
91
92 return ""
93 }
94
95 func IsComponentFolder(name string) bool {
96 return componentFoldersSet[name]
97 }