| 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/gohugoio/hugo/common/paths" |
| 21 |
"github.com/gohugoio/hugo/common/urls" |
| 22 |
"github.com/gohugoio/hugo/langs" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/config" |
| 25 |
"github.com/gohugoio/hugo/modules" |
| 26 |
|
| 27 |
"github.com/gohugoio/hugo/hugofs" |
| 28 |
) |
| 29 |
|
| 30 |
var FilePathSeparator = string(filepath.Separator) |
| 31 |
|
| 32 |
type Paths struct { |
| 33 |
Fs *hugofs.Fs |
| 34 |
Cfg config.AllProvider |
| 35 |
|
| 36 |
// Directories to store Resource related artifacts. |
| 37 |
AbsResourcesDir string |
| 38 |
|
| 39 |
AbsPublishDir string |
| 40 |
|
| 41 |
// When in multihost mode, this returns a list of base paths below PublishDir |
| 42 |
// for each language. |
| 43 |
MultihostTargetBasePaths []string |
| 44 |
|
| 45 |
// When in multihost mode, this is the URL base path (the path part of baseURL, |
| 46 |
// without trailing slash) for each language, parallel to MultihostTargetBasePaths. |
| 47 |
MultihostBasePaths []string |
| 48 |
|
| 49 |
// When in multihost mode, this is the deepest of the per-host base paths |
| 50 |
// in MultihostBasePaths. css.Build uses this so file-loader URLs are |
| 51 |
// reachable on every host (each host's base path is a prefix). |
| 52 |
MultihostLongestBasePath string |
| 53 |
} |
| 54 |
|
| 55 |
// allBaseURLsProvider is satisfied by allconfig.ConfigLanguage and lets us |
| 56 |
// access per-host BaseURLs without importing allconfig (which would create an |
| 57 |
// import cycle via helpers). |
| 58 |
type allBaseURLsProvider interface { |
| 59 |
AllBaseURLs() []urls.BaseURL |
| 60 |
} |
| 61 |
|
| 62 |
func New(fs *hugofs.Fs, cfg config.AllProvider) (*Paths, error) { |
| 63 |
bcfg := cfg.BaseConfig() |
| 64 |
publishDir := bcfg.PublishDir |
| 65 |
if publishDir == "" { |
| 66 |
panic("publishDir not set") |
| 67 |
} |
| 68 |
|
| 69 |
absPublishDir := hpaths.AbsPathify(bcfg.WorkingDir, publishDir) |
| 70 |
if !strings.HasSuffix(absPublishDir, FilePathSeparator) { |
| 71 |
absPublishDir += FilePathSeparator |
| 72 |
} |
| 73 |
// If root, remove the second '/' |
| 74 |
if absPublishDir == "//" { |
| 75 |
absPublishDir = FilePathSeparator |
| 76 |
} |
| 77 |
absResourcesDir := hpaths.AbsPathify(bcfg.WorkingDir, cfg.Dirs().ResourceDir) |
| 78 |
if !strings.HasSuffix(absResourcesDir, FilePathSeparator) { |
| 79 |
absResourcesDir += FilePathSeparator |
| 80 |
} |
| 81 |
if absResourcesDir == "//" { |
| 82 |
absResourcesDir = FilePathSeparator |
| 83 |
} |
| 84 |
|
| 85 |
var ( |
| 86 |
multihostTargetBasePaths []string |
| 87 |
multihostBasePaths []string |
| 88 |
multihostLongestBasePath string |
| 89 |
) |
| 90 |
if cfg.IsMultihost() && len(cfg.Languages().(langs.Languages)) > 1 { |
| 91 |
baseURLs := cfg.(allBaseURLsProvider).AllBaseURLs() |
| 92 |
for i, l := range cfg.Languages().(langs.Languages) { |
| 93 |
multihostTargetBasePaths = append(multihostTargetBasePaths, hpaths.ToSlashPreserveLeading(l.Lang)) |
| 94 |
bp := baseURLs[i].BasePathNoTrailingSlash |
| 95 |
multihostBasePaths = append(multihostBasePaths, bp) |
| 96 |
if len(bp) > len(multihostLongestBasePath) { |
| 97 |
multihostLongestBasePath = bp |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
p := &Paths{ |
| 103 |
Fs: fs, |
| 104 |
Cfg: cfg, |
| 105 |
AbsResourcesDir: absResourcesDir, |
| 106 |
AbsPublishDir: absPublishDir, |
| 107 |
MultihostTargetBasePaths: multihostTargetBasePaths, |
| 108 |
MultihostBasePaths: multihostBasePaths, |
| 109 |
MultihostLongestBasePath: multihostLongestBasePath, |
| 110 |
} |
| 111 |
|
| 112 |
return p, nil |
| 113 |
} |
| 114 |
|
| 115 |
func (p *Paths) AllModules() modules.Modules { |
| 116 |
return p.Cfg.GetConfigSection("allModules").(modules.Modules) |
| 117 |
} |
| 118 |
|
| 119 |
// GetBasePath returns any path element in baseURL if needed. |
| 120 |
// The path returned will have a leading, but no trailing slash. |
| 121 |
func (p *Paths) GetBasePath(isRelativeURL bool) string { |
| 122 |
if isRelativeURL && p.Cfg.CanonifyURLs() { |
| 123 |
// The baseURL will be prepended later. |
| 124 |
return "" |
| 125 |
} |
| 126 |
return p.Cfg.BaseURL().BasePathNoTrailingSlash |
| 127 |
} |
| 128 |
|
| 129 |
func (p *Paths) Lang() string { |
| 130 |
if p == nil || p.Cfg.Language() == nil { |
| 131 |
return "" |
| 132 |
} |
| 133 |
return p.Cfg.Language().(*langs.Language).Lang |
| 134 |
} |
| 135 |
|
| 136 |
func (p *Paths) GetTargetLanguageBasePath() string { |
| 137 |
if p.Cfg.IsMultihost() { |
| 138 |
// In a multihost configuration all assets will be published below the language code. |
| 139 |
return p.Lang() |
| 140 |
} |
| 141 |
return p.GetLanguagePrefix() |
| 142 |
} |
| 143 |
|
| 144 |
func (p *Paths) GetLanguagePrefix() string { |
| 145 |
return p.Cfg.LanguagePrefix() |
| 146 |
} |
| 147 |
|
| 148 |
// AbsPathify creates an absolute path if given a relative path. If already |
| 149 |
// absolute, the path is just cleaned. |
| 150 |
func (p *Paths) AbsPathify(inPath string) string { |
| 151 |
return hpaths.AbsPathify(p.Cfg.BaseConfig().WorkingDir, inPath) |
| 152 |
} |
| 153 |
|
| 154 |
// RelPathify trims any WorkingDir prefix from the given filename. If |
| 155 |
// the filename is not considered to be absolute, the path is just cleaned. |
| 156 |
func (p *Paths) RelPathify(filename string) string { |
| 157 |
filename = filepath.Clean(filename) |
| 158 |
if !filepath.IsAbs(filename) { |
| 159 |
return filename |
| 160 |
} |
| 161 |
|
| 162 |
return strings.TrimPrefix(strings.TrimPrefix(filename, p.Cfg.BaseConfig().WorkingDir), FilePathSeparator) |
| 163 |
} |