Module:FRCalendar

local FRCalendar = {} --- imported libraries -- Parses invocation and template parameters, trims whitespace, and removes blanks. local getArgs = require('Dev:Arguments').getArgs -- Smarter boolean logic local yesno = require( 'Dev:Yesno' ) -- Hash table of 2400 year names. local getYearName = require ('Module:Years').yearName -- Hash table of five moon phases, from full moon to full moon. local phaseSymbols = {"🌕", "🌗", "🌑", "🌓", "🌕" } -- Hash table of img tags local phaseImg = { tostring(mw.html.create('img')     :attr('src','https://vignette.wikia.nocookie.net/forgottenrealms/images/0/05/Moon5-full.png/revision/latest')      :attr('alt', phaseSymbols[1]):attr('width','16'):done), tostring(mw.html.create('img')     :attr('src','https://vignette.wikia.nocookie.net/forgottenrealms/images/3/33/Moon7-3rdQ.png/revision/latest')      :attr('alt', phaseSymbols[2]):attr('width','16'):done), tostring(mw.html.create('img')     :attr('src','https://vignette.wikia.nocookie.net/forgottenrealms/images/9/95/Moon1-new.png/revision/latest')      :attr('alt', phaseSymbols[3]):attr('width','16'):done), tostring(mw.html.create('img')     :attr('src','https://vignette.wikia.nocookie.net/forgottenrealms/images/e/e0/Moon3-1stQ.png/revision/latest')      :attr('alt', phaseSymbols[4]):attr('width','16'):done), tostring(mw.html.create('img')     :attr('src','https://vignette.wikia.nocookie.net/forgottenrealms/images/0/05/Moon5-full.png/revision/latest')      :attr('alt', phaseSymbols[5]):attr('width','16'):done) } -- Hash table of six special days and what month of the calendar should be generated for them. local monthSpecial = { ['Midwinter']        = 'Hammer', ['Greengrass']       = 'Tarsakh', ['Midsummer']        = 'Flamerule', ['Shieldmeet']       = 'Flamerule', ['Highharvestide']   = 'Eleint', ['Feast of the Moon'] = 'Uktar' } -- The months of the year, as numbers. You can place alternate spellings or common aliases here. local monthNumber = { ['Hammer']    = 1, ['Alturiak']  = 2, ['Ches']      = 3, ['Tarsakh']   = 4, ['Mirtul']    = 5, ['Kythorn']   = 6, ['Flamerule'] = 7, ['Eleasis']   = 8, ['Eleasias']  = 8, ['Eleint']    = 9, ['Marpenoth'] = 10, ['Uktar']     = 11, ['Nightal']   = 12 } -- But do not alter this sequence. local months = {'Hammer','Alturiak','Ches','Tarsakh','Mirtul','Kythorn', 'Flamerule','Eleasis','Eleint','Marpenoth','Uktar','Nightal'}

local function addPhases(phases, phaseList) local _phases = mw.text.split(phaseList, ',', true) for phaseIndex,dayNum in ipairs(_phases) do       phases['D'..dayNum] = phaseSymbols[phaseIndex] end end

local function buildPhaseList(FRmonth, yearsSinceLeap) local f = mw.getCurrentFrame local currMonth = monthNumber[FRmonth] local currYear = 2004 + yearsSinceLeap local prevMonth, prevYear, nextMonth, nextYear = 0, 0, 0, 0 local currPhaseDate = tostring(currYear)..'-'..currMonth..'-15' local currPhaseList = f:expandTemplate({title = 'Selune phase',                                            args  = {mode='fivePhases', currPhaseDate}}) if currMonth - 1 < 1 then prevMonth = 12 prevYear = currYear - 1 else prevMonth = currMonth - 1 prevYear = currYear end local prevPhaseDate = tostring(prevYear)..'-'..prevMonth..'-15' local prevPhaseList = f:expandTemplate({title = 'Selune phase',                                            args  = {mode='fivePhases', prevPhaseDate}}) if currMonth + 1 > 12 then nextMonth = 1 nextYear = currYear + 1 else nextMonth = currMonth + 1 nextYear = currYear end local nextPhaseDate = tostring(nextYear)..'-'..nextMonth..'-15' local nextPhaseList = f:expandTemplate({title = 'Selune phase',                                            args  = {mode='fivePhases', nextPhaseDate}}) local phases = {} addPhases(phases, prevPhaseList) addPhases(phases, currPhaseList) addPhases(phases, nextPhaseList) return phases, months[prevMonth], months[nextMonth] end

local function addSpecial(rows, day, phase) local altLink = '|'..day..(phase or '') rows:tag('tr') :tag('td'):attr('colspan','10'):wikitext(..day..altLink..):done :done end local function getPrevSpecial(FRmonth, phaseDay, phases, leapYear) local rows = mw.html.create('')

if FRmonth == 'Alturiak' then phaseDay = phaseDay - 1 addSpecial(rows, 'Midwinter', phases['D'..phaseDay]) elseif FRmonth == 'Mirtul' then phaseDay = phaseDay - 1 addSpecial(rows, 'Greengrass', phases['D'..phaseDay]) elseif FRmonth == 'Eleasis' then phaseDay = phaseDay - 1 addSpecial(rows, 'Midsummer', phases['D'..phaseDay]) if leapYear then phaseDay = phaseDay - 1 addSpecial(rows, 'Shieldmeet', phases['D'..phaseDay]) end elseif FRmonth == 'Marpenoth' then phaseDay = phaseDay - 1 addSpecial(rows, 'Highharvestide', phases['D'..phaseDay]) elseif FRmonth == 'Nightal' then phaseDay = phaseDay - 1 addSpecial(rows, 'Feast of the Moon', phases['D'..phaseDay]) end phaseDay = phaseDay - 30 -- jump to the previous month if phaseDay < 0 then phaseDay = phaseDay + 1461 -- jump to the last month of the 4-year cycle end return phaseDay, tostring(rows) end

local function getNextSpecial(FRmonth, phaseDay, phases, leapYear) local rows = mw.html.create('') phaseDay = phaseDay + 30 -- Jump to the next month if phaseDay >= 1461 then phaseDay = phaseDay - 1461 -- jump to the first month of the 4-year cycle end

if FRmonth == 'Hammer' then addSpecial(rows, 'Midwinter', phases['D'..phaseDay]) phaseDay = phaseDay + 1 elseif FRmonth == 'Tarsakh' then addSpecial(rows, 'Greengrass', phases['D'..phaseDay]) phaseDay = phaseDay + 1 elseif FRmonth == 'Flamerule' then addSpecial(rows, 'Midsummer', phases['D'..phaseDay]) phaseDay = phaseDay + 1 if leapYear then addSpecial(rows, 'Shieldmeet', phases['D'..phaseDay]) phaseDay = phaseDay + 1 end elseif FRmonth == 'Eleint' then addSpecial(rows, 'Highharvestide', phases['D'..phaseDay]) phaseDay = phaseDay + 1 elseif FRmonth == 'Uktar' then addSpecial(rows, 'Feast of the Moon', phases['D'..phaseDay]) phaseDay = phaseDay + 1 end return phaseDay, tostring(rows) end

local function getMonthHtml(FRmonth, phaseDay, phases) local altLink = '' local rows = mw.html.create('') for tenday = 0, 2 do       rows:tag('tr') for day = 1, 10 do           altLink = phases['D'..phaseDay] or tostring(day + 10*tenday) rows:tag('td'):wikitext(..altLink..):done phaseDay = phaseDay + 1 end rows:done end return tostring(rows) end

function FRCalendar.ThreeMonthsArgs( args ) local f = mw.getCurrentFrame local FRdate = args[1] local FRyear = tonumber(args[2]) local FRday, FRmonth = string.match(FRdate, '(%d+)%s+(%a+)') if FRday == nil then FRday  = FRdate == 'Shieldmeet' and 32 or 31 FRmonth = monthSpecial[FRdate] else FRday  = tonumber(FRday) FRmonth = tostring(FRmonth) end local yearsSinceLeap = ((FRyear % 4) + 4) % 4 local leapYear = (yearsSinceLeap == 0) if FRdate == 'Shieldmeet' and not leapYear then return mw.html.create('p'):css('color','red') :wikitext('Shieldmeet only occurs on leap years.'):done end local daysSinceLeap = f:expandTemplate({title = 'Date number', args = {FRdate, FRyear}}) local phaseTable, prevMonth, nextMonth = buildPhaseList(FRmonth, yearsSinceLeap)

local title = getYearName(FRyear) == '' and 'Calendar of Harptos' or getYearName(FRyear)

local currPhaseDay = daysSinceLeap - FRday + 1 local prevPhaseDay, prevSpecials = getPrevSpecial(FRmonth, currPhaseDay, phaseTable, leapYear) local nextPhaseDay, nextSpecials = getNextSpecial(FRmonth, currPhaseDay, phaseTable, leapYear)

local output = mw.html.create('div'):addClass('RoMcontainer') :tag('div'):addClass('RoMtop'):wikitext(''):done :tag('div'):addClass('RoMcenter') :tag('div'):addClass('RoMmiddle') :tag('table'):addClass('rollOfMonths') :tag('tr') :tag('th'):attr('colspan','10') :wikitext(title) :done :done :tag('tr') :tag('th'):attr('colspan','10') :wikitext(''..FRyear..' DR') :done :done :tag('tr') :tag('th'):attr('colspan','10') :wikitext(prevMonth) :done :done :wikitext(getMonthHtml(prevMonth, prevPhaseDay, phaseTable)) :wikitext(prevSpecials) :tag('tr') :tag('th'):attr('colspan','10') :wikitext(FRmonth) :done :done :wikitext(getMonthHtml(FRmonth, currPhaseDay, phaseTable)) :wikitext(nextSpecials) :tag('tr') :tag('th'):attr('colspan','10') :wikitext(nextMonth) :done :done :wikitext(getMonthHtml(nextMonth, nextPhaseDay, phaseTable)) :done :done :done :tag('div'):addClass('RoMbottom'):wikitext(''):done :allDone return tostring(output) end

function FRCalendar.ThreeMonths( frame ) local args = getArgs(frame) return FRCalendar.ThreeMonthsArgs( args ) end

return FRCalendar