templating

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: GPL-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CastFuncs = map[string]any{

	"itof": Itof,
	"ftoi": Ftoi,
}

CastFuncs is a map of templating functions for typecasting values.

View Source
var CollectionFuncs = map[string]any{
	"map":      MakeMap,
	"list":     MakeList,
	"reversed": Reversed,
	"append":   AppendList,
}
View Source
var EnvFuncs = map[string]any{
	"getenv": os.Getenv,
}
View Source
var EventConsts = map[string]any{
	"CHAG":                event.CHAG,
	"LIGHT_CANDLES":       event.LIGHT_CANDLES,
	"YOM_TOV_ENDS":        event.YOM_TOV_ENDS,
	"CHUL_ONLY":           event.CHUL_ONLY,
	"IL_ONLY":             event.IL_ONLY,
	"LIGHT_CANDLES_TZEIS": event.LIGHT_CANDLES_TZEIS,
	"CHANUKAH_CANDLES":    event.CHANUKAH_CANDLES,
	"ROSH_CHODESH":        event.ROSH_CHODESH,
	"MINOR_FAST":          event.MINOR_FAST,
	"SPECIAL_SHABBAT":     event.SPECIAL_SHABBAT,
	"PARSHA_HASHAVUA":     event.PARSHA_HASHAVUA,
	"DAF_YOMI":            event.DAF_YOMI,
	"OMER_COUNT":          event.OMER_COUNT,
	"MODERN_HOLIDAY":      event.MODERN_HOLIDAY,
	"MAJOR_FAST":          event.MAJOR_FAST,
	"SHABBAT_MEVARCHIM":   event.SHABBAT_MEVARCHIM,
	"MOLAD":               event.MOLAD,
	"USER_EVENT":          event.USER_EVENT,
	"HEBREW_DATE":         event.HEBREW_DATE,
	"MINOR_HOLIDAY":       event.MINOR_HOLIDAY,
	"EREV":                event.EREV,
	"CHOL_HAMOED":         event.CHOL_HAMOED,
	"MISHNA_YOMI":         event.MISHNA_YOMI,
	"YOM_KIPPUR_KATAN":    event.YOM_KIPPUR_KATAN,
	"ZMANIM":              event.ZMANIM,
	"YERUSHALMI_YOMI":     event.YERUSHALMI_YOMI,
	"NACH_YOMI":           event.NACH_YOMI,
}
View Source
var HDateConsts = map[string]any{
	"Nisan":    hdate.Nisan,
	"Iyyar":    hdate.Iyyar,
	"Sivan":    hdate.Sivan,
	"Tamuz":    hdate.Tamuz,
	"Av":       hdate.Av,
	"Elul":     hdate.Elul,
	"Tishrei":  hdate.Tishrei,
	"Cheshvan": hdate.Cheshvan,
	"Kislev":   hdate.Kislev,
	"Tevet":    hdate.Tevet,
	"Shvat":    hdate.Shvat,
	"Adar1":    hdate.Adar1,
	"Adar2":    hdate.Adar2,

	"Epoch": hdate.Epoch,
}
View Source
var HDateFuncs = map[string]any{
	"hdateEqual":                  xhdate.Equal,
	"hdateParse":                  xhdate.Parse,
	"hdateNextMonth":              xhdate.NextMonth,
	"hdatePrevMonth":              xhdate.PrevMonth,
	"hdateIsLeapYear":             hdate.IsLeapYear,
	"hdateMonthsInYear":           hdate.MonthsInYear,
	"hdateDaysInYear":             hdate.DaysInYear,
	"hdateLongCheshvan":           hdate.LongCheshvan,
	"hdateShortKislev":            hdate.ShortKislev,
	"hdateDaysInMonth":            hdate.DaysInMonth,
	"hdateToRD":                   hdate.ToRD,
	"hdateNew":                    hdate.New,
	"hdateFromRD":                 hdate.FromRD,
	"hdateFromGregorian":          hdate.FromGregorian,
	"hdateFromProlepticGregorian": hdate.FromProlepticGregorian,
	"hdateFromTime":               hdate.FromTime,
	"hdateMonthFromName":          hdate.MonthFromName,
	"hdateDayOnOrBefore":          hdate.DayOnOrBefore,
}
View Source
var SedraConsts = map[string]any{

	"Bereshit":    1,
	"Noach":       2,
	"LechLecha":   3,
	"Vayera":      4,
	"ChayeiSara":  5,
	"Toldot":      6,
	"Vayetzei":    7,
	"Vayishlach":  8,
	"Vayeshev":    9,
	"Miketz":      10,
	"Vayigash":    11,
	"Vayechi":     12,
	"Shemot":      13,
	"Vaera":       14,
	"Bo":          15,
	"Beshalach":   16,
	"Yitro":       17,
	"Mishpatim":   18,
	"Terumah":     19,
	"Tetzaveh":    20,
	"KiTisa":      21,
	"Vayakhel":    22,
	"Pekudei":     23,
	"Vayikra":     24,
	"Tzav":        25,
	"Shmini":      26,
	"Tazria":      27,
	"Metzora":     28,
	"AchreiMot":   29,
	"Kedoshim":    30,
	"Emor":        31,
	"Behar":       32,
	"Bechukotai":  33,
	"Bamidbar":    34,
	"Nasso":       35,
	"Behaalotcha": 36,
	"Shlach":      37,
	"Korach":      38,
	"Chukat":      39,
	"Balak":       40,
	"Pinchas":     41,
	"Matot":       42,
	"Masei":       43,
	"Devarim":     44,
	"Vaetchanan":  45,
	"Eikev":       46,
	"Reeh":        47,
	"Shoftim":     48,
	"KiTeitzei":   49,
	"KiTavo":      50,
	"Nitzavim":    51,
	"Vayeilech":   52,
	"Haazinu":     53,
}
View Source
var SedraFuncs = map[string]any{
	"sedra":   Sedra,
	"parasha": LocalizedParasha,
}
View Source
var StringFuncs = map[string]any{
	"contains":        strings.Contains,
	"containsAny":     strings.ContainsAny,
	"count":           strings.Count,
	"equalFold":       strings.EqualFold,
	"hasPrefix":       strings.HasPrefix,
	"hasSuffix":       strings.HasSuffix,
	"stringsIndex":    strings.Index,
	"stringsIndexAny": strings.IndexAny,
	"join":            strings.Join,
	"lastIndex":       strings.LastIndex,
	"lastIndexAny":    strings.LastIndexAny,
	"lines":           strings.Lines,
	"repeat":          strings.Repeat,
	"replace":         strings.Replace,
	"replaceAll":      strings.ReplaceAll,
	"split":           strings.Split,
	"splitAfter":      strings.SplitAfter,
	"splitAfterN":     strings.SplitAfterN,
	"splitAfterSeq":   strings.SplitAfterSeq,
	"splitN":          strings.SplitN,
	"splitSeq":        strings.SplitSeq,
	"toLower":         strings.ToLower,
	"toTitle":         strings.ToTitle,
	"toUpper":         strings.ToUpper,
	"toValidUTF8":     strings.ToValidUTF8,
	"trim":            strings.Trim,
	"trimLeft":        strings.TrimLeft,
	"tripPrefix":      strings.TrimPrefix,
	"trimRight":       strings.TrimRight,
	"trimSpace":       strings.TrimSpace,
	"trimSuffix":      strings.TrimSuffix,

	"translate": Translate,
}
View Source
var TimeConsts = map[string]any{
	"Hour":        time.Hour,
	"Minute":      time.Minute,
	"Second":      time.Second,
	"Millisecond": time.Millisecond,
	"Microsecond": time.Microsecond,
	"Nanosecond":  time.Nanosecond,

	"Layout":      time.Layout,
	"ANSIC":       time.ANSIC,
	"UnixDate":    time.UnixDate,
	"RubyDate":    time.RubyDate,
	"RFC822":      time.RFC822,
	"RFC822Z":     time.RFC822Z,
	"RFC850":      time.RFC850,
	"RFC1123":     time.RFC1123,
	"RFC1123Z":    time.RFC1123Z,
	"RFC3339":     time.RFC3339,
	"RFC3339Nano": time.RFC3339Nano,
	"Kitchen":     time.Kitchen,

	"Stamp":      time.Stamp,
	"StampMilli": time.StampMilli,
	"StampMicro": time.StampMicro,
	"StampNano":  time.StampNano,
	"DateTime":   time.DateTime,
	"DateOnly":   time.DateOnly,
	"TimeOnly":   time.TimeOnly,

	"January":   time.January,
	"February":  time.February,
	"March":     time.March,
	"April":     time.April,
	"May":       time.May,
	"June":      time.June,
	"July":      time.July,
	"August":    time.August,
	"September": time.September,
	"October":   time.October,
	"November":  time.November,
	"December":  time.December,

	"Sunday":    time.Sunday,
	"Monday":    time.Monday,
	"Tuesday":   time.Tuesday,
	"Wednesday": time.Wednesday,
	"Thursday":  time.Thursday,
	"Friday":    time.Friday,
	"Saturday":  time.Saturday,
}
View Source
var TimeFuncs = map[string]any{

	"secondsDuration":   SecondsDuration,
	"timeParseDuration": time.ParseDuration,
	"timeSince":         time.Since,
	"timeUntil":         time.Until,
	"durationDiv":       DurationDiv,
	"durationMul":       DurationMul,

	"timeFixedZone":    time.FixedZone,
	"timeLoadLocation": time.LoadLocation,

	"timeNow":             time.Now,
	"timeParse":           time.Parse,
	"timeParseInLocation": time.ParseInLocation,
	"timeUnix":            time.Unix,
	"timeUnixMicro":       time.UnixMicro,
	"timeUnixMilli":       time.UnixMilli,
	"timeDate":            time.Date,
	"datePartsEqual":      DatePartsEqual,
}

Functions

func AppendList added in v0.0.7

func AppendList(s []any, args ...any) []any

AppendList returns the result of appending args to s.

func Apply added in v0.0.5

func Apply(s []string, fn func(string) string) []string

Apply calls fn on each of the members of s and returns the results.

func AsEvent

func AsEvent[T event.CalEvent](e event.CalEvent) *T

AsEvent attempts to convert an event.CalEvent interface to its underlying concrete type. If the conversion fails, it returns nil.

func BuildData added in v0.0.5

func BuildData(
	cfg *config.Config,
	files fs.FS,
	tmplPath string,
) (*template.Template, map[string]any, error)

BuildData loads tmplPath from the files and configures it. It returns the tmpl and the data on which it should be executed.

In detail:

  1. Builds variables and adds them to a data map, which the template will run on.

  2. Builds the FuncMap and adds it to the template.

  3. Parses the template.

  4. Sets the template ParseName (for runtime debugging messages).

These are the variables provided to the template:

  • `$.now` - the current time
  • `$.nowInLocation` - the current time, localized to the config file's timezone.
  • `$.calOptions` - the options that will be passed through to hebcal library functions. This is controlled via the JSON config file, CLI arguments, and certain config-altering functions which can be called from the template itself.
  • `$.configSource` - the name of the JSON config file, or else the empty string if the compiled default config was used.
  • `$.language` - the name of the language to be used.
  • `$.dateRange` - the [daterange.DateRange] implied or specified by the command line arguments.
  • `$.tz` - the time.Location of the configured city.
  • `$.location` - a zmanim.Location configuring which place to calculate zmanim and holidays for. This can be customized in the JSON config via `city`, `geo.lat`, `geo.lon`, `timezone`, and `il` (whether the place is in Israel).
  • `$.z` - a zmanim.Zmanim object for calculating zmanim for a location.
  • `$.hdate.*` - HDateConsts, a map of constants for Hebrew dates.
  • `$.event.*` - EventConsts, a map of constants for categorizing events.
  • `$.sedra.*` - SedraConsts, a map of constants for parshas.
  • `$.time.*` - TimeConsts, a map of constants for the time package.

func CalOptionsFuncs added in v0.0.2

func CalOptionsFuncs(opts *hebcal.CalOptions) map[string]any

CalOptionsFuncs contains functions for Modifying opts from a template.

func CompareTimedEvents added in v0.0.2

func CompareTimedEvents(events []hebcal.TimedEvent) func(i, j int) bool

CompareTimedEvents allows hebcal.TimedEvents to be sorted. When clock times match, ties are broken by prioritizing Havdalah first and Candle lighting last. For example:

sort.Slice(events, CompareTimedEvents(events))

func DatePartsEqual

func DatePartsEqual(a, b time.Time) bool

DatePartsEqual compares the year, month, and day of two [time.Time]s and returns true if those parts all match.

func DayHasFlags added in v0.0.5

func DayHasFlags(
	opts *hebcal.CalOptions,
) func(d hdate.HDate, flags ...event.HolidayFlags) (bool, error)

DayHasFlags returns whether d has any of the given flags set on any of its events.

func DayIsShabbatOrYomTov added in v0.0.5

func DayIsShabbatOrYomTov(
	opts *hebcal.CalOptions,
) func(d hdate.HDate) (bool, error)

DayIsShabbatOrYomTov returns whether melacha is forbidden on the given day. It may be used by logic which determines candle lighting and havdalah times.

func DurationDiv added in v0.0.3

func DurationDiv(d time.Duration, divisor float64) (time.Duration, error)

DurationDiv divides d by the divisor.

func DurationMul added in v0.0.3

func DurationMul(d time.Duration, factor float64) time.Duration

DurationMul multiplies d by the given factor.

func EventsByFlags added in v0.0.5

func EventsByFlags(
	events []event.CalEvent,
	flags ...event.HolidayFlags,
) []event.CalEvent

EventsByFlags filters the events to just the ones which have any of the given flags set.

func ForDate added in v0.0.2

func ForDate(loc *zmanim.Location) func(d time.Time) (*zmanim.Zmanim, error)

ForDate takes a zmanim.Location and returns a constructor for new zmanim.Zmanim objects with different dates in that Location. Unlike zmanim.New which can panic and returns a struct, this constructor returns a struct pointer and an error.

func ForLocationDate added in v0.0.2

func ForLocationDate(
	loc *zmanim.Location,
	d time.Time,
) (*zmanim.Zmanim, error)

ForLocationDate creates a new zmanim.Zmanim object. Unlike zmanim.New which can panic and returns a struct, this constructor returns a struct pointer and an error.

func Ftoi

func Ftoi(f float64) int

Ftoi casts a float64 to an int.

func Hebcal added in v0.0.2

func Hebcal(
	opts *hebcal.CalOptions,
) func(dates ...hdate.HDate) ([]event.CalEvent, error)

Hebcal returns a slice of event.CalEvent like the original hebcal program. If no dates are provided, it uses the hebcal.CalOptions to select a date range. If one date is provided, only the events for that day are returned. If two dates, all the events between them are returned, including those on the end date.

func HebcalFuncs

func HebcalFuncs(opts *hebcal.CalOptions) map[string]any

HebcalFuncs builds a map of templating functions from a hebcal.CalOptions.

func Itof

func Itof(i int) float64

Itof casts an int to a float64.

func LocalizedParasha added in v0.0.5

func LocalizedParasha(hd hdate.HDate, il bool, lang string) string

LocalizedParasha returns the name of the Parasha for the given hdate.HDate, for the given lang. If the ordering in the Pentateuch is broken for a special holiday reading that week, it returns "Parashat hachag" or the translated equivalent.

func LookupCity added in v0.0.2

func LookupCity(city string) (*zmanim.Location, error)

LookupCity is the same as zmanim.LookupCity, except that we return an error if no match is found.

func MakeList added in v0.0.7

func MakeList(args ...any) []any

MakeList takes any number of arguments and returns a slice containing them.

func MakeMap added in v0.0.7

func MakeMap(kvs ...any) (map[string]any, error)

MakeMap takes a slice of keys and values and builds a map out of it. The keys must be strings.

func MergeFlags added in v0.0.6

func MergeFlags(flags ...event.HolidayFlags) event.HolidayFlags

MergeFlags combines the flags into a single mask.

func ParseFile

func ParseFile(
	files fs.FS,
	tmpl *template.Template,
	fpath string,
) (*template.Template, error)

ParseFile opens fpath from the files given and parses it into the tmpl.

func Reversed added in v0.0.7

func Reversed(s []any) []any

Reversed takes a slice and returns a copy of it with the items in reverse order.

func SecondsDuration

func SecondsDuration(secs float64) time.Duration

SecondsDuration converts float64 seconds into a time.Duration.

func Sedra added in v0.0.2

func Sedra(year int, il bool) *sedra.Sedra

Sedra returns a sedra.Sedra, which calculates a calendar of Parashot for that Hebrew year. The result can be used to query for the Parasha of any week in that year.

func SetDates added in v0.0.2

func SetDates(opts *hebcal.CalOptions) func(dates ...hdate.HDate) (any, error)

SetDates configures the opts with the provided dates. If no dates are provided, it does nothing. If one date is provided, it sets both opts.Start and opts.End to select that single date. If two dates, it checks that the first date is before the second, and sets opts.Start with the first and opts.End with the second. If more dates are provided, we return an error.

func SetEnd

func SetEnd(opts *hebcal.CalOptions) func(hd hdate.HDate) any

SetEnd tells template functions like hebcal and timedEvents the date through which events should be returned.

func SetFuncMap

func SetFuncMap(
	tmpl interface {
		Funcs(template.FuncMap) *template.Template
	},
	opts *hebcal.CalOptions,
) *template.Template

SetFuncMap loads the hebcalfmt templating functions into tmpl's FuncMap.

func SetIsHebrewYear

func SetIsHebrewYear(opts *hebcal.CalOptions) func(b bool) any

SetIsHebrewYear tells template functions like hebcal and timedEvents how to interpret the Year and NumYears fields.

func SetNumYears

func SetNumYears(opts *hebcal.CalOptions) func(n int) any

SetNumYears tells template functions like hebcal and timedEvents how many years of events should be returned.

func SetStart

func SetStart(opts *hebcal.CalOptions) func(hd hdate.HDate) any

SetStart tells template functions like hebcal and timedEvents the date from which events should be returned.

func SetYear

func SetYear(opts *hebcal.CalOptions) func(y int) any

SetYear tells template functions like hebcal and timedEvents the starting year from which events should be returned.

func TimedEvents

func TimedEvents(
	opts *hebcal.CalOptions,
) func(dates ...hdate.HDate) ([]hebcal.TimedEvent, error)

TimedEvents uses the given opts and returns just the [event.CalEvent]s which are [hebcal.TimedEvent]s. This is an easy way to pull zmanim from Hebcal, if you don't want to fully customize your zmanim list yourself. This interface is very similar to the one provided by the classic hebcal binary.

Unlike Hebcal, results are sorted by time, and certain ties are broken by putting Havdalah first and Candle lighting last.

If no dates are provided, it uses the hebcal.CalOptions to select a date range. If one date is provided, only the events for that day are returned. If two dates, all the events between them are returned, including those on the end date.

func Translate added in v0.0.5

func Translate(lang, s string) string

Translate attempts to translate s into lang using hebcal's translation dictionaries. If it fails, it returns s.

func ZmanimFuncs added in v0.0.2

func ZmanimFuncs(opts *hebcal.CalOptions) map[string]any

ZmanimFuncs builds a map of templating functions for zmanim using the given hebcal.CalOptions.

Types

This section is empty.

Jump to

Keyboard shortcuts

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