-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
breaking: refactor event into separate package
- Loading branch information
Showing
23 changed files
with
1,129 additions
and
952 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.