Module: Date: Difference between revisions

From A Wiki of Ice and Fire
Jump to: navigation, search
Abjiklam (talk | contribs)
No edit summary
Abjiklam (talk | contribs)
No edit summary
Line 7: Line 7:
end
end


local function link(year, suffix)
function p.link(year, suffix)
     local s = tostring(year)
     local s, name, page
    local name, page
     if year > 0 then
     if year > 0 then
         name = suffix and (s .. " ᴀᴄ") or s
        s = tostring(year)
       
         name = suffix and (s .. " ᴀᴄ") or s
         page = mw.title.makeTitle(0, s .. " AC")
         page = mw.title.makeTitle(0, s .. " AC")
         if not page.exists then
         if not page.exists then
Line 22: Line 21:
         end
         end
     elseif year < 0 then
     elseif year < 0 then
         name = suffix and (s .. " ʙᴄ") or s
        s = tostring(math.abs(year))
       
         name = suffix and (s .. "&nbsp;ʙᴄ") or s
         page = mw.title.makeTitle(0, s .. " BC")
         page = mw.title.makeTitle(0, s .. " BC")
         if not page.exists then
         if not page.exists then
Line 32: Line 31:
                 )
                 )
         end
         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;ᴀᴄ") or s
     elseif year < 0 then
     elseif year < 0 then
        s = tostring(math.abs(year))
        return suffix and (s .. "&nbsp;ʙᴄ") or s
     end
     end
    return "[[" .. page.fullText .. "|" .. name .. "]]"
end
end


Line 42: Line 50:
      
      
     local y1 = tonumber(pargs[1]) or tonumber(cargs[1])
     local y1 = tonumber(pargs[1]) or tonumber(cargs[1])
    local y2 = tonumber(pargs[2]) or tonumber(cargs[2])
     local suffix = yesno(pargs.suffix)
     local suffix = yesno(pargs.suffix)
     if suffix == nil then suffix = yesno(cargs.suffix) end
     if suffix == nil then suffix = yesno(cargs.suffix) end
     if suffix == nil then suffix = true end
     if suffix == nil then suffix = true end
      
      
     return link(y1, suffix)
     if 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
end


return p
return p

Revision as of 22:47, 29 August 2018

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

local yesno = require('Module:Yesno')

local p = {}

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 = tostring(year)
        name = suffix and (s .. "&nbsp;ᴀᴄ") or s
        page = mw.title.makeTitle(0, s .. " AC")
        if not page.exists then
            page = mw.title.makeTitle(
                    0,
                    "Years after Aegon's Conquest",
                    "Year " .. s .. " After the Conquest"
                )
        end
    elseif year < 0 then
        s = tostring(math.abs(year))
        name = suffix and (s .. "&nbsp;ʙᴄ") or s
        page = mw.title.makeTitle(0, s .. " BC")
        if not page.exists then
            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;ᴀᴄ") or s
    elseif year < 0 then
        s = tostring(math.abs(year))
        return suffix and (s .. "&nbsp;ʙᴄ") or s
    end
end

function p.main(frame)
    local pargs = frame:getParent().args
    local cargs = frame.args
    
    local y1 = tonumber(pargs[1]) or tonumber(cargs[1])
    local y2 = tonumber(pargs[2]) or tonumber(cargs[2])
    local suffix = yesno(pargs.suffix)
    if suffix == nil then suffix = yesno(cargs.suffix) end
    if suffix == nil then suffix = true end
    
    if 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

return p