לדלג לתוכן

יחידה:פרשת השבוע

מתוך המכלול, האנציקלופדיה היהודית

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:פרשת השבוע/תיעוד

local p = {}
local dateModule = require('Module:תאריך')
local hebDate = require('Module:תאריך עברי')
local gimatria = require('Module:גימטריה')
local listOfParashot = require('Module:פרשת השבוע/רשימה')

local numberOfYear = 0
local dateForCurrentRoshHashaba = nil
local dateForNextRoshHashana = nil
local typeOfYear = 0
local leapYear = false

local function convertDate(date)
    mw.log("Attempting to convert date:" .. date)
    local success, hebrewDate = pcall(hebDate.fromhebrew, mw.getCurrentFrame():newChild { args = { date } })
    if success then
        mw.log("Converted to Hebrew date: " .. hebrewDate)
        return hebrewDate
    else
        mw.log("Conversion failed for date: " .. date)
        --error("Couldn't convert: " .. date)
    end
end

local function isLeapYear(hebrewYear)
    return (((hebrewYear * 7) + 1) % 19) < 7
end

local function getNextYear()
    local nextYear = numberOfYear + 1
    if nextYear < 1000 then
        return gimatria.mispar_lotiot(nextYear)
    else
        local thousands = gimatria.mispar_lotiot(tonumber(mw.ustring.sub(tostring(nextYear), 1, 1)))
        local hundreds = gimatria.mispar_lotiot(tonumber(mw.ustring.sub(tostring(nextYear), 2)))
        return thousands .. "'" .. mw.ustring.sub(hundreds, 1, -2) .. '"' .. mw.ustring.sub(hundreds, -1)
    end
end

local function getWeekday(gregorianDate)
    -- Parse the Gregorian date
    local day, month, year = gregorianDate:match("(%d+) ב(%a+) (%d+)")
    if not (day and month and year) then
        mw.log("Failed to parse Gregorian date: " .. gregorianDate)
        return nil
    end

    -- Convert month name to number
    local monthNumbers = {
        January = 1,
        February = 2,
        March = 3,
        April = 4,
        May = 5,
        June = 6,
        July = 7,
        August = 8,
        September = 9,
        October = 10,
        November = 11,
        December = 12
    }
    local monthNumber = monthNumbers[month]
    if not monthNumber then
        mw.log("Invalid month name: " .. month)
        return nil
    end

    -- Create a time table
    local timeTable = os.time({ year = tonumber(year), month = monthNumber, day = tonumber(day) })

    -- Get the weekday (1 = Sunday, 2 = Monday, etc.)
    return os.date("*t", timeTable).wday
end

local function getTypeOfYear()
    local currentWeekday = getWeekday(dateForCurrentRoshHashaba)
    local nextWeekday = getWeekday(dateForNextRoshHashana)
    mw.log("current: " .. currentWeekday)
    mw.log("next: " .. nextWeekday)

    if not (currentWeekday and nextWeekday) then
        mw.log("Failed to get weekdays")
        return
    end

    local mod = (nextWeekday - currentWeekday + 7) % 7
    if leapYear then
        mod = mod - 2
    end
    typeOfYear = mod
    mw.log("typeOfYear: " .. typeOfYear)
end

function p._calculateYearSettings(year)
    mw.log("calculating year settings" .. year)
    if (mw.ustring.find(year, "'")) then
        numberOfYear = gimatria.gimatria(mw.ustring.sub(year, 1, 2)) * 1000 + gimatria.gimatria(mw.ustring.sub(year, 2))
    else
        numberOfYear = gimatria.gimatria(year)
    end
    leapYear = isLeapYear(numberOfYear)
    mw.log("leapYear: ", leapYear)
    dateForCurrentRoshHashaba = convertDate("א' בתשרי " .. year)
    dateForNextRoshHashana = convertDate("א' בתשרי " .. getNextYear())
    getTypeOfYear()
    for i = 1, #listOfParashot do
        if listOfParashot[i] == "ויקהל" and (not leapYear and (dateForCurrentRoshHashaba ~= 5 or typeOfYear ~= 3)) then
            listOfParashot[i] = "ויקהל " .. table.remove(listOfParashot, i + 1)
        end

        if listOfParashot[i] == "תזריע" and not leapYear then
            listOfParashot[i] = "תזריע " .. table.remove(listOfParashot, i + 1)
        end

        if listOfParashot[i] == "אחרי מות" then
            if not leapYear then
                listOfParashot[i] = "אחרי מות " .. table.remove(listOfParashot, i + 1)
            elseif leapYear then
                table.insert(listOfParashot, i + 1, "שבת חול המועד פסח")
            end
        end

        if listOfParashot[i] == "בהר" and not leapYear then
            listOfParashot[i] = "בהר " .. table.remove(listOfParashot, i + 1)
        end

        if listOfParashot[i] == "מטות" and (not leapYear or dateForCurrentRoshHashaba ~= 5) then
            listOfParashot[i] = "מטות " .. table.remove(listOfParashot, i + 1)
        end
    end
    mw.logObject(table.concat(listOfParashot, ", " ))
end

return p