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
- Variables
- func AbsPathify(inPath string) string
- func AbsURL(path string) string
- func AddContextRoot(baseURL, relativePath string) string
- func BytesToHTML(b []byte) template.HTML
- func Deprecated(object, item, alternative string)
- func DiffStringSlices(slice1 []string, slice2 []string) []string
- func DirExists(path string, fs afero.Fs) (bool, error)
- func DoArithmetic(a, b interface{}, op rune) (interface{}, 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 FileContains(filename string, subslice []byte, fs afero.Fs) (bool, error)
- func FileContainsAny(filename string, subslices [][]byte, fs afero.Fs) (bool, error)
- func Filename(in string) (name string)
- func FindAvailablePort() (*net.TCPAddr, error)
- func FindCWD() (string, error)
- func FirstUpper(s string) string
- func GetDottedRelativePath(inPath string) string
- func GetRelativePath(path, base string) (final string, err error)
- func GetStaticDirPath() string
- func GetTempDir(subPath string, fs afero.Fs) string
- func GetThemeDataDirPath() (string, error)
- func GetThemeDir() string
- func GetThemeStaticDirPath() (string, error)
- func GetThemesDirPath() string
- func GuessSection(in string) string
- func GuessType(in string) string
- func HasAsciidoc() bool
- func HasPygments() bool
- func HasRst() bool
- func Highlight(code, lang, optsStr string) string
- func HugoReleaseVersion() string
- func HugoVersion() string
- func InStringArray(arr []string, el 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 MakePath(s string) string
- func MakePathSanitized(s string) string
- func MakePermalink(host, plink string) *url.URL
- func MakeStaticPathRelative(inPath string) (string, error)
- func MakeTitle(inpath string) string
- func Md5String(f string) string
- func NextHugoReleaseVersion() string
- func NormalizeHugoFlags(f *pflag.FlagSet, name string) pflag.NormalizedName
- func PaginateAliasPath(base string, page int) string
- func PathPrep(ugly bool, in string) string
- func PrettifyPath(in string) string
- func PrettifyURL(in string) string
- func PrettifyURLPath(in string) string
- func ReaderContains(r io.Reader, subslice []byte) bool
- func ReaderToBytes(lines io.Reader) []byte
- func ReaderToString(lines io.Reader) string
- func RelURL(path string) string
- func RemoveSummaryDivider(content []byte) []byte
- func RenderBytes(ctx *RenderingContext) []byte
- func ReplaceExtension(path string, newExt string) string
- func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error)
- func SanitizeURL(in string) string
- func SanitizeURLKeepTrailingSlash(in string) string
- func Seq(args ...interface{}) ([]int, error)
- func SliceToLower(s []string) []string
- func StripHTML(s string) string
- func SymbolicWalk(fs afero.Fs, root string, walker filepath.WalkFunc) error
- func ThemeSet() bool
- func TotalWords(s string) int
- func TruncateWordsByRune(words []string, max int) (string, bool)
- func TruncateWordsToWholeSentence(words []string, max int) (string, bool)
- func URLPrep(ugly bool, in string) string
- func URLize(uri string) string
- func URLizeAndPrep(in string) string
- func Uglify(in string) string
- func UnicodeSanitize(s string) string
- func UniqueStrings(s []string) []string
- func WordCount(s string) map[string]int
- func WriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error)
- type Blackfriday
- type DistinctLogger
- type FileResolverFunc
- type HugoHTMLRenderer
- type HugoMmarkHTMLRenderer
- type LinkResolverFunc
- type RenderingContext
Constants ¶
const FilePathSeparator = string(filepath.Separator)
FilePathSeparator as defined by os.Separator.
const HugoVersionNumber = 0.17
HugoVersionNumber represents the current build version. This should be the only one
const HugoVersionSuffix = "-DEV" // blank this when doing a release
HugoVersionSuffix is the suffix used in the Hugo version string. It will be blank for release versions.
Variables ¶
var DistinctErrorLog = NewDistinctErrorLogger()
DistinctErrorLog cann be used to avoid spamming the logs with errors.
var SummaryDivider = []byte("<!--more-->")
SummaryDivider denotes where content summarization should end. The default is "<!--more-->".
var SummaryLength = 70
SummaryLength is the length of the summary that Hugo extracts from a content.
var WalkRootTooShortError = errors.New("Path too short. Stop walking.")
Functions ¶
func AbsPathify ¶
AbsPathify creates an absolute path if given a relative path. If already absolute, the path is just cleaned.
func AbsURL ¶
AbsURL creates a absolute URL from the relative path given and the BaseURL set in config.
func AddContextRoot ¶
AddContextRoot adds the context root to an URL if it's not already set. For relative URL entries on sites with a base url with a context root set (i.e. http://example.com/mysite), relative URLs must not include the context root if canonifyURLs is enabled. But if it's disabled, it must be set.
func BytesToHTML ¶
BytesToHTML converts bytes to type template.HTML.
func Deprecated ¶
func Deprecated(object, item, alternative string)
Deprecated logs ERROR logs about a deprecation, but only once for a given set of arguments' values.
func DiffStringSlices ¶
DiffStringSlices returns the difference between two string slices. Useful in tests. See: http://stackoverflow.com/questions/19374219/how-to-find-the-difference-between-two-slices-of-strings-in-golang
func DoArithmetic ¶
DoArithmetic performs arithmetic operations (+,-,*,/) using reflection to determine the type of the two terms.
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 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 FileContains ¶
FileContains checks if a file contains a specified string.
func FileContainsAny ¶
FileContainsAny checks if a file contains any of the specified strings.
func FindAvailablePort ¶
FindAvailablePort returns an available and valid TCP port.
func FirstUpper ¶
FirstUpper returns a string with the first character as upper case.
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 GetRelativePath ¶
GetRelativePath returns the relative path of a given path.
func GetStaticDirPath ¶
func GetStaticDirPath() string
GetStaticDirPath returns the absolute path to the static file dir for the current Hugo project.
func GetTempDir ¶
GetTempDir returns a temporary directory with the given sub path.
func GetThemeDataDirPath ¶
GetThemeDataDirPath returns the theme's data dir path if theme is set. If theme is set and the data dir doesn't exist, an error is returned.
func GetThemeDir ¶
func GetThemeDir() string
GetThemeDir gets the root directory of the current theme, if there is one. If there is no theme, returns the empty string.
func GetThemeStaticDirPath ¶
GetThemeStaticDirPath returns the theme's static dir path if theme is set. If theme is set and the static dir doesn't exist, an error is returned.
func GetThemesDirPath ¶
func GetThemesDirPath() string
GetThemesDirPath gets the static files directory of the current theme, if there is one. Ignores underlying errors. TODO(bep) Candidate for deprecation?
func GuessSection ¶
GuessSection returns the section given a source path. A section is the part between the root slash and the second slash or before the first slash.
func HasAsciidoc ¶
func HasAsciidoc() bool
HasAsciidoc returns whether Asciidoctor or Asciidoc is installed on this computer.
func HasPygments ¶
func HasPygments() bool
HasPygments checks to see if Pygments is installed and available on the system.
func HugoReleaseVersion ¶
func HugoReleaseVersion() string
HugoReleaseVersion is same as HugoVersion, but no suffix.
func HugoVersion ¶
func HugoVersion() string
HugoVersion returns the current Hugo version. It will include a suffix, typically '-DEV', if it's development version.
func InStringArray ¶
InStringArray checks if a string is an element of a slice of strings and returns a boolean value.
func IsWhitespace ¶
IsWhitespace determines if the given rune is whitespace.
func 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 MakePathSanitized ¶
MakePathSanitized creates a Unicode-sanitized string, with the spaces replaced
func MakePermalink ¶
MakePermalink combines base URL with content path to create full URL paths. Example
base: http://spf13.com/ path: post/how-i-blog result: http://spf13.com/post/how-i-blog
func MakeStaticPathRelative ¶
MakeStaticPathRelative makes a relative path to the static files directory. It does so by taking either the project's static path or the theme's static path into consideration.
func MakeTitle ¶
MakeTitle converts the path given to a suitable title, trimming whitespace and replacing hyphens with whitespace.
func NextHugoReleaseVersion ¶
func NextHugoReleaseVersion() string
NextHugoReleaseVersion returns the next Hugo release version.
func NormalizeHugoFlags ¶
func NormalizeHugoFlags(f *pflag.FlagSet, name string) pflag.NormalizedName
NormalizeHugoFlags facilitates transitions of Hugo command-line flags, e.g. --baseUrl to --baseURL, --uglyUrls to --uglyURLs
func PaginateAliasPath ¶
PaginateAliasPath creates a path used to access the aliases in the paginator.
func PathPrep ¶
PathPrep prepares the path using the uglify setting to create paths on either the form /section/name/index.html or /section/name.html.
func PrettifyPath ¶
PrettifyPath is the same as PrettifyURLPath but for file paths.
/section/name.html becomes /section/name/index.html /section/name/ becomes /section/name/index.html /section/name/index.html becomes /section/name/index.html
func PrettifyURL ¶
PrettifyURL takes a URL string and returns a semantic, clean URL.
func PrettifyURLPath ¶
PrettifyURLPath takes a URL path to a content and converts it to enable pretty URLs.
/section/name.html becomes /section/name/index.html /section/name/ becomes /section/name/index.html /section/name/index.html becomes /section/name/index.html
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 RelURL ¶
RelURL creates a URL relative to the BaseURL root. Note: The result URL will not include the context root if canonifyURLs is enabled.
func RemoveSummaryDivider ¶
RemoveSummaryDivider removes summary-divider <!--more--> from content.
func ReplaceExtension ¶
ReplaceExtension takes a path and an extension, strips the old extension and returns the path with the new extension.
func SafeWriteToDisk ¶
SafeWriteToDisk is the same as WriteToDisk but it also checks to see if file/directory already exists.
func SanitizeURLKeepTrailingSlash ¶
SanitizeURLKeepTrailingSlash is the same as SanitizeURL, but will keep any trailing slash.
func Seq ¶
Seq creates a sequence of integers. It's named and used as GNU's seq. Examples: 3 => 1, 2, 3 1 2 4 => 1, 3 -3 => -1, -2, -3 1 4 => 1, 2, 3, 4 1 -2 => 1, 0, -1, -2
func SliceToLower ¶
SliceToLower goes through the source slice and lowers all values.
func SymbolicWalk ¶
SymbolicWalk is like filepath.Walk, but it supports the root being a symbolic link. It will still not follow symbolic links deeper down in the file structure
func TotalWords ¶
TotalWords returns an int of the total number of words in a given content.
func TruncateWordsByRune ¶
TruncateWordsByRune truncates words by runes.
func TruncateWordsToWholeSentence ¶
TruncateWordsToWholeSentence takes content and an int and returns entire sentences from content, delimited by the int and whether it's truncated or not.
func URLize ¶
URLize is similar to MakePath, but with Unicode handling Example:
uri: Vim (text editor) urlize: vim-text-editor
func URLizeAndPrep ¶
func Uglify ¶
Uglify does the opposite of PrettifyURLPath().
/section/name/index.html becomes /section/name.html /section/name/ becomes /section/name.html /section/name.html becomes /section/name.html
func UnicodeSanitize ¶
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, Uniccode accents are also removed.
func UniqueStrings ¶
UniqueStrings returns a new slice with any duplicates removed.
Types ¶
type Blackfriday ¶
type Blackfriday struct { Smartypants bool AngledQuotes bool Fractions bool HrefTargetBlank bool SmartDashes bool LatexDashes bool PlainIDAnchors bool SourceRelativeLinksEval bool SourceRelativeLinksProjectFolder string Extensions []string ExtensionsMask []string }
Blackfriday holds configuration values for Blackfriday rendering.
func NewBlackfriday ¶
func NewBlackfriday() *Blackfriday
NewBlackfriday creates a new Blackfriday filled with site config or some sane defaults.
type DistinctLogger ¶
DistinctLogger ignores duplicate log statements.
func NewDistinctErrorLogger ¶
func NewDistinctErrorLogger() *DistinctLogger
NewDistinctErrorLogger creates a new DistinctLogger that logs ERRORs
func NewDistinctFeedbackLogger ¶
func NewDistinctFeedbackLogger() *DistinctLogger
NewDistinctFeedbackLogger creates a new DistinctLogger that can be used to give feedback to the user while not spamming with duplicates.
func (*DistinctLogger) Printf ¶
func (l *DistinctLogger) Printf(format string, v ...interface{})
Printf will log the string returned from fmt.Sprintf given the arguments, but not if it has been logged before. Note: A newline is appended.
func (*DistinctLogger) Println ¶
func (l *DistinctLogger) Println(v ...interface{})
Println will log the string returned from fmt.Sprintln given the arguments, but not if it has been logged before.
type FileResolverFunc ¶
type HugoHTMLRenderer ¶
type HugoHTMLRenderer struct { *RenderingContext blackfriday.Renderer }
HugoHTMLRenderer wraps a blackfriday.Renderer, typically a blackfriday.Html Enabling Hugo to customise the rendering experience
func (*HugoHTMLRenderer) BlockCode ¶
func (renderer *HugoHTMLRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string)
type HugoMmarkHTMLRenderer ¶
HugoMmarkHTMLRenderer wraps a mmark.Renderer, typically a mmark.html Enabling Hugo to customise the rendering experience
type LinkResolverFunc ¶
type RenderingContext ¶
type RenderingContext struct { Content []byte PageFmt string DocumentID string Config *Blackfriday RenderTOC bool FileResolver FileResolverFunc LinkResolver LinkResolverFunc // contains filtered or unexported fields }
RenderingContext holds contextual information, like content and configuration, for a given content rendering.