Module:Wp/khw/Iranian months table maker

From Wikimedia Incubator

Documentation for this module may be created at Module:Wp/khw/Iranian months table maker/doc

-- Iranian 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:Iranian months table maker|main|{{PAGENAME}}}}' into Iranian month templates; Or place '{{#invoke:Iranian months table maker|main|month_name}}' anywhere with replacing 'month_name' with Iranian month name.
]]--

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

local p = {}

local monthNames = {"حمل","ثور","جوزا","سرطان","اسد","سنبله","میزان","عقرب","قوس","جدی","دلو","حوت"}
local monthDays = {31,31,31,31,31,31,30,30,30,30,30,30}

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

  -- convert argument: if true convert returned number to Baluchi.
  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("bgn",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 .. "|<<]]"):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 .. "|>>]]"):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("bgn",n) .. " " .. selectedMonth .. "|" .. convertToFa("bgn",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