github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/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/gohugoio/hugo/common/paths"
    21  
    22  	"github.com/gohugoio/hugo/config"
    23  	"github.com/gohugoio/hugo/modules"
    24  
    25  	"github.com/gohugoio/hugo/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  func (p *Paths) GetBasePath(isRelativeURL bool) string {
    91  	if isRelativeURL && p.Cfg.CanonifyURLs() {
    92  		// The baseURL will be prepended later.
    93  		return ""
    94  	}
    95  	return p.Cfg.BaseURL().BasePath
    96  }
    97  
    98  func (p *Paths) Lang() string {
    99  	if p == nil || p.Cfg.Language() == nil {
   100  		return ""
   101  	}
   102  	return p.Cfg.Language().Lang
   103  }
   104  
   105  func (p *Paths) GetTargetLanguageBasePath() string {
   106  	if p.Cfg.IsMultihost() {
   107  		// In a multihost configuration all assets will be published below the language code.
   108  		return p.Lang()
   109  	}
   110  	return p.GetLanguagePrefix()
   111  }
   112  
   113  func (p *Paths) GetLanguagePrefix() string {
   114  	return p.Cfg.LanguagePrefix()
   115  }
   116  
   117  // AbsPathify creates an absolute path if given a relative path. If already
   118  // absolute, the path is just cleaned.
   119  func (p *Paths) AbsPathify(inPath string) string {
   120  	return hpaths.AbsPathify(p.Cfg.BaseConfig().WorkingDir, inPath)
   121  }
   122  
   123  // RelPathify trims any WorkingDir prefix from the given filename. If
   124  // the filename is not considered to be absolute, the path is just cleaned.
   125  func (p *Paths) RelPathify(filename string) string {
   126  	filename = filepath.Clean(filename)
   127  	if !filepath.IsAbs(filename) {
   128  		return filename
   129  	}
   130  
   131  	return strings.TrimPrefix(strings.TrimPrefix(filename, p.Cfg.BaseConfig().WorkingDir), FilePathSeparator)
   132  }