Module:YearNav: Difference between revisions

From JJSWiki
Jump to navigation Jump to search
No edit summary
Tag: Reverted
(OK, seems the trouble was I was just seeing the output on a page without a four digit year...)
Tag: Manual revert
 
(One intermediate revision by the same user not shown)
Line 20: Line 20:


     _, _, OriginalYear = string.find(PageName, "(%d%d%d%d)")
     _, _, OriginalYear = string.find(PageName, "(%d%d%d%d)")
    --DebugOutput = "ThisPageTitle " .. ThisPageTitle .. " Prefix " .. Prefix .. " Namespace " .. Namespace .. " PageName " .. PageName
    DebugOutput = ThisPageTitle
    return DebugOutput
end
function p.fakesoitwillshutup(frame)
     PageWithout = string.gsub(PageName, OriginalYear, "@@@@")
     PageWithout = string.gsub(PageName, OriginalYear, "@@@@")



Latest revision as of 19:13, 2 April 2026

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

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
    --Prefix = "Category"
    --Namespace = 0
    --PageName = "1987 Sandwiches"

    _, _, 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

function p.JustTheYear(frame)
	PageName=frame.args[1] or -1
    if PageName==-1 then
    	ThisPageTitle = mw.title.getCurrentTitle()
    	PageName = ThisPageTitle.text
    end

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

    return OriginalYear
end

return p