Module:YearNav

From The Satanic Wiki
Revision as of 07:38, 9 February 2017 by Mediawiki>KockaAdmiralac (Undo revision 51434 by KockaAdmiralac (talk) Oh wait didn't realize the test was actually just plain links)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

-- <nowiki>
local p = {}

function p.YearNav(frame)
    Pattern = frame.args[1] or -1

    if Pattern==-1 then
        Pattern = "####-10 ・・ ####-1 ・ '''####''' ・ ####+1 ・・ ####+10" --Default. Will be overwritten if something was there.
        for i,v in pairs(frame.args) do
            Pattern= i .. "=" .. v
        end
    end

    ThisPageTitle = mw.title.getCurrentTitle()
    Prefix = ThisPageTitle.nsText
    Namespace = ThisPageTitle.namespace
    PageName = ThisPageTitle.text

    _, _, OriginalYear = string.find(PageName, "(%d%d%d%d)")
    PageWithout = string.gsub(PageName, OriginalYear, "@@@@")

    CurrentText = Pattern

    itsthere, _, _, _ = string.find(CurrentText, "####([%+%-])(%d+)")

    while itsthere do --Continue while there are still instances in the pattern to alter
        _, _, plusminus, amount = string.find(CurrentText, "####([%+%-])(%d+)")
        if plusminus=="-" then
            NewYear = OriginalYear - amount
        else --assume it's plus
            NewYear = OriginalYear + amount
        end
        NewPageName = string.gsub(PageWithout, "@@@@", NewYear)
        if Namespace==0 then
            ReplacementSection = "[[" .. NewPageName .. "]]"
        else
            ReplacementSection = "[[:" .. Prefix .. ":" .. NewPageName .. "|" .. NewPageName .. "]]"
        end
        CurrentText = string.gsub(CurrentText, "####[%+%-]%d+", ReplacementSection, 1)
        itsthere, _, _, _ = string.find(CurrentText, "####([%+%-])(%d+)")
    end

    CurrentText = string.gsub(CurrentText, "####", PageName)

    return CurrentText

end


return p