| 1 |
// Copyright 2024 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 internal |
| 15 |
|
| 16 |
import ( |
| 17 |
"path" |
| 18 |
"path/filepath" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"github.com/gohugoio/hugo/common/paths" |
| 22 |
) |
| 23 |
|
| 24 |
// ResourcePaths holds path information for a resource. |
| 25 |
// All directories in here have Unix-style slashes, with leading slash, but no trailing slash. |
| 26 |
// Empty directories are represented with an empty string. |
| 27 |
type ResourcePaths struct { |
| 28 |
// This is the directory component for the target file or link. |
| 29 |
Dir string |
| 30 |
|
| 31 |
// Any base directory for the target file. Will be prepended to Dir. |
| 32 |
BaseDirTarget string |
| 33 |
|
| 34 |
// This is the directory component for the link will be prepended to Dir. |
| 35 |
BaseDirLink string |
| 36 |
|
| 37 |
// Set when publishing in a multihost setup. |
| 38 |
TargetBasePaths []string |
| 39 |
|
| 40 |
// This is the File component, e.g. "data.json". |
| 41 |
File string |
| 42 |
} |
| 43 |
|
| 44 |
func (d ResourcePaths) join(p ...string) string { |
| 45 |
var s string |
| 46 |
for i, pp := range p { |
| 47 |
if pp == "" { |
| 48 |
continue |
| 49 |
} |
| 50 |
if i > 0 && !strings.HasPrefix(pp, "/") { |
| 51 |
pp = "/" + pp |
| 52 |
} |
| 53 |
s += pp |
| 54 |
|
| 55 |
} |
| 56 |
if !strings.HasPrefix(s, "/") { |
| 57 |
s = "/" + s |
| 58 |
} |
| 59 |
return s |
| 60 |
} |
| 61 |
|
| 62 |
func (d ResourcePaths) TargetLink() string { |
| 63 |
return d.join(d.BaseDirLink, d.Dir, d.File) |
| 64 |
} |
| 65 |
|
| 66 |
func (d ResourcePaths) TargetPath() string { |
| 67 |
return d.join(d.BaseDirTarget, d.Dir, d.File) |
| 68 |
} |
| 69 |
|
| 70 |
func (d ResourcePaths) Path() string { |
| 71 |
return d.join(d.Dir, d.File) |
| 72 |
} |
| 73 |
|
| 74 |
func (d ResourcePaths) TargetPaths() []string { |
| 75 |
if len(d.TargetBasePaths) == 0 { |
| 76 |
return []string{d.TargetPath()} |
| 77 |
} |
| 78 |
|
| 79 |
var paths []string |
| 80 |
for _, p := range d.TargetBasePaths { |
| 81 |
paths = append(paths, p+d.TargetPath()) |
| 82 |
} |
| 83 |
return paths |
| 84 |
} |
| 85 |
|
| 86 |
func (d ResourcePaths) TargetFilenames() []string { |
| 87 |
filenames := d.TargetPaths() |
| 88 |
for i, p := range filenames { |
| 89 |
filenames[i] = filepath.FromSlash(p) |
| 90 |
} |
| 91 |
return filenames |
| 92 |
} |
| 93 |
|
| 94 |
func (d ResourcePaths) FromTargetPath(targetPath string) ResourcePaths { |
| 95 |
targetPath = filepath.ToSlash(targetPath) |
| 96 |
dir, file := path.Split(targetPath) |
| 97 |
dir = paths.ToSlashPreserveLeading(dir) |
| 98 |
if dir == "/" { |
| 99 |
dir = "" |
| 100 |
} |
| 101 |
d.Dir = dir |
| 102 |
d.File = file |
| 103 |
d.BaseDirLink = "" |
| 104 |
d.BaseDirTarget = "" |
| 105 |
|
| 106 |
return d |
| 107 |
} |