ماژول:Gregorian months table maker

توضیحات این پودمان می‌تواند در ماژول:Gregorian months table maker/توضیحات قرار گیرد.

-- Gregorian months table maker
-- Used in templates such as {{ژانویه}}
-- Written by Alireza Eskandarpour Shoferi in Lua
--
-- Distributed under the terms of the CC BY-SA 4.0

--[[ INSTRUCTIONS
1. Place '{{#invoke:Gregorian months table maker|main|{{PAGENAME}}}}' into Gregorian month templates; Or place '{{#invoke:Gregorian months table maker|main|month_name}}' anywhere with replacing 'month_name' with Gregorian month name.
]]--

-- Load necessary modules.
local convertToFa = require("Module:Numeral converter").convert

local p = {}

local monthNames = {"ژانویه","فوریه","مارس","آوریل","مه","ژوئن","ژوئیه","آگوست","سپتامبر","اکتبر","نوامبر","دسامبر"}
local monthDays = {31,29,31,30,31,30,31,31,30,31,30,31}

function p.main(frame)
  local selectedMonth = frame.args[1]

  -- convert argument: if true convert returned number to Persian.
  local function getMonthNumber(givenMonth, convert)
    convert = convert or false

    for k,v in pairs(monthNames) do
      if v == givenMonth then
        if convert then
          return convertToFa("fa",k)
        end
        return k
      end
    end
  end

  local function getPreviousMonthName(givenMonth)
    local previousMonthName
    for _,v in pairs(monthNames) do
      if(v == givenMonth) then
        if type(previousMonthName) == "nil" then
          return monthNames[#monthNames]
        end
        return previousMonthName
      end
      previousMonthName = v
    end
  end

  local function getNextMonthName(givenMonth)
    local _,v = next(monthNames,getMonthNumber(givenMonth))
    if type(v) == "nil" then
      return monthNames[1]
    end
    return v
  end

  if type(selectedMonth) == "string" and type(monthNames[getMonthNumber(selectedMonth)]) ~= "nil" then
    local wrapper = mw.html.create("table")
    :addClass("toccolours")
    :css("float","left")
    :css("margin-right","1em")
    :css("text-align","center")

    local header = mw.html.create("tr")
    :css("background-color","#ccccff")
    :css("text-align","center")

    -- Modify disambiguation pages to the right month article pages.
    local link = nil
    local preLink = nil
    local nexLink = nil
    for _,m in pairs({"مه"}) do
      if m == selectedMonth then
        link = m .. " (ماه)"
      elseif m == getPreviousMonthName(selectedMonth) then
        preLink = m .. " (ماه)"
      elseif m == getNextMonthName(selectedMonth) then
        nexLink = m .. " (ماه)"
      end
    end

    link = link or selectedMonth
    preLink = preLink or getPreviousMonthName(selectedMonth)
    nexLink = nexLink or getNextMonthName(selectedMonth)

    header:node(mw.html.create("td"):attr("colspan","2"):wikitext("[[" .. preLink .. "|" .. getPreviousMonthName(selectedMonth) .. "]]"):done())
    header:node(mw.html.create("td"):attr("colspan","3"):wikitext("'''[[" .. link .. "|" .. selectedMonth .. "]]'''"):done())
    header:node(mw.html.create("td"):attr("colspan","2"):wikitext("[[" .. nexLink .. "|" .. getNextMonthName(selectedMonth) .. "]]"):done())

    local underHeader = mw.html.create("tr")
    :css("background-color","#ccccff")
    :css("text-align","center")
    for _ = 1, 7 do
      local underHeaderContent = mw.html.create("td")
      :css("width","14%")
      underHeader:node(underHeaderContent:done())
    end

    local container = mw.html.create(nil)
    local n = 1
    for _ = 1, 5 do
      if n < monthDays[getMonthNumber(selectedMonth)] then
        local numberRow = mw.html.create("tr")
        for _=1, 7 do
          if n > monthDays[getMonthNumber(selectedMonth)] then
            break
          end
          numberRow:node(mw.html.create("td"):wikitext("[[" .. convertToFa("fa",n) .. " " .. selectedMonth .. "|" .. convertToFa("fa",n) .. "]]"):done())
          n = n +1
        end
        container:node(numberRow:allDone())
      end
    end

    local footer = mw.html.create("tr")
    :css("background-color","lightblue")
    footer:node(mw.html.create("td"):attr("colspan","7"):attr("align","center"):attr("bgcolor","#ccccff"):wikitext(" * * [[میلادی تقویم]] * * "):done())

    wrapper:node(header)
    wrapper:node(underHeader)
    wrapper:node(container)
    wrapper:node(footer)
    return tostring(wrapper)

  else
    error("آرگومان اول ونه رشته یا ماهِ نوم جه بائه")
  end

end

return p