helpers

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const FilePathSeparator = string(filepath.Separator)

FilePathSeparator as defined by os.Separator.

Variables

This section is empty.

Functions

func AddTrailingFileSeparator

func AddTrailingFileSeparator(s string) string

func BytesToHTML

func BytesToHTML(b []byte) template.HTML

BytesToHTML converts bytes to type template.HTML.

func DirExists

func DirExists(path string, fs afero.Fs) (bool, error)

DirExists checks if a path exists and is a directory.

func Emojify

func Emojify(source []byte) []byte

Emojify "emojifies" the input source. Note that the input byte slice will be modified if needed. See http://www.emoji-cheat-sheet.com/

func Exists

func Exists(path string, fs afero.Fs) (bool, error)

Exists checks if a file or directory exists.

func ExtractRootPaths

func ExtractRootPaths(paths []string) []string

ExtractRootPaths extracts the root paths from the supplied list of paths. The resulting root path will not contain any file separators, but there may be duplicates. So "/content/section/" becomes "content"

func FindCWD

func FindCWD() (string, error)

FindCWD returns the current working directory from where the Hugo executable is run.

func FirstUpper

func FirstUpper(s string) string

FirstUpper returns a string with the first character as upper case.

func FormatByteCount

func FormatByteCount(bc uint64) string

FormatByteCount pretty formats b.

func GetDottedRelativePath

func GetDottedRelativePath(inPath string) string

GetDottedRelativePath expects a relative path starting after the content directory. It returns a relative path with dots ("..") navigating up the path structure.

func GetTempDir

func GetTempDir(subPath string, fs afero.Fs) string

GetTempDir returns a temporary directory with the given sub path.

func GetTitleFunc

func GetTitleFunc(style string) func(s string) string

GetTitleFunc returns a func that can be used to transform a string to title case.

The supported styles are

- "Go" (strings.Title) - "AP" (see https://www.apstylebook.com/) - "Chicago" (see https://www.chicagomanualofstyle.org/home.html) - "FirstUpper" (only the first character is upper case) - "None" (no transformation)

If an unknown or empty style is provided, AP style is what you get.

func HasStringsPrefix

func HasStringsPrefix(s, prefix []string) bool

HasStringsPrefix tests whether the string slice s begins with prefix slice s.

func HasStringsSuffix

func HasStringsSuffix(s, suffix []string) bool

HasStringsSuffix tests whether the string slice s ends with suffix slice s.

func IsDir

func IsDir(path string, fs afero.Fs) (bool, error)

IsDir checks if a given path is a directory.

func IsEmpty

func IsEmpty(path string, fs afero.Fs) (bool, error)

IsEmpty checks if a given path is empty, meaning it doesn't contain any regular files.

func IsWhitespace

func IsWhitespace(r rune) bool

IsWhitespace determines if the given rune is whitespace.

func MD5FromReader

func MD5FromReader(r io.Reader) (string, error)

MD5FromReader creates a MD5 hash from the given reader.

func MD5FromReaderFast

func MD5FromReaderFast(r io.ReadSeeker) (string, int64, error)

MD5FromReaderFast creates a MD5 hash from the given file. It only reads parts of the file for speed, so don't use it if the files are very subtly different. It will not close the file. It will return the MD5 hash and the size of r in bytes.

func MD5String

func MD5String(f string) string

MD5String takes a string and returns its MD5 hash.

func MakePathRelative

func MakePathRelative(inPath string, possibleDirectories ...string) (string, error)

MakeTitleInPath converts the path given to a suitable title, trimming whitespace

func MakeTitle

func MakeTitle(inpath string) string

MakeTitle converts the path given to a suitable title, trimming whitespace and replacing hyphens with whitespace.

func OpenFileForWriting

func OpenFileForWriting(fs afero.Fs, filename string) (afero.File, error)

OpenFileForWriting opens or creates the given file. If the target directory does not exist, it gets created.

func OpenFilesForWriting

func OpenFilesForWriting(fs afero.Fs, filenames ...string) (io.WriteCloser, error)

OpenFilesForWriting opens all the given filenames for writing.

func PrintFs

func PrintFs(fs afero.Fs, path string, w io.Writer)

PrintFs prints the given filesystem to the given writer starting from the given path. This is useful for debugging.

func ReaderContains

func ReaderContains(r io.Reader, subslice []byte) bool

ReaderContains reports whether subslice is within r.

func ReaderToBytes

func ReaderToBytes(lines io.Reader) []byte

ReaderToBytes takes an io.Reader argument, reads from it and returns bytes.

func ReaderToString

func ReaderToString(lines io.Reader) string

ReaderToString is the same as ReaderToBytes, but returns a string.

func SafeWriteToDisk

func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error)

SafeWriteToDisk is the same as WriteToDisk but it also checks to see if file/directory already exists.

func SliceToLower

func SliceToLower(s []string) []string

SliceToLower goes through the source slice and lowers all values.

func StripHTML

func StripHTML(s string) string

StripHTML strips out all HTML tags in s.

func TCPListen

func TCPListen() (net.Listener, *net.TCPAddr, error)

TCPListen starts listening on a valid TCP port.

func UniqueStrings

func UniqueStrings(s []string) []string

UniqueStrings returns a new slice with any duplicates removed.

func UniqueStringsReuse

func UniqueStringsReuse(s []string) []string

UniqueStringsReuse returns a slice with any duplicates removed. It will modify the input slice.

func UniqueStringsSorted

func UniqueStringsSorted(s []string) []string

UniqueStringsSorted returns a sorted slice with any duplicates removed. It will modify the input slice.

func WriteToDisk

func WriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error)

WriteToDisk writes content to disk.

Types

type NamedSlice

type NamedSlice struct {
	Name  string
	Slice []string
}

func ExtractAndGroupRootPaths

func ExtractAndGroupRootPaths(paths []string) []NamedSlice

func (NamedSlice) String

func (n NamedSlice) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL