package helpers
import "github.com/gohugoio/hugo/helpers"
Package helpers implements general utility functions that work with and on content. The helper functions defined here lay down the foundation of how Hugo works with files and filepaths, and perform string operations on content.
Index ¶
- Constants
- func BytesToHTML(b []byte) template.HTML
- func DirExists(path string, fs afero.Fs) (bool, error)
- func Emojify(source []byte) []byte
- func Exists(path string, fs afero.Fs) (bool, error)
- func ExtractRootPaths(paths []string) []string
- func ExtractTOC(content []byte) (newcontent []byte, toc []byte)
- func FindCWD() (string, error)
- func FirstUpper(s string) string
- func FormatByteCount(bc uint64) string
- func GetCacheDir(fs afero.Fs, cacheDir string) (string, error)
- func GetDottedRelativePath(inPath string) string
- func GetTempDir(subPath string, fs afero.Fs) string
- func GetTitleFunc(style string) func(s string) string
- func HasStringsPrefix(s, prefix []string) bool
- func HasStringsSuffix(s, suffix []string) bool
- func IsDir(path string, fs afero.Fs) (bool, error)
- func IsEmpty(path string, fs afero.Fs) (bool, error)
- func IsWhitespace(r rune) bool
- func MakePathRelative(inPath string, possibleDirectories ...string) (string, error)
- func MakeTitle(inpath string) string
- func OpenFileForWriting(fs afero.Fs, filename string) (afero.File, error)
- func OpenFilesForWriting(fs afero.Fs, filenames ...string) (io.WriteCloser, error)
- func PrintFs(fs afero.Fs, path string, w io.Writer)
- func ProcessingStatsTable(w io.Writer, stats ...*ProcessingStats)
- func ReaderContains(r io.Reader, subslice []byte) bool
- func ReaderToBytes(lines io.Reader) []byte
- func ReaderToString(lines io.Reader) string
- func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error)
- func SliceToLower(s []string) []string
- func TCPListen() (net.Listener, *net.TCPAddr, error)
- func TotalWords(s string) int
- func UniqueStrings(s []string) []string
- func UniqueStringsReuse(s []string) []string
- func UniqueStringsSorted(s []string) []string
- func Walk(fs afero.Fs, root string, walker hugofs.WalkFunc) error
- func WriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error)
- type ContentSpec
- func NewContentSpec(cfg config.AllProvider, logger loggers.Logger, contentFs afero.Fs, ex *hexec.Exec) (*ContentSpec, error)
- func (c *ContentSpec) ResolveMarkup(in string) string
- func (c *ContentSpec) SanitizeAnchorName(s string) string
- func (c *ContentSpec) TrimShortHTML(input []byte, markup string) []byte
- type NamedSlice
- type PathSpec
- func NewPathSpec(fs *hugofs.Fs, cfg config.AllProvider, logger loggers.Logger) (*PathSpec, error)
- func NewPathSpecWithBaseBaseFsProvided(fs *hugofs.Fs, cfg config.AllProvider, logger loggers.Logger, baseBaseFs *filesystems.BaseFs) (*PathSpec, error)
- func (p *PathSpec) AbsURL(in string, addLanguage bool) string
- func (p *PathSpec) IsAbsURL(in string) (bool, error)
- func (p *PathSpec) MakePath(s string) string
- func (p *PathSpec) MakePathSanitized(s string) string
- func (p *PathSpec) MakePathsSanitized(paths []string)
- func (p *PathSpec) PermalinkForBaseURL(link, baseURL string) string
- func (p *PathSpec) PrependBasePath(rel string, isAbs bool) string
- func (p *PathSpec) RelURL(in string, addLanguage bool) string
- func (p *PathSpec) URLEscape(uri string) string
- func (p *PathSpec) URLize(uri string) string
- func (p *PathSpec) URLizeFilename(filename string) string
- type ProcessingStats
Constants ¶
FilePathSeparator as defined by os.Separator.
Functions ¶
func BytesToHTML ¶
BytesToHTML converts bytes to type template.HTML.
func DirExists ¶
DirExists checks if a path exists and is a directory.
func Emojify ¶
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 ¶
Exists checks if a file or directory exists.
func ExtractRootPaths ¶
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 ExtractTOC ¶
ExtractTOC extracts Table of Contents from content.
func FindCWD ¶
FindCWD returns the current working directory from where the Hugo executable is run.
func FirstUpper ¶
FirstUpper returns a string with the first character as upper case.
func FormatByteCount ¶
FormatByteCount pretty formats b.
func GetCacheDir ¶
GetCacheDir returns a cache dir from the given filesystem and config. The dir will be created if it does not exist.
func GetDottedRelativePath ¶
GetDottedRelativePath expects a relative path starting after the content directory. It returns a relative path with dots ("..") navigating up the path structure.
func GetTempDir ¶
GetTempDir returns a temporary directory with the given sub path.
func GetTitleFunc ¶
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 ¶
HasStringsPrefix tests whether the string slice s begins with prefix slice s.
func HasStringsSuffix ¶
HasStringsSuffix tests whether the string slice s ends with suffix slice s.
func IsDir ¶
IsDir checks if a given path is a directory.
func IsEmpty ¶
IsEmpty checks if a given path is empty, meaning it doesn't contain any regular files.
func IsWhitespace ¶
IsWhitespace determines if the given rune is whitespace.
func MakePathRelative ¶
MakeTitleInPath converts the path given to a suitable title, trimming whitespace
func MakeTitle ¶
MakeTitle converts the path given to a suitable title, trimming whitespace and replacing hyphens with whitespace.
func OpenFileForWriting ¶
OpenFileForWriting opens or creates the given file. If the target directory does not exist, it gets created.
func OpenFilesForWriting ¶
OpenFilesForWriting opens all the given filenames for writing.
func PrintFs ¶
PrintFs prints the given filesystem to the given writer starting from the given path. This is useful for debugging.
func ProcessingStatsTable ¶
func ProcessingStatsTable(w io.Writer, stats ...*ProcessingStats)
ProcessingStatsTable writes a table-formatted representation of stats to w.
func ReaderContains ¶
ReaderContains reports whether subslice is within r.
func ReaderToBytes ¶
ReaderToBytes takes an io.Reader argument, reads from it and returns bytes.
func ReaderToString ¶
ReaderToString is the same as ReaderToBytes, but returns a string.
func SafeWriteToDisk ¶
SafeWriteToDisk is the same as WriteToDisk but it also checks to see if file/directory already exists.
func SliceToLower ¶
SliceToLower goes through the source slice and lowers all values.
func TCPListen ¶
TCPListen starts listening on a valid TCP port.
func TotalWords ¶
TotalWords counts instance of one or more consecutive white space characters, as defined by unicode.IsSpace, in s. This is a cheaper way of word counting than the obvious len(strings.Fields(s)).
func UniqueStrings ¶
UniqueStrings returns a new slice with any duplicates removed.
func UniqueStringsReuse ¶
UniqueStringsReuse returns a slice with any duplicates removed. It will modify the input slice.
func UniqueStringsSorted ¶
UniqueStringsSorted returns a sorted slice with any duplicates removed. It will modify the input slice.
func Walk ¶
Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root.
func WriteToDisk ¶
WriteToDisk writes content to disk.
Types ¶
type ContentSpec ¶
type ContentSpec struct { Converters markup.ConverterProvider Cfg config.AllProvider // contains filtered or unexported fields }
ContentSpec provides functionality to render markdown content.
func NewContentSpec ¶
func NewContentSpec(cfg config.AllProvider, logger loggers.Logger, contentFs afero.Fs, ex *hexec.Exec) (*ContentSpec, error)
NewContentSpec returns a ContentSpec initialized with the appropriate fields from the given config.Provider.
func (*ContentSpec) ResolveMarkup ¶
func (c *ContentSpec) ResolveMarkup(in string) string
func (*ContentSpec) SanitizeAnchorName ¶
func (c *ContentSpec) SanitizeAnchorName(s string) string
func (*ContentSpec) TrimShortHTML ¶
func (c *ContentSpec) TrimShortHTML(input []byte, markup string) []byte
TrimShortHTML removes the outer tags from HTML input where (a) the opening tag is present only once with the input, and (b) the opening and closing tags wrap the input after white space removal.
type NamedSlice ¶
func ExtractAndGroupRootPaths ¶
func ExtractAndGroupRootPaths(paths []string) []NamedSlice
func (NamedSlice) String ¶
func (n NamedSlice) String() string
type PathSpec ¶
type PathSpec struct { *paths.Paths *filesystems.BaseFs ProcessingStats *ProcessingStats // The file systems to use Fs *hugofs.Fs // The config provider to use Cfg config.AllProvider }
PathSpec holds methods that decides how paths in URLs and files in Hugo should look like.
func NewPathSpec ¶
NewPathSpec creates a new PathSpec from the given filesystems and language.
func NewPathSpecWithBaseBaseFsProvided ¶
func NewPathSpecWithBaseBaseFsProvided(fs *hugofs.Fs, cfg config.AllProvider, logger loggers.Logger, baseBaseFs *filesystems.BaseFs) (*PathSpec, error)
NewPathSpecWithBaseBaseFsProvided creates a new PathSpec from the given filesystems and language. If an existing BaseFs is provided, parts of that is reused.
func (*PathSpec) AbsURL ¶
AbsURL creates an absolute URL from the relative path given and the BaseURL set in config.
func (*PathSpec) IsAbsURL ¶
func (*PathSpec) MakePath ¶
MakePath takes a string with any characters and replace it so the string could be used in a path. It does so by creating a Unicode-sanitized string, with the spaces replaced, whilst preserving the original casing of the string. E.g. Social Media -> Social-Media
func (*PathSpec) MakePathSanitized ¶
MakePathSanitized creates a Unicode-sanitized string, with the spaces replaced
func (*PathSpec) MakePathsSanitized ¶
MakePathsSanitized applies MakePathSanitized on every item in the slice
func (*PathSpec) PermalinkForBaseURL ¶
PermalinkForBaseURL creates a permalink from the given link and baseURL.
func (*PathSpec) PrependBasePath ¶
PrependBasePath prepends any baseURL sub-folder to the given resource
func (*PathSpec) RelURL ¶
func (*PathSpec) URLEscape ¶
URLEscape escapes unicode letters.
func (*PathSpec) URLize ¶
URLize is similar to MakePath, but with Unicode handling Example:
uri: Vim (text editor) urlize: vim-text-editor
func (*PathSpec) URLizeFilename ¶
URLizeFilename creates an URL from a filename by escaping unicode letters and turn any filepath separator into forward slashes.
type ProcessingStats ¶
type ProcessingStats struct { Name string Pages uint64 PaginatorPages uint64 Static uint64 ProcessedImages uint64 Files uint64 Aliases uint64 Cleaned uint64 }
ProcessingStats represents statistics about a site build.
func NewProcessingStats ¶
func NewProcessingStats(name string) *ProcessingStats
NewProcessingStats returns a new ProcessingStats instance.
func (*ProcessingStats) Add ¶
func (s *ProcessingStats) Add(counter *uint64, amount int)
Add adds an amount to a given counter.
func (*ProcessingStats) Incr ¶
func (s *ProcessingStats) Incr(counter *uint64)
Incr increments a given counter.
func (*ProcessingStats) Table ¶
func (s *ProcessingStats) Table(w io.Writer)
Table writes a table-formatted representation of the stats in a ProcessingStats instance to w.
Source Files ¶
content.go docshelper.go emoji.go general.go path.go pathspec.go processing_stats.go url.go
- Version
- v0.144.2 (latest)
- Published
- Feb 19, 2025
- Platform
- linux/amd64
- Imports
- 40 packages
- Last checked
- 13 hours ago –
Tools for package owners.