Debian Codegraph @

helpers/pathspec.go

raw
1 // Copyright 2016-present 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 helpers
15
16 import (
17 "strings"
18
19 "github.com/gohugoio/hugo/common/loggers"
20 "github.com/gohugoio/hugo/config"
21 "github.com/gohugoio/hugo/hugofs"
22 "github.com/gohugoio/hugo/hugolib/filesystems"
23 "github.com/gohugoio/hugo/hugolib/paths"
24 )
25
26 // PathSpec holds methods that decides how paths in URLs and files in Hugo should look like.
27 type PathSpec struct {
28 *paths.Paths
29 *filesystems.BaseFs
30
31 ProcessingStats *ProcessingStats
32
33 // The file systems to use
34 Fs *hugofs.Fs
35 }
36
37 // NewPathSpec creates a new PathSpec from the given filesystems and language.
38 // If an existing BaseFs is provided, parts of that is reused.
39 func NewPathSpec(fs *hugofs.Fs, cfg config.AllProvider, logger loggers.Logger, baseBaseFs *filesystems.BaseFs) (*PathSpec, error) {
40 p, err := paths.New(fs, cfg)
41 if err != nil {
42 return nil, err
43 }
44
45 var options []func(*filesystems.BaseFs) error
46 if baseBaseFs != nil {
47 options = []func(*filesystems.BaseFs) error{
48 filesystems.WithBaseFs(baseBaseFs),
49 }
50 }
51 bfs, err := filesystems.NewBase(p, logger, options...)
52 if err != nil {
53 return nil, err
54 }
55
56 ps := &PathSpec{
57 Paths: p,
58 BaseFs: bfs,
59 Fs: fs,
60 ProcessingStats: NewProcessingStats(p.Lang()),
61 }
62
63 return ps, nil
64 }
65
66 // PermalinkForBaseURL creates a permalink from the given link and baseURL.
67 func (p *PathSpec) PermalinkForBaseURL(link, baseURL string) string {
68 return baseURL + strings.TrimPrefix(link, "/")
69 }