github.com/neohugo/neohugo@v0.123.8/hugolib/paths/paths.go (about) 1 // Copyright 2018 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 paths 15 16 import ( 17 "path/filepath" 18 "strings" 19 20 hpaths "github.com/neohugo/neohugo/common/paths" 21 22 "github.com/neohugo/neohugo/config" 23 "github.com/neohugo/neohugo/modules" 24 25 "github.com/neohugo/neohugo/hugofs" 26 ) 27 28 var FilePathSeparator = string(filepath.Separator) 29 30 type Paths struct { 31 Fs *hugofs.Fs 32 Cfg config.AllProvider 33 34 // Directories to store Resource related artifacts. 35 AbsResourcesDir string 36 37 AbsPublishDir string 38 39 // When in multihost mode, this returns a list of base paths below PublishDir 40 // for each language. 41 MultihostTargetBasePaths []string 42 } 43 44 func New(fs *hugofs.Fs, cfg config.AllProvider) (*Paths, error) { 45 bcfg := cfg.BaseConfig() 46 publishDir := bcfg.PublishDir 47 if publishDir == "" { 48 panic("publishDir not set") 49 } 50 51 absPublishDir := hpaths.AbsPathify(bcfg.WorkingDir, publishDir) 52 if !strings.HasSuffix(absPublishDir, FilePathSeparator) { 53 absPublishDir += FilePathSeparator 54 } 55 // If root, remove the second '/' 56 if absPublishDir == "//" { 57 absPublishDir = FilePathSeparator 58 } 59 absResourcesDir := hpaths.AbsPathify(bcfg.WorkingDir, cfg.Dirs().ResourceDir) 60 if !strings.HasSuffix(absResourcesDir, FilePathSeparator) { 61 absResourcesDir += FilePathSeparator 62 } 63 if absResourcesDir == "//" { 64 absResourcesDir = FilePathSeparator 65 } 66 67 var multihostTargetBasePaths []string 68 if cfg.IsMultihost() && len(cfg.Languages()) > 1 { 69 for _, l := range cfg.Languages() { 70 multihostTargetBasePaths = append(multihostTargetBasePaths, l.Lang) 71 } 72 } 73 74 p := &Paths{ 75 Fs: fs, 76 Cfg: cfg, 77 AbsResourcesDir: absResourcesDir, 78 AbsPublishDir: absPublishDir, 79 MultihostTargetBasePaths: multihostTargetBasePaths, 80 } 81 82 return p, nil 83 } 84 85 func (p *Paths) AllModules() modules.Modules { 86 return p.Cfg.GetConfigSection("allModules").(modules.Modules) 87 } 88 89 // GetBasePath returns any path element in baseURL if needed. 90 // The path returned will have a leading, but no trailing slash. 91 func (p *Paths) GetBasePath(isRelativeURL bool) string { 92 if isRelativeURL && p.Cfg.CanonifyURLs() { 93 // The baseURL will be prepended later. 94 return "" 95 } 96 return p.Cfg.BaseURL().BasePathNoTrailingSlash 97 } 98 99 func (p *Paths) Lang() string { 100 if p == nil || p.Cfg.Language() == nil { 101 return "" 102 } 103 return p.Cfg.Language().Lang 104 } 105 106 func (p *Paths) GetTargetLanguageBasePath() string { 107 if p.Cfg.IsMultihost() { 108 // In a multihost configuration all assets will be published below the language code. 109 return p.Lang() 110 } 111 return p.GetLanguagePrefix() 112 } 113 114 func (p *Paths) GetLanguagePrefix() string { 115 return p.Cfg.LanguagePrefix() 116 } 117 118 // AbsPathify creates an absolute path if given a relative path. If already 119 // absolute, the path is just cleaned. 120 func (p *Paths) AbsPathify(inPath string) string { 121 return hpaths.AbsPathify(p.Cfg.BaseConfig().WorkingDir, inPath) 122 } 123 124 // RelPathify trims any WorkingDir prefix from the given filename. If 125 // the filename is not considered to be absolute, the path is just cleaned. 126 func (p *Paths) RelPathify(filename string) string { 127 filename = filepath.Clean(filename) 128 if !filepath.IsAbs(filename) { 129 return filename 130 } 131 132 return strings.TrimPrefix(strings.TrimPrefix(filename, p.Cfg.BaseConfig().WorkingDir), FilePathSeparator) 133 }