| 1 |
// Copyright 2019 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 |
"fmt" |
| 18 |
"io" |
| 19 |
"net" |
| 20 |
"os" |
| 21 |
"path/filepath" |
| 22 |
"slices" |
| 23 |
"strings" |
| 24 |
"unicode" |
| 25 |
"unicode/utf8" |
| 26 |
|
| 27 |
bp "github.com/gohugoio/hugo/bufferpool" |
| 28 |
|
| 29 |
"github.com/spf13/afero" |
| 30 |
|
| 31 |
"github.com/jdkato/prose/transform" |
| 32 |
) |
| 33 |
|
| 34 |
// FilePathSeparator as defined by os.Separator. |
| 35 |
const FilePathSeparator = string(filepath.Separator) |
| 36 |
|
| 37 |
// TCPListen starts listening on a valid TCP port. |
| 38 |
func TCPListen() (net.Listener, *net.TCPAddr, error) { |
| 39 |
l, err := net.Listen("tcp", ":0") |
| 40 |
if err != nil { |
| 41 |
return nil, nil, err |
| 42 |
} |
| 43 |
addr := l.Addr() |
| 44 |
if a, ok := addr.(*net.TCPAddr); ok { |
| 45 |
return l, a, nil |
| 46 |
} |
| 47 |
l.Close() |
| 48 |
return nil, nil, fmt.Errorf("unable to obtain a valid tcp port: %v", addr) |
| 49 |
} |
| 50 |
|
| 51 |
// FirstUpper returns a string with the first character as upper case. |
| 52 |
func FirstUpper(s string) string { |
| 53 |
if s == "" { |
| 54 |
return "" |
| 55 |
} |
| 56 |
r, n := utf8.DecodeRuneInString(s) |
| 57 |
return string(unicode.ToUpper(r)) + s[n:] |
| 58 |
} |
| 59 |
|
| 60 |
// ReaderToBytes takes an io.Reader argument, reads from it |
| 61 |
// and returns bytes. |
| 62 |
func ReaderToBytes(lines io.Reader) []byte { |
| 63 |
if lines == nil { |
| 64 |
return []byte{} |
| 65 |
} |
| 66 |
b := bp.GetBuffer() |
| 67 |
defer bp.PutBuffer(b) |
| 68 |
|
| 69 |
b.ReadFrom(lines) |
| 70 |
|
| 71 |
bc := make([]byte, b.Len()) |
| 72 |
copy(bc, b.Bytes()) |
| 73 |
return bc |
| 74 |
} |
| 75 |
|
| 76 |
// ReaderToString is the same as ReaderToBytes, but returns a string. |
| 77 |
func ReaderToString(lines io.Reader) string { |
| 78 |
if lines == nil { |
| 79 |
return "" |
| 80 |
} |
| 81 |
b := bp.GetBuffer() |
| 82 |
defer bp.PutBuffer(b) |
| 83 |
b.ReadFrom(lines) |
| 84 |
return b.String() |
| 85 |
} |
| 86 |
|
| 87 |
// GetTitleFunc returns a func that can be used to transform a string to |
| 88 |
// title case. |
| 89 |
// |
| 90 |
// # The supported styles are |
| 91 |
// |
| 92 |
// - "Go" (strings.Title) |
| 93 |
// - "AP" (see https://www.apstylebook.com/) |
| 94 |
// - "Chicago" (see https://www.chicagomanualofstyle.org/home.html) |
| 95 |
// - "FirstUpper" (only the first character is upper case) |
| 96 |
// - "None" (no transformation) |
| 97 |
// |
| 98 |
// If an unknown or empty style is provided, AP style is what you get. |
| 99 |
func GetTitleFunc(style string) func(s string) string { |
| 100 |
switch strings.ToLower(style) { |
| 101 |
case "go": |
| 102 |
//lint:ignore SA1019 keep for now. |
| 103 |
return strings.Title |
| 104 |
case "chicago": |
| 105 |
tc := transform.NewTitleConverter(transform.ChicagoStyle) |
| 106 |
return tc.Title |
| 107 |
case "none": |
| 108 |
return func(s string) string { return s } |
| 109 |
case "firstupper": |
| 110 |
return FirstUpper |
| 111 |
default: |
| 112 |
tc := transform.NewTitleConverter(transform.APStyle) |
| 113 |
return tc.Title |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// HasStringsPrefix tests whether the string slice s begins with prefix slice s. |
| 118 |
func HasStringsPrefix(s, prefix []string) bool { |
| 119 |
return len(s) >= len(prefix) && compareStringSlices(s[0:len(prefix)], prefix) |
| 120 |
} |
| 121 |
|
| 122 |
// HasStringsSuffix tests whether the string slice s ends with suffix slice s. |
| 123 |
func HasStringsSuffix(s, suffix []string) bool { |
| 124 |
return len(s) >= len(suffix) && compareStringSlices(s[len(s)-len(suffix):], suffix) |
| 125 |
} |
| 126 |
|
| 127 |
func compareStringSlices(a, b []string) bool { |
| 128 |
if a == nil && b == nil { |
| 129 |
return true |
| 130 |
} |
| 131 |
|
| 132 |
if a == nil || b == nil { |
| 133 |
return false |
| 134 |
} |
| 135 |
|
| 136 |
return slices.Equal(a, b) |
| 137 |
} |
| 138 |
|
| 139 |
// SliceToLower goes through the source slice and lowers all values. |
| 140 |
func SliceToLower(s []string) []string { |
| 141 |
if s == nil { |
| 142 |
return nil |
| 143 |
} |
| 144 |
|
| 145 |
l := make([]string, len(s)) |
| 146 |
for i, v := range s { |
| 147 |
l[i] = strings.ToLower(v) |
| 148 |
} |
| 149 |
|
| 150 |
return l |
| 151 |
} |
| 152 |
|
| 153 |
// StringSliceToList formats a string slice into a human-readable list. |
| 154 |
// It joins the elements of the slice s with commas, using an Oxford comma, |
| 155 |
// and precedes the final element with the conjunction c. |
| 156 |
func StringSliceToList(s []string, c string) string { |
| 157 |
const defaultConjunction = "and" |
| 158 |
|
| 159 |
if c == "" { |
| 160 |
c = defaultConjunction |
| 161 |
} |
| 162 |
if len(s) == 0 { |
| 163 |
return "" |
| 164 |
} |
| 165 |
if len(s) == 1 { |
| 166 |
return s[0] |
| 167 |
} |
| 168 |
if len(s) == 2 { |
| 169 |
return fmt.Sprintf("%s %s %s", s[0], c, s[1]) |
| 170 |
} |
| 171 |
return fmt.Sprintf("%s, %s %s", strings.Join(s[:len(s)-1], ", "), c, s[len(s)-1]) |
| 172 |
} |
| 173 |
|
| 174 |
// IsWhitespace determines if the given rune is whitespace. |
| 175 |
func IsWhitespace(r rune) bool { |
| 176 |
return r == ' ' || r == '\t' || r == '\n' || r == '\r' |
| 177 |
} |
| 178 |
|
| 179 |
// PrintFs prints the given filesystem to the given writer starting from the given path. |
| 180 |
// This is useful for debugging. |
| 181 |
func PrintFs(fs afero.Fs, path string, w io.Writer) { |
| 182 |
if fs == nil { |
| 183 |
return |
| 184 |
} |
| 185 |
|
| 186 |
afero.Walk(fs, path, func(path string, info os.FileInfo, err error) error { |
| 187 |
if err != nil { |
| 188 |
panic(fmt.Sprintf("error: path %q: %s", path, err)) |
| 189 |
} |
| 190 |
path = filepath.ToSlash(path) |
| 191 |
if path == "" { |
| 192 |
path = "." |
| 193 |
} |
| 194 |
fmt.Fprintln(w, path, info.IsDir()) |
| 195 |
return nil |
| 196 |
}) |
| 197 |
} |
| 198 |
|
| 199 |
// FormatByteCount pretty formats b. |
| 200 |
func FormatByteCount(bc uint64) string { |
| 201 |
const ( |
| 202 |
Gigabyte = 1 << 30 |
| 203 |
Megabyte = 1 << 20 |
| 204 |
Kilobyte = 1 << 10 |
| 205 |
) |
| 206 |
switch { |
| 207 |
case bc > Gigabyte || -bc > Gigabyte: |
| 208 |
return fmt.Sprintf("%.2f GB", float64(bc)/Gigabyte) |
| 209 |
case bc > Megabyte || -bc > Megabyte: |
| 210 |
return fmt.Sprintf("%.2f MB", float64(bc)/Megabyte) |
| 211 |
case bc > Kilobyte || -bc > Kilobyte: |
| 212 |
return fmt.Sprintf("%.2f KB", float64(bc)/Kilobyte) |
| 213 |
} |
| 214 |
return fmt.Sprintf("%d B", bc) |
| 215 |
} |