Terra Battle 2 Wiki
Advertisement
Template-info.png Documentation

This module is used to parse and display release dates, or to add release date categories.

Usage[]

  • {{#invoke:release date|link|September 28, 2017}} → September 28, 2017
  • {{#invoke:release date|link|Sep 28, 2017}} → September 28, 2017
  • {{#invoke:release date|link|invalid date}} → TBD
  • {{#invoke:release date|category|September 28, 2017}} → Adds the category Released on September 28, 2017

local p = {}

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

function p._parse(date)
    local months = {
        January = 1,
        Jan = 1,
        February = 2,
        Feb = 2,
        March = 3,
        Mar = 3,
        April = 4,
        Apr = 4,
        May = 5,
        June = 6,
        Jun = 6,
        July = 7,
        Jul = 7,
        August = 8,
        Aug = 8,
        September = 9,
        Sep = 9,
        Sept = 9,
        October = 10,
        Oct = 10,
        November = 11,
        Nov = 11,
        December = 12,
        Dec = 12,
    }
    if isempty(date) then
        return nil
    end
    m, d, y = string.match(mw.text.trim(date), "(%a+)%s+(%d+),%s+(%d+)")
    y = tonumber(y)
    d = tonumber(d)
    if not y or not months[m] or not d or d < 1 or d > 31 then
        return nil
    end
    return os.time{year = y, month = months[m], day = d}
end

function p.link(frame)
    local date = p._parse(frame.args[1])
    if not date then
        return "TBD"
    end
    local s = os.date("%B %d, %Y", date):gsub(" 0", " ")
    return string.format("[[Changes on %s|%s]]", s, s)
end

function p.category(frame)
    local date = p._parse(frame.args[1])
    if not date then
        return nil
    end
    local s = os.date("%B %d, %Y", date):gsub(" 0", " ")
    return string.format("[[Category:Released_on_%s]]", s)
end

-- Run this before saving changes.
function p._test()
    local testcases = {
        {"September 28, 2017", "[[Changes on September 28, 2017|September 28, 2017]]"},
        {"Sep 28, 2017", "[[Changes on September 28, 2017|September 28, 2017]]"},
        {"Sep 8, 2017", "[[Changes on September 8, 2017|September 8, 2017]]"},
        {"today", "TBD"},
        {"", "TBD"},
    }
    for _, tc in ipairs(testcases) do
        local out = p.link({args = {tc[1]}})
        if out ~= tc[2] then
            mw.log(string.format("link(%s) is %s; expected %s\n", tc[1], out, tc[2]))
        end
    end
end

return p
Advertisement