Module:Years

-- This module loads a big hashtable for translating a Dalereckoning year -- to the name of the year as given to us by Augathra the Mad and Alaundo. local RoY = {} --- imported libraries -- Parses invocation and template parameters, trims whitespace, and removes blanks. local getArgs = require('Dev:Arguments').getArgs

local yearNames = mw.loadData( 'Module:Years/data2' ) local offset = 701 --The offset needed to convert a calendar year to a sequence number.

function RoY._yearName( year )

return yearNames[year + offset][2] or '' end

function RoY.yearName( frame ) local args = getArgs(frame) local year = tonumber(args[1] or 0) return RoY._yearName(year) end

function RoY.randomYear math.randomseed(mw.language.new( 'en' ):formatDate('U')) local randomYearNum = math.random(-700,1600)

return {randomYearNum, RoY._yearName(randomYearNum) or ''} end

function RoY._relatedYear( year ) local yearsSinceLeap = ((year % 4) + 4) % 4 local randomYr = RoY.randomYear[1]

local relatedYr = randomYr - (randomYr % 4) + yearsSinceLeap if relatedYr > 1600 then relatedYr = relatedYr - 4 end return {relatedYr, RoY._yearName(relatedYr) or ''} end

function RoY.RelatedYear( frame ) local args = getArgs(frame) local relatedYear = RoY._relatedYear( args[1] ) return ..relatedYear[2].. end

function RoY.yearlinkName( frame ) local args = getArgs(frame) local year = tonumber(args[1] or 0) local formattedYear = string.gsub(year,'-','−')..' DR' local output =    if RoY._yearName(year) ==  then        output = output..formattedYear..'' else output = output..RoY._yearName(year)..', '..formattedYear..']]' end return output end

function RoY.yearsByName local output = mw.html.create('table') :addClass('wikitable'):addClass('sortable') :tag('tr') :tag('th'):attr('data-sort-type','number'):attr('scope','col'):wikitext('Year'):done :tag('th'):wikitext('Name of Year'):done :done table.sort(yearNames, function(a,b) return a[2] < b[2] end) for i,v in ipairs(yearNames) do       output:tag('tr') :tag('td'):attr('data-sort-value',tonumber(v[1])) :wikitext(''..string.gsub(v[1],'-','−')..' DR') :done :tag('td'):wikitext(v[2]):done :done end output:allDone return tostring(output) end

return RoY