summaryrefslogtreecommitdiffstats
path: root/helpers/path.go
diff options
context:
space:
mode:
Diffstat (limited to 'helpers/path.go')
-rw-r--r--helpers/path.go105
1 files changed, 14 insertions, 91 deletions
diff --git a/helpers/path.go b/helpers/path.go
index 3172d3452..4a6c9a688 100644
--- a/helpers/path.go
+++ b/helpers/path.go
@@ -1,4 +1,4 @@
-// Copyright 2023 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -23,12 +23,12 @@ import (
"regexp"
"sort"
"strings"
- "unicode"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/text"
"github.com/gohugoio/hugo/htesting"
+ "github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/common/hugio"
@@ -41,7 +41,11 @@ import (
// whilst preserving the original casing of the string.
// E.g. Social Media -> Social-Media
func (p *PathSpec) MakePath(s string) string {
- return p.UnicodeSanitize(s)
+ s = paths.Sanitize(s)
+ if p.Cfg.RemovePathAccents() {
+ s = text.RemoveAccentsString(s)
+ }
+ return s
}
// MakePathsSanitized applies MakePathSanitized on every item in the slice
@@ -59,74 +63,13 @@ func (p *PathSpec) MakePathSanitized(s string) string {
return strings.ToLower(p.MakePath(s))
}
-// ToSlashTrimLeading is just a filepath.ToSlaas with an added / prefix trimmer.
-func ToSlashTrimLeading(s string) string {
- return strings.TrimPrefix(filepath.ToSlash(s), "/")
-}
-
// MakeTitle converts the path given to a suitable title, trimming whitespace
// and replacing hyphens with whitespace.
func MakeTitle(inpath string) string {
return strings.Replace(strings.TrimSpace(inpath), "-", " ", -1)
}
-// From https://golang.org/src/net/url/url.go
-func ishex(c rune) bool {
- switch {
- case '0' <= c && c <= '9':
- return true
- case 'a' <= c && c <= 'f':
- return true
- case 'A' <= c && c <= 'F':
- return true
- }
- return false
-}
-
-// UnicodeSanitize sanitizes string to be used in Hugo URL's, allowing only
-// a predefined set of special Unicode characters.
-// If RemovePathAccents configuration flag is enabled, Unicode accents
-// are also removed.
-// Hyphens in the original input are maintained.
-// Spaces will be replaced with a single hyphen, and sequential replacement hyphens will be reduced to one.
-func (p *PathSpec) UnicodeSanitize(s string) string {
- if p.Cfg.RemovePathAccents() {
- s = text.RemoveAccentsString(s)
- }
-
- source := []rune(s)
- target := make([]rune, 0, len(source))
- var (
- prependHyphen bool
- wasHyphen bool
- )
-
- for i, r := range source {
- isAllowed := r == '.' || r == '/' || r == '\\' || r == '_' || r == '#' || r == '+' || r == '~' || r == '-' || r == '@'
- isAllowed = isAllowed || unicode.IsLetter(r) || unicode.IsDigit(r) || unicode.IsMark(r)
- isAllowed = isAllowed || (r == '%' && i+2 < len(source) && ishex(source[i+1]) && ishex(source[i+2]))
-
- if isAllowed {
- // track explicit hyphen in input; no need to add a new hyphen if
- // we just saw one.
- wasHyphen = r == '-'
-
- if prependHyphen {
- // if currently have a hyphen, don't prepend an extra one
- if !wasHyphen {
- target = append(target, '-')
- }
- prependHyphen = false
- }
- target = append(target, r)
- } else if len(target) > 0 && !wasHyphen && unicode.IsSpace(r) {
- prependHyphen = true
- }
- }
-
- return string(target)
-}
-
+// MakeTitleInPath converts the path given to a suitable title, trimming whitespace
func MakePathRelative(inPath string, possibleDirectories ...string) (string, error) {
for _, currentPath := range possibleDirectories {
if strings.HasPrefix(inPath, currentPath) {
@@ -317,13 +260,12 @@ func FindCWD() (string, error) {
return path, nil
}
-// SymbolicWalk is like filepath.Walk, but it follows symbolic links.
-func SymbolicWalk(fs afero.Fs, root string, walker hugofs.WalkFunc) error {
+// Walk walks the file tree rooted at root, calling walkFn for each file or
+// directory in the tree, including root.
+func Walk(fs afero.Fs, root string, walker hugofs.WalkFunc) error {
if _, isOs := fs.(*afero.OsFs); isOs {
- // Mainly to track symlinks.
fs = hugofs.NewBaseFileDecorator(fs)
}
-
w := hugofs.NewWalkway(hugofs.WalkwayConfig{
Fs: fs,
Root: root,
@@ -333,16 +275,6 @@ func SymbolicWalk(fs afero.Fs, root string, walker hugofs.WalkFunc) error {
return w.Walk()
}
-// LstatIfPossible can be used to call Lstat if possible, else Stat.
-func LstatIfPossible(fs afero.Fs, path string) (os.FileInfo, error) {
- if lstater, ok := fs.(afero.Lstater); ok {
- fi, _, err := lstater.LstatIfPossible(path)
- return fi, err
- }
-
- return fs.Stat(path)
-}
-
// SafeWriteToDisk is the same as WriteToDisk
// but it also checks to see if file/directory already exists.
func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
@@ -382,7 +314,7 @@ func OpenFileForWriting(fs afero.Fs, filename string) (afero.File, error) {
if !herrors.IsNotExist(err) {
return nil, err
}
- if err = fs.MkdirAll(filepath.Dir(filename), 0777); err != nil { // before umask
+ if err = fs.MkdirAll(filepath.Dir(filename), 0o777); err != nil { // before umask
return nil, err
}
f, err = fs.Create(filename)
@@ -402,7 +334,7 @@ func GetCacheDir(fs afero.Fs, cacheDir string) (string, error) {
return "", err
}
if !exists {
- err := fs.MkdirAll(cacheDir, 0777) // Before umask
+ err := fs.MkdirAll(cacheDir, 0o777) // Before umask
if err != nil {
return "", fmt.Errorf("failed to create cache dir: %w", err)
}
@@ -417,7 +349,7 @@ func GetCacheDir(fs afero.Fs, cacheDir string) (string, error) {
userCacheDir, err := os.UserCacheDir()
if err == nil {
cacheDir := filepath.Join(userCacheDir, hugoCacheBase)
- if err := fs.Mkdir(cacheDir, 0777); err == nil || os.IsExist(err) {
+ if err := fs.Mkdir(cacheDir, 0o777); err == nil || os.IsExist(err) {
return cacheDir, nil
}
}
@@ -494,12 +426,3 @@ func IsEmpty(path string, fs afero.Fs) (bool, error) {
func Exists(path string, fs afero.Fs) (bool, error) {
return afero.Exists(fs, path)
}
-
-// AddTrailingSlash adds a trailing Unix styled slash (/) if not already
-// there.
-func AddTrailingSlash(path string) string {
- if !strings.HasSuffix(path, "/") {
- path += "/"
- }
- return path
-}