utils

package
v0.0.0-...-e2a01bb Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const TemplatePrefix = "views"

TemplatePrefix is the constant containing the filepath prefix for templates.

Variables

View Source
var BaseTemplates = []string{
	"partials/header.tmpl",
	"partials/footer.tmpl",
	"elements/navbar.tmpl",
	"partials/base.tmpl",
}

BaseTemplates is the array of 'base' templates used in each template render.

View Source
var StartHour = 16

StartHour is the hour of the day (local time) at which the scheduled day begins.

Functions

func FormatWeekRelative

func FormatWeekRelative(start time.Time) string

FormatWeekRelative pretty-prints the name of a week starting on start, relative to today. start must be a Monday.

func FormatWeekRelativeTo

func FormatWeekRelativeTo(start, now time.Time) string

FormatWeekRelativeTo pretty-prints the name of the current week of start, relative to the current week of now.

func GetRequesterIP

func GetRequesterIP(c *structs.Config, r *http.Request) (net.IP, error)

func IsoWeekToDate

func IsoWeekToDate(year, week int, weekday time.Weekday) (time.Time, error)

IsoWeekToDate interprets year, week, and weekday strings as an ISO weekday. The time is set to local midnight.

func MostRecentMonday

func MostRecentMonday(d time.Time) time.Time

MostRecentMonday returns the most recent Monday before d. The resulting date has the same time as d.

func ParseIsoWeek

func ParseIsoWeek(isoyear, isoweek, isoweekday string) (year int, week int, weekday time.Weekday, err error)

ParseIsoWeek parses an ISO weekday from year, week, and weekday strings. It performs bounds checking. weekday must be an integer from 1 (Monday) to 7 (Sunday).

func PrefixURL

func PrefixURL(url, p string) string

PrefixURL prefixes a URL url with prefix p. If url begins with a single slash, it is treated as relative and prefixed with prefix. If p does not begin with a slash and end in zero slashes, it is changed to do so.

func RenderTemplate

func RenderTemplate(w http.ResponseWriter, context structs.PageContext, data interface{}, mainTmpl string, addTmpls ...string) error

RenderTemplate renders a 2016site template on the ResponseWriter w.

This function automatically adds in the 2016site base templates, performs error handling, and builds a global context.

The PageContext context gives the context for the page to be rendered, sent to the template as PageContext. The interface{} data gives the data to be sent to the template as PageData.

The string mainTmpl gives the name, relative to views, of the main template to render. The variadic argument addTmpls names any additional templates mainTmpl depends on.

RenderTemplate returns any error that occurred when rendering the template.

func StartOfDayOn

func StartOfDayOn(date time.Time) time.Time

StartOfDayOn gets the schedule start-of-day on a given date. This is in terms of StartHour.

func StripHTML

func StripHTML(htmls string) (string, error)

StripHTML strips HTML tags from a string, extracting all plain text.

Types

type StartOffset

type StartOffset int

StartOffset is the type of offsets from the start hour of a schedule.

func HourToStartOffset

func HourToStartOffset(hour int) (StartOffset, error)

HourToStartOffset takes an hour (0-23) and gives the number of hours elapsed since the last day start. It returns an error if the hour is invalid.

func (StartOffset) ToHour

func (h StartOffset) ToHour() (int, error)

ToHour takes a number of hours h since the last day start (0-23) and gives the actual hour. It returns an error if the hour is invalid.

func (StartOffset) Valid

func (h StartOffset) Valid() bool

Valid returns whether a StartOffset is within the 0-23 range required for it to index a schedule hour.

Jump to

Keyboard shortcuts

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