Documentation
¶
Overview ¶
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 ExtractAndGroupRootPaths(in []string) []string
- func ExtractTOC(content []byte) (newcontent []byte, toc []byte)
- func FirstLower(s string) string
- 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 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 ReaderToString(lines io.Reader) string
- func SliceToLower(s []string) []string
- func StringSliceToList(s []string, c string) string
- func TCPListen() (net.Listener, *net.TCPAddr, error)
- func TotalWords(s string) int
- 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
- type NamedSlice
- type PathSpec
- 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
- type ProcessingStats
Constants ¶
const FilePathSeparator = string(filepath.Separator)
FilePathSeparator as defined by os.Separator.
Variables ¶
This section is empty.
Functions ¶
func BytesToHTML ¶
BytesToHTML converts bytes to type template.HTML.
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 ExtractAndGroupRootPaths ¶ added in v0.56.0
ExtractAndGroupRootPaths extracts and groups root paths from the supplied list of paths. Note that the in slice will be sorted in place.
func ExtractTOC ¶
ExtractTOC extracts Table of Contents from content.
func FirstLower ¶ added in v0.167.0
FirstLower returns s with the first Unicode letter mapped to lowercase.
func FirstUpper ¶
FirstUpper returns s with the first Unicode letter mapped to uppercase.
func FormatByteCount ¶ added in v0.123.0
FormatByteCount pretty formats b.
func GetCacheDir ¶ added in v0.54.0
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 ¶ added in v0.27.1
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 ¶ added in v0.25.1
HasStringsPrefix tests whether the string slice s begins with prefix slice s.
func HasStringsSuffix ¶ added in v0.25.1
HasStringsSuffix tests whether the string slice s ends with suffix slice s.
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 OpenFileForWriting ¶ added in v0.47.1
OpenFileForWriting opens or creates the given file. If the target directory does not exist, it gets created.
func OpenFilesForWriting ¶ added in v0.47.1
OpenFilesForWriting opens all the given filenames for writing.
func PrintFs ¶ added in v0.42.1
PrintFs prints the given filesystem to the given writer starting from the given path. This is useful for debugging.
func ProcessingStatsTable ¶ added in v0.32.1
func ProcessingStatsTable(w io.Writer, stats ...*ProcessingStats)
ProcessingStatsTable writes a table-formatted representation of stats to w.
func ReaderToString ¶
ReaderToString reads from an io.Reader and returns its content as a string.
func SliceToLower ¶
SliceToLower goes through the source slice and lowers all values.
func StringSliceToList ¶ added in v0.148.0
StringSliceToList formats a string slice into a human-readable list. It joins the elements of the slice s with commas, using an Oxford comma, and precedes the final element with the conjunction c.
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)).
Types ¶
type ContentSpec ¶ added in v0.20.1
type ContentSpec struct {
Converters markup.ConverterProvider
Cfg config.AllProvider
// contains filtered or unexported fields
}
ContentSpec provides functionality to render markdown content.
func NewContentSpec ¶ added in v0.20.1
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 ¶ added in v0.60.0
func (c *ContentSpec) ResolveMarkup(in string) string
func (*ContentSpec) SanitizeAnchorName ¶ added in v0.62.2
func (c *ContentSpec) SanitizeAnchorName(s string) string
func (*ContentSpec) TrimShortHTML ¶ added in v0.55.0
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 ¶ added in v0.56.0
func (NamedSlice) String ¶ added in v0.56.0
func (n NamedSlice) String() string
type PathSpec ¶
type PathSpec struct {
*paths.Paths
*filesystems.BaseFs
ProcessingStats *ProcessingStats
// The file systems to use
Fs *hugofs.Fs
}
PathSpec holds methods that decides how paths in URLs and files in Hugo should look like.
func NewPathSpec ¶ added in v0.20.1
func NewPathSpec(fs *hugofs.Fs, cfg config.AllProvider, logger loggers.Logger, baseBaseFs *filesystems.BaseFs) (*PathSpec, error)
NewPathSpec 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) 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 ¶ added in v0.55.0
MakePathsSanitized applies MakePathSanitized on every item in the slice
func (*PathSpec) PermalinkForBaseURL ¶ added in v0.32.1
PermalinkForBaseURL creates a permalink from the given link and baseURL.
func (*PathSpec) PrependBasePath ¶ added in v0.20.1
PrependBasePath prepends any baseURL sub-folder to the given resource
type ProcessingStats ¶ added in v0.32.1
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 ¶ added in v0.32.1
func NewProcessingStats(name string) *ProcessingStats
NewProcessingStats returns a new ProcessingStats instance.
func (*ProcessingStats) Incr ¶ added in v0.32.1
func (s *ProcessingStats) Incr(counter *uint64)
Incr increments a given counter.