mtemplate

package
v0.0.0-...-94ee829 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 13 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FuncMap = template.FuncMap{
	"CollapseParagraphs":           CollapseParagraphs,
	"DistanceOfTimeInWords":        DistanceOfTimeInWords,
	"DistanceOfTimeInWordsFromNow": DistanceOfTimeInWordsFromNow,
	"DownloadedImage":              DownloadedImage,
	"Figure":                       Figure,
	"FigureSingle":                 FigureSingle,
	"FigureSingleWithClass":        FigureSingleWithClass,
	"FormatTime":                   FormatTime,
	"FormatTimeRFC3339UTC":         FormatTimeRFC3339UTC,
	"FormatTimeSimpleDate":         FormatTimeSimpleDate,
	"HTMLRender":                   HTMLRender,
	"HTMLSafePassThrough":          HTMLSafePassThrough,
	"ImgSrcAndAlt":                 ImgSrcAndAlt,
	"ImgSrcAndAltAndClass":         ImgSrcAndAltAndClass,
	"Map":                          Map,
	"MapVal":                       MapVal,
	"MapValAdd":                    MapValAdd,
	"QueryEscape":                  QueryEscape,
	"RomanNumeral":                 RomanNumeral,
	"RoundToString":                RoundToString,
	"TimeIn":                       TimeIn,
	"To2X":                         To2X,
}

FuncMap is a set of helper functions to make available in templates for the project.

Functions

func CollapseParagraphs

func CollapseParagraphs(s string) string

CollapseParagraphs strips paragraph tags out of rendered HTML. Note that it does not handle HTML with any attributes, so is targeted mainly for use with HTML generated from Markdown.

func CombineFuncMaps

func CombineFuncMaps(funcMaps ...template.FuncMap) template.FuncMap

CombineFuncMaps combines a number of function maps into one. The combined version is a new function map so that none of the originals are tainted.

func DistanceOfTimeInWords

func DistanceOfTimeInWords(to, from time.Time) string

DistanceOfTimeInWords returns a string describing the relative time passed between two times.

func DistanceOfTimeInWordsFromNow

func DistanceOfTimeInWordsFromNow(to time.Time) string

DistanceOfTimeInWordsFromNow returns a string describing the relative time passed between a time and the current moment.

func DownloadedImage

func DownloadedImage(ctx context.Context, slug, imageURL string, width int) string

DownloadedImage represents an image that's available remotely, and which will be downloaded and stored as the local target slug. This doesn't happen automatically though -- DownloadedImageContext must be called first to set a context container, and from there any downloaded image slugs and URLs can be extracted after all sources are rendered to be sent to mimage for processing.

func Figure

func Figure(figCaption string, imgs ...*HTMLImage) template.HTML

Figure wraps a number of images into a figure and assigns them a caption as well as alt text.

func FigureSingle

func FigureSingle(figCaption, src string) template.HTML

FigureSingle is a shortcut for creating a simple figure with a single image and with an alt that matches the caption.

func FigureSingleWithClass

func FigureSingleWithClass(figCaption, src, class string) template.HTML

FigureSingleWithClass is a shortcut for creating a simple figure with a single image and with an alt that matches the caption, and with an HTML class..

func FormatTime

func FormatTime(t time.Time, format string) string

FormatTime formats time according to the given format string.

func FormatTimeRFC3339UTC

func FormatTimeRFC3339UTC(t time.Time) string

FormatTime formats time according to the given format string.

func FormatTimeSimpleDate

func FormatTimeSimpleDate(t time.Time) string

FormatTimeSimpleDate formats time according to a relatively straightforward time format.

func HTMLFuncMapToText

func HTMLFuncMapToText(funcMap template.FuncMap) texttemplate.FuncMap

HTMLFuncMapToText transforms an HTML func map to a text func map.

func HTMLRender

func HTMLRender(elements ...HTMLElement) template.HTML

HTMLRender renders a series of mtemplate HTML elements.

func HTMLSafePassThrough

func HTMLSafePassThrough(s string) template.HTML

HTMLSafePassThrough passes a string through to the final render. This is especially useful for code samples that contain Go template syntax which shouldn't be rendered.

func Map

func Map(vals ...*mapVal) map[string]interface{}

func MapVal

func MapVal(key string, val interface{}) *mapVal

MapVal generates a new map key/value for use with MapValAdd.

func MapValAdd

func MapValAdd(m map[string]interface{}, vals ...*mapVal) map[string]interface{}

MapValAdd is a convenience helper for adding a new key and value to a shallow copy of the given map and returning it.

func QueryEscape

func QueryEscape(s string) string

QueryEscape escapes a URL.

func RomanNumeral

func RomanNumeral(num int) string

func RoundToString

func RoundToString(f float64) string

RoundToString rounds a float to a presentation-friendly string.

func TimeIn

func TimeIn(t time.Time, locationName string) time.Time

func To2X

func To2X(imagePath string) template.HTML

To2X takes a 1x (standad resolution) image path and changes it to a 2x path by putting `@2x` into its name right before its extension.

Types

type DownloadedImageContextContainer

type DownloadedImageContextContainer struct {
	Images []*DownloadedImageInfo
}

type DownloadedImageInfo

type DownloadedImageInfo struct {
	Slug  string
	URL   *url.URL
	Width int
	// contains filtered or unexported fields
}

func (*DownloadedImageInfo) OriginalExt

func (p *DownloadedImageInfo) OriginalExt() string

type HTMLElement

type HTMLElement interface {
	// contains filtered or unexported methods
}

HTMLElement represents an HTML element that can be rendered.

type HTMLImage

type HTMLImage struct {
	Src   string
	Alt   string
	Class string
}

HTMLImage is a simple struct representing an HTML image to be rendered and some of the attributes it might have.

func ImgSrcAndAlt

func ImgSrcAndAlt(imgSrc, imgAlt string) *HTMLImage

ImgSrcAndAlt is a shortcut for creating ImgSrcAndAlt.

func ImgSrcAndAltAndClass

func ImgSrcAndAltAndClass(imgSrc, imgAlt, class string) *HTMLImage

ImgSrcAndAltAndClass is a shortcut for creating ImgSrcAndAlt with a CSS class.

Jump to

Keyboard shortcuts

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