local p = {} local en = require('মডিউল:ConvertDigitRevert').bn_to_en local bn = require('মডিউল:ConvertDigit')._main local getArgs = require('Module:Arguments').getArgs local months = {     [1] = "বৈশাখ", [2] = "জ্যৈষ্ঠ", [3] = "আষাঢ়", [4] = "শ্রাবণ",     [5] = "ভাদ্র", [6] = "আশ্বিন", [7] = "কার্তিক", [8] = "অগ্রহায়ণ",     [9] = "পৌষ", [10] = "মাঘ", [11] = "ফাল্গুন", [12] = "চৈত্র" } local bn_date = require("মডিউল:বাংলা তারিখ/খেলাঘর")._main  local function dmy(format)     return bn_date(format) end  function p.getCalender(bndmy, bnmy, link)     local b_month, b_year = bnmy:match("(.+) (%d%d?%d?%d?)")     local l_day     if link then          local m = mw.title.getCurrentTitle().text         l_day = mw.ustring.match(m, '^[০১২৩৪৫৬৭৮৯]+') or         b_month = mw.ustring.gsub(m, '^[০১২৩৪৫৬৭৮৯]+%s*', )         b_year = en(dmy("y"))     end     local h_d, h_m, h_y = bndmy:match("(%d%d?) (.+) (%d%d?%d?%d?)")     local h_day = tonumber(h_d)      local function isLeapYear(y)         y = tonumber(y)         return (y % 4 == 0 and y % 100 ~= 0) or (y % 400 == 0)     end     local m_index = {         ["বৈশাখ"] = 1, ["জ্যৈষ্ঠ"] = 2, ["আষাঢ়"] = 3, ["শ্রাবণ"] = 4,         ["ভাদ্র"] = 5, ["আশ্বিন"] = 6, ["কার্তিক"] = 7, ["অগ্রহায়ণ"] = 8,         ["পৌষ"] = 9, ["মাঘ"] = 10, ["ফাল্গুন"] = 11, ["চৈত্র"] = 12     }     local month_order = {         "বৈশাখ", "জ্যৈষ্ঠ", "আষাঢ়", "শ্রাবণ", "ভাদ্র", "আশ্বিন",         "কার্তিক", "অগ্রহায়ণ", "পৌষ", "মাঘ", "ফাল্গুন", "চৈত্র"     }      local starting_day = {         ['বৈশাখ'] = { 4, 14, 31 }, ['জ্যৈষ্ঠ'] = { 5, 15, 31 }, ['আষাঢ়'] = { 6, 15, 31 },         ['শ্রাবণ'] = { 7, 16, 31 }, ['ভাদ্র'] = { 8, 16, 31 }, ['আশ্বিন'] = { 9, 16, 31 },         ['কার্তিক'] = { 10, 16, 30 }, ['অগ্রহায়ণ'] = { 11, 15, 30 }, ['পৌষ'] = { 12, 16, 30 },         ['মাঘ'] = { 1, 15, 30 }, ['ফাল্গুন'] = { 2, 14, 29 }, ['চৈত্র'] = { 3, 15, 30 }     }     local plus = 593     if m_index[b_month] and m_index[b_month] > 9 then         plus = 594     end              local g_year = tonumber(b_year) + plus     if isLeapYear(g_year) then         starting_day["ফাল্গুন"][3] = 30     end      local function getMonthIndex(month)         for i, v in ipairs(month_order) do             if v == month then return i end         end     end      local function getPrevNextMonth(month, year)         local idx = getMonthIndex(month)         local prev_idx = idx == 1 and 12 or idx - 1         local next_idx = idx == 12 and 1 or idx + 1         local prev_year = idx == 1 and tonumber(year) - 1 or tonumber(year)         local next_year = idx == 12 and tonumber(year) + 1 or tonumber(year)         return {             prev = { month = month_order[prev_idx], year = prev_year },             next = { month = month_order[next_idx], year = next_year }         }     end      local function next_leap_year(year)         local candidate         if year % 4 == 0 then             candidate = year + 4         else             candidate = year + (4 - year % 4)         end         if candidate % 100 == 0 and candidate % 400 ~= 0 then             candidate = candidate + 4         end         return candidate - year     end      if l_day == '৩০' and b_month == 'ফাল্গুন' then         b_year = b_year + next_leap_year(g_year)         g_year = b_year + plus         h_day = 30         starting_day["ফাল্গুন"][3] = 30     end      local s_m, s_d, t_d = unpack(starting_day[b_month])     local wday = os.date("*t", os.time{ year = g_year, month = s_m, day = s_d }).wday     local first_day_index = (wday + 1) % 7 + 1     local total_days = t_d     local class = '"'..'wikitable'..'"'      local output = '{| class='..class..' '..'style="text-align: center; border-collapse: collapse; margin:auto; display:flex; justify-content: center;background-color: transparent; float:right;"\n'     local center = "" .. b_month .. " "           if link then         local nav = getPrevNextMonth(b_month, b_year)         local function exist(page)             return mw.title.new(page).exists         end         local prev = bn(l_day or "১").." ".. nav.prev.month         local next = bn(l_day or "১").." ".. nav.next.month         if not exist(prev) then             prev = bn(tostring(tonumber(en(l_day or "১"))-1)).." ".. nav.prev.month         elseif not exist(next) then             next = bn(tostring(tonumber(en(l_day or "১"))-1)).." ".. nav.next.month         end         prev = "<<"         next = ">>"         output = output .. '|- style="text-align:center; "\n| colspan="7" | ' .. prev .. '   ' .. center .. '   ' .. next .. '\n'     else                output = output .. '|- style="text-align:center;"\n| colspan="7" | '..center.." "..bn(b_year)..'\n'     end      output = output .. '|-\n'     output = output .. '! scope="col" |শুক্র\n! scope="col" |শনি\n! scope="col" |রবি\n! scope="col" |সোম\n! scope="col" |মঙ্গল\n! scope="col" |বুধ\n! scope="col" |বৃহ\n'      local rows, cols = 5, 7     local total_cells = rows * cols     local grid = {}     for i = 1, total_cells do         grid[i] = ""     end      for day = 1, total_days do         local pos = (first_day_index + day - 2) % total_cells + 1         grid[pos] = tostring(day)     end      for row = 0, rows - 1 do         output = output .. "|-\n"         for col = 0, cols - 1 do             local idx = row * cols + col + 1             local cell = grid[idx]             local cell_display = " "             if cell ~= "" then                 local day_bn = bn(cell)                 if link then                     cell_display = "" .. day_bn .. ""                 else                     cell_display = day_bn                 end                 if tonumber(en(cell)) == tonumber(en(tostring(h_day))) and b_month == h_m and tonumber(b_year) == tonumber(h_y) then                     if col == 0 then                          output = output .. '|style="background-color:gold;" |' .. cell_display                     else                         output = output .. 'style="background-color:gold;" |' .. cell_display                     end                 else                     output = output .. '|' .. cell_display                 end             else                 output = output .. "|  "             end             if col < cols - 1 then                 output = output .. " || "             end         end         output = output .. "\n"     end      if link then         output = output .. '|- style="text-align: center;"\n'         output = output .. '| colspan="7" | ' .. bn(tostring(b_year)) .. '\n'     end      output = output .. "|}\n"     return output end  function p.calendar(frame)     local args = getArgs(frame)     local y = args[1] or args["year"] or os.date("%Y")     y = tonumber(en(y))     local output = '<table style="border-collapse: collapse; width: 500px; font-size: 90%; text-align: center;">'     local row_open = false     for i = 1, 12 do         if (i - 1) % 3 == 0 then             if row_open then output = output .. '</tr>' end             output = output .. ''             row_open = true         end         local month = months[i] .. ' ' .. y         output = output .. ''         output = output .. p.getCalender("1 " .. months[i] .. " " .. y, months[i] .. " " .. y, false)         output = output .. ''     end     if row_open then output = output .. '' end     output = output .. '</table>'     return output end  function p.main(frame)     local args = getArgs(frame)     local bndmy = args[2] or args["bndmy"] or dmy("d m y")     local bnmy = args[1] or args["bnmy"] or dmy("m y")     -- convert to English numerals for processing     bndmy = en(bndmy)     bnmy = en(bnmy)     local link = args.link == "yes" or args[3] == "yes"     return p.getCalender(bndmy, bnmy, link) end  return p