utils

package
v0.0.0-...-44e12ef Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: BSD-2-Clause Imports: 16 Imported by: 0

Documentation

Overview

Package utils contains utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsPaths

func AbsPaths(urlPrefix, html string) string

AbsPaths adds urlPrefix to paths of src and href attributes in html starting with a slash (/).

func AddIndexIfNeeded

func AddIndexIfNeeded(filename string) string

AddIndexIfNeeded adds "index.html" to the filename if it ends with a slash. Otherwise returns the original filename.

func CleanPermalink(s string) string

CleanPermalink returns a cleaned version of permalink: without index.htm[l] ending and starting with slash.

func DirExist

func DirExist(path string) bool

DirExists returns true if the given directory exists.

func HasFileExt

func HasFileExt(filename string, extensions []string) bool

Returns true if filename has one of the given extension. Extensions must start with dot.

func Hash

func Hash(data []byte) []byte

Hash returns an SHA256 hash of the given string.

func NoVowelsHexEncode

func NoVowelsHexEncode(b []byte) string

NoVowelsHexEncode returns bytes encoded in a hex-like encoding which doesn't use vowels.

This is useful to avoid producing substrings, such as "ad", that may be blocked by ad-blockers.

func OpenEditor

func OpenEditor(filename string) error

OpenEditor launches editor for editing the given file.

func OpenURL

func OpenURL(addr string) error

OpenURL opens URL in the operating system (probably in the default browser).

func ParseAnyDate

func ParseAnyDate(s string) (d time.Time, err error)

ParseAnyDate parses date in any of the few allowed formats.

func ReplaceFileExt

func ReplaceFileExt(filename string, ext string) string

ReplaceExtension replaces file extension with the given string. Extension must start with dot.

func SplitTags

func SplitTags(tags string) []string

SplitTags splits a comma-separated list of tags into a slice.

func StripEndSlash

func StripEndSlash(s string) string

StripEndSlash returns a string with ending slash removed, or if there was no slash, returns the original string.

func StripHTMLTags

func StripHTMLTags(s string) (output string)

func TemplatedHash

func TemplatedHash(template string, input []byte) string

TemplatedHash replaces ":hash" in template with hexadecimal characters of the hash of the input string and returns the result.

func ToSlug

func ToSlug(title string) string

ToSlug creates a slug from a title, for example "Hello, world!" becomes "hello-world". Only supports Latin characters, others are replaced with dashes.

func UnmarshallYAMLFile

func UnmarshallYAMLFile(filename string, data interface{}) error

UnmarshallYAMLFile reads YAML file and unmarshalls it into data.

Types

type Pool

type Pool struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Pool is a worker pool for parallel job processing.

func NewPool

func NewPool() *Pool

NewPool creates a new pool which calls fn for each added item and stores the first returned error.

func (*Pool) Add

func (p *Pool) Add(job func() error) bool

Add adds a new job to pool. Function passed to Add will be called for each job in a worker goroutine.

If Add returns false, which happens if one of the previously added jobs returned an error, the job will not be added to the pool.

After finishing adding items, Wait must be called on the pool to wait for unfinished jobs to complete and get the first error.

func (*Pool) Wait

func (p *Pool) Wait() error

Wait for jobs to complete and return the first error or nil if there were no errors.

After calling Wait, the pool can be reused.

Jump to

Keyboard shortcuts

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