Module: Date

From A Wiki of Ice and Fire
Jump to: navigation, search

Documentation for this module may be created at Module:Date/doc

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local lang  = mw.getContentLanguage()

local p = {}

-- Do not replace the following with a variation of #ifexist. This is an expensive parser function that should not be used for this module.
local year_articles = { -- When an article is created for a specific year, add it to the list. Don't forget the negative sign for BC!
      [1] = true,
      [2] = true,
      [4] = true,
      [5] = true,
      [9] = true,
     [10] = true,
     [12] = true,
     [13] = true,
     [37] = true,
     [39] = true,
     [41] = true,
     [42] = true,
     [43] = true,
     [44] = true,
     [45] = true,
     [47] = true,
     [48] = true,
     [49] = true,
     [50] = true,
     [51] = true,
     [52] = true,
     [54] = true,
     [56] = true,
     [59] = true,
     [60] = true,
     [61] = true,
     [83] = true,
     [92] = true,
     [99] = true,
    [101] = true,
    [103] = true,
    [104] = true,
    [105] = true,
    [106] = true,
    [111] = true,
    [120] = true,
    [127] = true,
    [128] = true,
    [129] = true,
    [130] = true,
    [131] = true,
    [132] = true,
    [133] = true,
    [134] = true,
    [135] = true,
    [136] = true,
    [160] = true,
    [161] = true,
    [184] = true,
    [196] = true,
    [209] = true,
    [211] = true,
    [212] = true,
    [219] = true,
    [233] = true,
    [236] = true,
    [259] = true,
    [260] = true,
    [261] = true,
    [262] = true,
    [280] = true,
    [281] = true,
    [282] = true,
    [283] = true,
    [284] = true,
    [285] = true,
    [289] = true,
    [290] = true,
    [297] = true,
    [298] = true,
    [299] = true,
    [300] = true,
}


local function is_empty(s)
    return s == nil or s == ''
end

function p.link(year, suffix)
    local s, name, page
    if year > 0 then
        s = lang:formatNum(year)
        name = suffix and (s .. " AC") or s
        if year_articles[year] then
            page = mw.title.makeTitle(0, s .. " AC")
        else
            page = mw.title.makeTitle(
                    0,
                    "Years after Aegon's Conquest",
                    "Year " .. s .. " After the Conquest"
                )
        end
    elseif year < 0 then
        s = lang:formatNum(math.abs(year))
        name = suffix and (s .. "&nbsp;BC") or s
        if year_articles[year] then
            page = mw.title.makeTitle(0, s .. " BC")
        else
            page = mw.title.makeTitle(
                    0,
                    "Years before Aegon's Conquest",
                    "Year " .. s .. " Before the Conquest"
                )
        end
    end
    return "[[" .. page.fullText .. "|" .. name .. "]]"
end

function p.nolink(year, suffix)
    local s = tostring(year)
    if year > 0 then
        return suffix and (s .. "&nbsp;AC") or s
    elseif year < 0 then
        s = tostring(math.abs(year))
        return suffix and (s .. "&nbsp;BC") or s
    end
end

function p.main(frame)
    local args = getArgs(frame)
    return p._main(args)
end

function p._main(args)
    local y1 = lang:parseFormattedNumber(args[1])
    local y2 = lang:parseFormattedNumber(args[2])
    local suffix = yesno(args.suffix)
    if suffix == nil then suffix = true end
    if y1 == nil then
        local text = args[1]
        return "[[Years after Aegon's Conquest|" .. text .. "&nbsp;AC]]"
    elseif y2 == nil then
        return p.link(y1, suffix)
    elseif (y1 > 0) == (y2 > 0) then
        return p.link(y1, false) .. "–" .. p.link(y2, true)
    else
        return p.link(y1, true) .. "&nbsp;– " .. p.link(y2, true)
    end
end

function p._format(d)
    return tonumber(d) and p._main{d} or d
end
return p