utils

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyToHTMLText

func ApplyToHTMLText(doc []byte, fn func(string) string) []byte

ApplyToHTMLText applies a filter only to the text within an HTML document.

func CopyFileContents

func CopyFileContents(dst, src string, perm os.FileMode) error

CopyFileContents copies the file contents from src to dst. It's not atomic and doesn't copy permissions or metadata.

func FollowDots

func FollowDots(data interface{}, props []string) (interface{}, error)

FollowDots applied to a property list ["a", "b", "c"] is equivalent to the Liquid data expression "data.a.b.c", except without special treatment of "first", "last", and "size".

func IsNotEmpty

func IsNotEmpty(err error) bool

IsNotEmpty returns a boolean indicating whether the error is known to report that a directory is not empty.

func LeftPad

func LeftPad(s string, n int) string

LeftPad left-pads s with spaces to n wide. It's an alternative to http://left-pad.io.

func MatchList added in v0.1.0

func MatchList(patterns []string, name string) bool

MatchList implement Jekyll include: and exclude: configurations. It reports whether any glob pattern matches the path. It panics with ErrBadPattern if any pattern is malformed. To match Jekyll, a string "dir/" matches that begins with this prefix.

func MergeStringMaps

func MergeStringMaps(ms ...map[string]interface{}) map[string]interface{}

MergeStringMaps creates a new variable map that merges its arguments, from first to last.

func MustAbs

func MustAbs(path string) string

MustAbs is like filepath.Abs, but panics instead of returning an error.

func MustRel

func MustRel(basepath, targpath string) string

MustRel is like filepath.Rel, but panics if the path cannot be relativized.

func NewPathError

func NewPathError(op, name, text string) *os.PathError

NewPathError returns an os.PathError that formats as the given text.

func ParseFilenameDateTitle added in v0.2.7

func ParseFilenameDateTitle(s string) (t time.Time, title string, found bool)

ParseFilenameDateTitle returns the date for a filename that uses Jekyll post convention. It also returns a bool indicating whether a date was found.

func PostfixWalk

func PostfixWalk(root string, walkFn filepath.WalkFunc) error

PostfixWalk is like filepath.Walk, but visits each directory after visiting its children instead of before. It does not implement SkipDir.

func ReadFileMagic

func ReadFileMagic(filename string) ([]byte, error)

ReadFileMagic returns the first four bytes of the file, with final '\r' replaced by '\n'.

func RemoveEmptyDirectories

func RemoveEmptyDirectories(root string) error

RemoveEmptyDirectories recursively removes empty directories.

func SafeReplaceAllStringFunc

func SafeReplaceAllStringFunc(re *regexp.Regexp, src string, repl func(m string) (string, error)) (out string, err error)

SafeReplaceAllStringFunc is like regexp.ReplaceAllStringFunc but passes an an error back from the replacement function.

func SearchStrings

func SearchStrings(array []string, s string) bool

SearchStrings returns a bool indicating whether array contains the string. Unlike sort.SearchStrings, it does not require a sorted array. This is useful when the array length is low; else consider sorting it and using sort.SearchStrings, or creating a map[string]bool.

func Slugify

func Slugify(s string) string

Slugify replaces each sequence of non-alphanumerics by a single hyphen

func StringArrayContains added in v0.2.7

func StringArrayContains(a []string, s string) bool

StringArrayContains returns a bool indicating whether the array contains the string.

func StringArrayToMap

func StringArrayToMap(a []string) map[string]bool

StringArrayToMap creates a map for use as a set.

func Titleize added in v0.2.7

func Titleize(s string) string

Titleize splits at ` `, capitalizes, and joins.

func TrimExt

func TrimExt(name string) string

TrimExt returns a path without its extension, if any

func URLJoin

func URLJoin(paths ...string) string

URLJoin interpolates paths with "/", skipping empty paths and avoiding "//".

func URLPathClean

func URLPathClean(url string) string

URLPathClean removes internal // etc. Unlike path.Clean, it leaves the final "/" intact.

func UnmarshalYAMLInterface

func UnmarshalYAMLInterface(b []byte, i *interface{}) error

UnmarshalYAMLInterface is a wrapper for yaml.Unmarshal that knows how to unmarshal maps and lists.

func VisitCreatedFile

func VisitCreatedFile(filename string, w func(io.Writer) error) (err error)

VisitCreatedFile calls os.Create to create a file, and applies w to it.

func WrapError added in v0.2.7

func WrapError(err error, m string) error

WrapError returns an error decorated with a message. If the error is nil, it returns nil.

func WrapPathError

func WrapPathError(err error, path string) error

WrapPathError returns an error that will print with a path.\ It wraps its argument if it is not nil and does not already provide a path.

Types

type PathError

type PathError interface {
	WrappedError
	Path() string
}

A PathError is an error with a source path.

An os.PathError is unfortunately not a PathError, but this is still useful for deciding whether to wrap other errors.

type StringList added in v0.2.7

type StringList []string

StringList adds methods to []string

func (StringList) Reject added in v0.2.7

func (sl StringList) Reject(p func(string) bool) StringList

Reject returns a copy of StringList without elements that its argument tests true on.

type StringSet added in v0.2.0

type StringSet map[string]bool

A StringSet is a set of strings, represented as a map.

func MakeStringSet added in v0.2.7

func MakeStringSet(a []string) StringSet

MakeStringSet creates a characteristic function map that tests for presence in an array.

func (StringSet) AddStrings added in v0.2.7

func (ss StringSet) AddStrings(a []string)

AddStrings modifies the set to include the strings in an array.

func (StringSet) Contains added in v0.2.7

func (ss StringSet) Contains(s string) bool

Contains returns true iff the string is in the set.

type WrappedError added in v0.2.7

type WrappedError interface {
	error
	Cause() error
}

A WrappedError decorates an error with a message

Jump to

Keyboard shortcuts

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