Skip to content

Commit

Permalink
breaking: refactor event into separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Dec 13, 2022
1 parent 71f3dfd commit 9e1e120
Show file tree
Hide file tree
Showing 23 changed files with 1,129 additions and 952 deletions.
43 changes: 43 additions & 0 deletions event/dafyomi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package event

import (
"strconv"

"github.com/hebcal/gematriya"
"github.com/hebcal/hebcal-go/dafyomi"
"github.com/hebcal/hebcal-go/hdate"
"github.com/hebcal/hebcal-go/locales"
)

type dafYomiEvent struct {
Date hdate.HDate
Daf dafyomi.Daf
}

func NewDafYomiEvent(hd hdate.HDate, daf dafyomi.Daf) CalEvent {
return dafYomiEvent{Date: hd, Daf: daf}
}

func (ev dafYomiEvent) GetDate() hdate.HDate {
return ev.Date
}

func (ev dafYomiEvent) Render(locale string) string {
name, _ := locales.LookupTranslation(ev.Daf.Name, locale)
if locale == "he" {
return name + " דף " + gematriya.Gematriya(ev.Daf.Blatt)
}
return name + " " + strconv.Itoa(ev.Daf.Blatt)
}

func (ev dafYomiEvent) GetFlags() HolidayFlags {
return DAF_YOMI
}

func (ev dafYomiEvent) GetEmoji() string {
return ""
}

func (ev dafYomiEvent) Basename() string {
return ev.Daf.String()
}
91 changes: 91 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Hebcal's event package provides an interface for calendar events.
package event

import "github.com/hebcal/hebcal-go/hdate"

// Hebcal - A Jewish Calendar Generator
// Copyright (c) 2022 Michael J. Radwin
// Derived from original C version, Copyright (C) 1994-2004 Danny Sadinoff
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

type HolidayFlags uint32

const (
// Chag, yontiff, yom tov
CHAG HolidayFlags = 1 << iota
// Light candles 18 minutes before sundown
LIGHT_CANDLES
// End of holiday (end of Yom Tov)
YOM_TOV_ENDS
// Observed only in the Diaspora (chutz l'aretz)
CHUL_ONLY
// Observed only in Israel
IL_ONLY
// Light candles in the evening at Tzeit time (3 small stars)
LIGHT_CANDLES_TZEIS
// Candle-lighting for Chanukah
CHANUKAH_CANDLES
// Rosh Chodesh, beginning of a new Hebrew month
ROSH_CHODESH
// Minor fasts like Tzom Tammuz, Ta'anit Esther, ...
MINOR_FAST
// Shabbat Shekalim, Zachor, ...
SPECIAL_SHABBAT
// Weekly sedrot on Saturdays
PARSHA_HASHAVUA
// Daily page of Talmud (Bavli)
DAF_YOMI
// Days of the Omer
OMER_COUNT
// Yom HaShoah, Yom HaAtzma'ut, ...
MODERN_HOLIDAY
// Yom Kippur and Tish'a B'Av
MAJOR_FAST
// On the Saturday before Rosh Chodesh
SHABBAT_MEVARCHIM
// Molad
MOLAD
// Yahrzeit or Hebrew Anniversary
USER_EVENT
// Daily Hebrew date ("11th of Sivan, 5780")
HEBREW_DATE
// A holiday that's not major, modern, rosh chodesh, or a fast day
MINOR_HOLIDAY
// Evening before a major or minor holiday
EREV
// Chol haMoed, intermediate days of Pesach or Sukkot
CHOL_HAMOED
// Mishna Yomi
MISHNA_YOMI
// Yom Kippur Katan, minor day of atonement on the day preceding each Rosh Chodesh
YOM_KIPPUR_KATAN
// Zemanim, halachic times of day
ZMANIM
// Daily page of Jerusalem Talmud (Yerushalmi)
YERUSHALMI_YOMI
)

type CalEvent interface {
GetDate() hdate.HDate // Holiday date of occurrence
Render(locale string) string // Description (e.g. "Pesach III (CH''M)")
GetFlags() HolidayFlags // Event flag bitmask
GetEmoji() string // Holiday-specific emoji
// Returns a simplified (untranslated) description for this event.
// For example, HolidayEvent supports "Erev Pesach" => "Pesach",
// and "Sukkot III (CH''M)" => "Sukkot".
// For many holidays the basename and the event description are
// the same.
Basename() string
}
14 changes: 7 additions & 7 deletions hebcal/event_test.go → event/event_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package hebcal_test
package event_test

import (
"testing"

"github.com/hebcal/hebcal-go/event"
"github.com/hebcal/hebcal-go/hdate"
"github.com/hebcal/hebcal-go/hebcal"
"github.com/hebcal/hebcal-go/sedra"
"github.com/stretchr/testify/assert"
)

func TestHolidayEvent_Basename(t *testing.T) {
ev := hebcal.HolidayEvent{Desc: "Sukkot III (CH''M)"}
ev := event.HolidayEvent{Desc: "Sukkot III (CH''M)"}
assert.Equal(t, "Sukkot", ev.Basename())
ev = hebcal.HolidayEvent{Desc: "Chanukah: 1 Candle"}
ev = event.HolidayEvent{Desc: "Chanukah: 1 Candle"}
assert.Equal(t, "Chanukah", ev.Basename())
ev = hebcal.HolidayEvent{Desc: "Chanukah: 7 Candles"}
ev = event.HolidayEvent{Desc: "Chanukah: 7 Candles"}
assert.Equal(t, "Chanukah", ev.Basename())
}

func TestHebrewDateEvent(t *testing.T) {
hd := hdate.New(5781, hdate.Sivan, 3)
ev := hebcal.NewHebrewDateEvent(hd)
ev := event.NewHebrewDateEvent(hd)
assert.Equal(t, "3 Sivan 5781", ev.Basename())
assert.Equal(t, "3rd of Sivan, 5781", ev.Render("en"))
assert.Equal(t, "ג׳ סִיוָן תשפ״א", ev.Render("he"))
Expand All @@ -34,7 +34,7 @@ func TestParshaEvent_Render(t *testing.T) {
Chag: false,
}
hd := hdate.New(5783, hdate.Tamuz, 26)
ev := hebcal.NewParshaEvent(hd, parsha, false)
ev := event.NewParshaEvent(hd, parsha, false)
assert.Equal(t, "Parashat Matot-Masei", ev.Render("en"))
assert.Equal(t, "Глава Матот-Масей", ev.Render("ru"))
assert.Equal(t, "פָּרָשַׁת מַּטּוֹת־מַסְעֵי", ev.Render("he"))
Expand Down
53 changes: 53 additions & 0 deletions event/hdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package event

import (
"strconv"

"github.com/hebcal/gematriya"
"github.com/hebcal/hebcal-go/hdate"
"github.com/hebcal/hebcal-go/locales"
"github.com/hebcal/hebcal-go/number"
)

type hebrewDateEvent struct {
Date hdate.HDate
}

func (ev hebrewDateEvent) GetDate() hdate.HDate {
return ev.Date
}

func (ev hebrewDateEvent) Render(locale string) string {
hd := ev.Date
enMonthName := hd.MonthName("en")
switch locale {
case "he":
return gematriya.Gematriya(hd.Day()) + " " + hd.MonthName("he") + " " + gematriya.Gematriya(hd.Year())
case "", "en", "sephardic", "ashkenazi",
"ashkenazi_litvish", "ashkenazi_poylish", "ashkenazi_standard":
return number.Ordinal(hd.Day()) + " of " + enMonthName +
", " + strconv.Itoa(hd.Year())
case "es":
monthName, _ := locales.LookupTranslation(enMonthName, locale)
return strconv.Itoa(hd.Day()) + "º " + monthName + " " + strconv.Itoa(hd.Year())

}
monthName, _ := locales.LookupTranslation(enMonthName, locale)
return strconv.Itoa(hd.Day()) + " " + monthName + " " + strconv.Itoa(hd.Year())
}

func (ev hebrewDateEvent) GetFlags() HolidayFlags {
return HEBREW_DATE
}

func (ev hebrewDateEvent) GetEmoji() string {
return ""
}

func (ev hebrewDateEvent) Basename() string {
return ev.Date.String()
}

func NewHebrewDateEvent(hd hdate.HDate) CalEvent {
return hebrewDateEvent{Date: hd}
}
71 changes: 71 additions & 0 deletions event/holiday.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package event

import (
"regexp"

"github.com/hebcal/hebcal-go/hdate"
"github.com/hebcal/hebcal-go/locales"
)

// HolidayEvent represents a built-in holiday like Pesach, Purim or Tu BiShvat
type HolidayEvent struct {
Date hdate.HDate // Holiday date of occurrence
Desc string // Description (e.g. "Pesach III (CH''M)")
Flags HolidayFlags // Event flag bitmask
Emoji string // Holiday-specific emoji
CholHaMoedDay int // used only for Pesach and Sukkot
ChanukahDay int // used only for Chanukah
}

func (ev HolidayEvent) GetDate() hdate.HDate {
return ev.Date
}

func (ev HolidayEvent) Render(locale string) string {
if (ev.Flags & ROSH_CHODESH) != 0 {
rchStr, _ := locales.LookupTranslation("Rosh Chodesh", locale)
monthStr, _ := locales.LookupTranslation(ev.Desc[13:], locale)
return rchStr + " " + monthStr
}
str, _ := locales.LookupTranslation(ev.Desc, locale)
return str
}

func (ev HolidayEvent) GetFlags() HolidayFlags {
return ev.Flags
}

func (ev HolidayEvent) GetEmoji() string {
if ev.Emoji != "" {
return ev.Emoji
}
switch ev.Flags {
case SPECIAL_SHABBAT:
return "🕍"
case ROSH_CHODESH:
return "🌒"
case SHABBAT_MEVARCHIM, YOM_KIPPUR_KATAN | MINOR_FAST:
return ""
default:
return "✡️"
}
}

var regexes = []*regexp.Regexp{
regexp.MustCompile(` \d{4}$`),
regexp.MustCompile(` \(CH''M\)$`),
regexp.MustCompile(` \(observed\)$`),
regexp.MustCompile(` \(Hoshana Raba\)$`),
regexp.MustCompile(` [IV]+$`),
regexp.MustCompile(`: \d Candles?$`),
regexp.MustCompile(`: 8th Day$`),
regexp.MustCompile(`^Erev `),
}

func (ev HolidayEvent) Basename() string {
str := ev.Desc
for _, regex := range regexes {
str = regex.ReplaceAllString(str, "")
}
return str
}
43 changes: 43 additions & 0 deletions event/molad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package event

import (
"fmt"

"github.com/hebcal/hebcal-go/hdate"
)

type moladEvent struct {
Date hdate.HDate
Molad hdate.Molad
MonthName string
}

func NewMoladEvent(date hdate.HDate, molad hdate.Molad, monthName string) CalEvent {
return moladEvent{
Date: date,
Molad: molad,
MonthName: monthName,
}
}

func (ev moladEvent) GetDate() hdate.HDate {
return ev.Date
}

func (ev moladEvent) Render(locale string) string {
return fmt.Sprintf("Molad %s: %s, %d minutes and %d chalakim after %d:00",
ev.MonthName, ev.Molad.Date.Weekday().String()[0:3],
ev.Molad.Minutes, ev.Molad.Chalakim, ev.Molad.Hours)
}

func (ev moladEvent) GetFlags() HolidayFlags {
return MOLAD
}

func (ev moladEvent) GetEmoji() string {
return ""
}

func (ev moladEvent) Basename() string {
return "Molad " + ev.MonthName
}
Loading

0 comments on commit 9e1e120

Please sign in to comment.