Difference between revisions of "Module:Automatic archive navigator/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Mr. Stradivarius
(simpler noredlinks logic)
blackwiki>Mr. Stradivarius
(sync)
Line 4: Line 4:
 
   
 
   
 
local p = {}
 
local p = {}
 +
 +
local args
 +
local frame
 +
local thisPage
  
local messageBox = require('Module:Message box')
+
-- Get a formatted link to the subpage a certain distance away, or nil
local yesno = require('Module:Yesno')
+
-- if that subpage does not exist. e.g. If the current subpage is
 +
-- /Archive 27, then getSubpageLink(3) returns a link to Archive 30.
 +
local function getSubpageLink(offset)
 +
    local subpageName
 +
    local startIdx, endIdx, archiveNum = mw.ustring.find(thisPage.subpageText, '^Archive ([0-9]+)')
 +
    if archiveNum then
 +
        subpageName = 'Archive ' .. (archiveNum + offset)
 +
    elseif tonumber(thisPage.subpageText) then
 +
        subpageName = thisPage.subpageText + offset
 +
    else
 +
        return nil  -- Couldn't parse out a subpage number
 +
    end
  
local function getSubpageLink(args, thisPage, offset)
+
    local page = mw.title.new(thisPage.baseText .. '/' .. subpageName, thisPage.namespace)
-- Get a formatted link to the subpage a certain distance away. E.g. If the current subpage is
+
    if page.exists then
-- /Archive 27, then getSubpageLink(3) returns a link to Archive 30. Returns nil for archive numbers
+
        return '[[../' .. subpageName .. '|' .. subpageName .. ']]'
-- less than 1, and if args.noredlinks is set returns nil if the archive subpage does not exist.
+
    else
local subpageName
+
        return nil
local startIdx, endIdx, archiveNum = mw.ustring.find(thisPage.subpageText, '^Archive ([1-9]%d*)')
+
    end
if archiveNum then
 
local subpageNum = tonumber(archiveNum) + offset
 
if subpageNum < 1 then
 
return nil
 
end
 
subpageName = 'Archive ' .. tostring(archiveNum + offset)
 
elseif tonumber(thisPage.subpageText) then
 
local subpageNum = tonumber(thisPage.subpageText) + offset
 
if subpageNum < 1 then
 
return nil
 
end
 
subpageName = tostring(subpageNum)
 
else
 
return nil  -- Couldn't parse out a subpage number
 
end
 
 
 
local page = mw.title.new(thisPage.baseText .. '/' .. subpageName, thisPage.namespace)
 
local noredlinks = yesno(args.noredlinks)
 
if not (noredlinks and not page.exists) then
 
return '[[../' .. subpageName .. '|' .. subpageName .. ']]'
 
else
 
return nil
 
end
 
 
end
 
end
  
local function getLinksText(args, thisPage)
+
local function getLinksText()
local nbsp = '&nbsp;'
+
    local arrowSpacer = '&nbsp;&nbsp;'
local arrowSpacer, linkSpacer
+
    local linkSpacer = '&nbsp;&nbsp;&nbsp;&nbsp;'
if mw.ustring.len(thisPage.subpageText) <= 4 then
+
    if mw.ustring.len(thisPage.subpageText) <= 4 then
-- If page names are short, we want more space. The previous template used a mix of regular, non-breaking,
+
        -- If page names are short, we want more space. But why the mix of regular, non-breaking, and em spaces?
-- and em spaces, but for simplicity we will use all non-breaking spaces.
+
        local emSpace = frame:expandTemplate({title = 'Unicode', args = {mw.ustring.char(8195)}})
arrowSpacer = string.rep(nbsp, 7)
+
        arrowSpacer = '&nbsp; ' .. emSpace .. ' &nbsp;&nbsp;'
linkSpacer = string.rep(nbsp, 10)
+
        linkSpacer = '&nbsp; ' .. emSpace .. ' &nbsp; ' .. emSpace .. '&nbsp;'
else
+
    end
arrowSpacer = string.rep(nbsp, 2)
+
   
linkSpacer = string.rep(nbsp, 4)
+
    local s = ''
end
+
   
+
    for offset = -5, -3 do
local s = ''
+
        local link = getSubpageLink(offset)
+
        if link then
for offset = -5, -3 do
+
            if offset == -3 then
local link = getSubpageLink(args, thisPage, offset)
+
                s = s .. link .. linkSpacer
if link then
+
            else
if offset == -3 then
+
                s = s .. link .. ' ←' .. arrowSpacer
s = s .. link .. linkSpacer
+
            end
else
+
            break
s = s .. link .. ' ←' .. arrowSpacer
+
        end
end
+
    end
break
+
   
end
+
    for offset = -2, -1 do
end
+
        local link = getSubpageLink(offset)
+
        if link then
for offset = -2, -1 do
+
            s = s .. link .. linkSpacer
local link = getSubpageLink(args, thisPage, offset)
+
        end
if link then
+
    end
s = s .. link .. linkSpacer
 
end
 
end
 
  
s = s .. '<span style="font-size:115%;">[[' .. thisPage.fullText .. '|' .. thisPage.subpageText .. ']]</span>'
+
    s = s .. '<span style="font-size:115%;">[[' .. thisPage.fullText .. '|' .. thisPage.subpageText .. ']]</span>'
+
   
for offset = 1, 2 do
+
    for offset = 1, 2 do
local link = getSubpageLink(args, thisPage, offset)
+
        local link = getSubpageLink(offset)
if link then
+
        if link then
s = s .. linkSpacer .. link
+
            s = s .. linkSpacer .. link
end
+
        end
end
+
    end
+
   
for offset = 5, 3, -1 do
+
    for offset = 5, 3, -1 do
local link = getSubpageLink(args, thisPage, offset)
+
        local link = getSubpageLink(offset)
if link then
+
        if link then
if offset == 3 then
+
            if offset == 3 then
s = s .. linkSpacer .. link
+
                s = s .. linkSpacer .. link
else
+
            else
s = s .. arrowSpacer .. '→ ' .. link
+
                s = s .. arrowSpacer .. '→ ' .. link
end
+
            end
break
+
            break
end
+
        end
end
+
    end
  
return s
+
    return s
 
end
 
end
  
local function getMessage(args, thisPage)
+
local function getMessage()
if args[1] == '1' then
+
    if args[1] == '1' then
return ''
+
        return ''
else
+
    else
local msg = '----\n'
+
        local msg = '----\n'
if args.text then
+
        if args.text then
msg = msg .. args.text
+
            msg = msg .. args.text
else
+
        else
msg = msg .. "This is an '''[[Help:Archiving a talk page|archive]]''' of past discussions"
+
            msg = msg .. "This is an '''[[Help:Archiving a talk page|archive]]''' of past discussions"
if args.period then
+
            if args.period then
msg = msg .. "&#32;for the period '''" .. args.period .. "'''"
+
                msg = msg .. "&#32;for the period '''" .. args.period .. "'''"
end
+
            end
msg = msg .. ". '''Do not edit the contents of this page.''' If you wish to start a new discussion or revive an old one, please do so on the "
+
            msg = msg .. ". '''Do not edit the contents of this page.''' If you wish to start a new discussion or revive an old one, please do so on the "
msg = msg .. "[[" .. thisPage.rootPageTitle.fullText .. "|current talk page]]."
+
            msg = msg .. "[[" .. thisPage.rootPageTitle.fullText .. "|current talk page]]."
end
+
        end
return msg
+
        return msg
end
+
    end
 
end
 
end
  
function p._aan(args)
+
local function _aan()
-- For testing purposes, allow passing in the page name as a param.
+
    frame = mw.getCurrentFrame()
if args.title then
+
thisPage = mw.title.new(args.title)
+
    -- For testing purposes, allow passing in the page name as a param.
else
+
    if args.title then
thisPage = mw.title.getCurrentTitle()
+
        thisPage = mw.title.new(args.title)
end
+
    else
local image = args.image
+
        thisPage = mw.title.getCurrentTitle()
if not image then
+
    end
image = '[[File:' .. (args.icon or 'Replacement filing cabinet.svg') .. '|40x40px|alt=|link=]]'
+
   
end
+
    local image = args.image
local mbox = messageBox.main('tmbox', {
+
    if not image then
image = image,
+
        image = '[[File:' .. (args.icon or 'Replacement filing cabinet.svg') .. '|40x40px|alt=|link=]]'
imageright = args.imageright,
+
    end
style = args.style or 'width:80%;margin-left:auto;margin-right:auto;',
+
   
textstyle = args.textstyle or 'text-align:center;',
+
    local mbox = frame:expandTemplate({title = 'tmbox', args = {
text = getLinksText(args, thisPage) .. '\n' .. getMessage(args, thisPage)
+
        image = image,
})
+
        imageright = args.imageright,
return mbox .. '__NONEWSECTIONLINK__ __NOEDITSECTION__'
+
        style = args.style or 'width:80%;margin-left:auto;margin-right:auto;',
 +
        textstyle = args.textstyle or 'text-align:center;',
 +
        text = getLinksText() .. '\n' .. getMessage()
 +
    }})
 +
   
 +
    return mbox .. '__NONEWSECTIONLINK__ __NOEDITSECTION__'
 
end
 
end
  
 
function p.aan(frame)
 
function p.aan(frame)
local origArgs
+
    local origArgs
-- If called via #invoke, use the args passed into the invoking template.
+
    -- If called via #invoke, use the args passed into the invoking template.
-- Otherwise, for testing purposes, assume args are being passed directly in.
+
    -- Otherwise, for testing purposes, assume args are being passed directly in.
if frame == mw.getCurrentFrame() then
+
    if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
+
        origArgs = frame:getParent().args
for k, v in pairs(frame.args) do
+
    else
origArgs = frame.args
+
        origArgs = frame
break
+
    end
end
+
else
+
    -- ParserFunctions considers the empty string to be false, so to preserve the previous  
origArgs = frame
+
    -- template behavior, change any empty arguments to nil, so Lua will consider
end
+
    -- them false too.
-- ParserFunctions considers the empty string to be false, so to preserve the previous  
+
    args = {}
-- template behavior, change any empty arguments to nil, so Lua will consider
+
    for k, v in pairs(origArgs) do
-- them false too.
+
        if v ~= '' then
args = {}
+
            args[k] = v
for k, v in pairs(origArgs) do
+
        end
if v ~= '' then
+
    end
args[k] = v
+
   
end
+
    return _aan()
end
 
return p._aan(args)
 
 
end
 
end
 
   
 
   
 
return p
 
return p

Revision as of 03:53, 5 October 2014

Documentation for this module may be created at Module:Automatic archive navigator/sandbox/doc

--
-- This module implements {{Automatic archive navigator}}
--
 
local p = {}
 
local args
local frame
local thisPage

-- Get a formatted link to the subpage a certain distance away, or nil
-- if that subpage does not exist. e.g. If the current subpage is
-- /Archive 27, then getSubpageLink(3) returns a link to Archive 30.
local function getSubpageLink(offset)
    local subpageName
    local startIdx, endIdx, archiveNum = mw.ustring.find(thisPage.subpageText, '^Archive ([0-9]+)')
    if archiveNum then
        subpageName = 'Archive ' .. (archiveNum + offset)
    elseif tonumber(thisPage.subpageText) then
        subpageName = thisPage.subpageText + offset
    else
        return nil  -- Couldn't parse out a subpage number
    end

    local page = mw.title.new(thisPage.baseText .. '/' .. subpageName, thisPage.namespace)
    if page.exists then
        return '[[../' .. subpageName .. '|' .. subpageName .. ']]'
    else
        return nil
    end
end

local function getLinksText()
    local arrowSpacer = '&nbsp;&nbsp;'
    local linkSpacer = '&nbsp;&nbsp;&nbsp;&nbsp;'
    if mw.ustring.len(thisPage.subpageText) <= 4 then
        -- If page names are short, we want more space. But why the mix of regular, non-breaking, and em spaces?
        local emSpace = frame:expandTemplate({title = 'Unicode', args = {mw.ustring.char(8195)}})
        arrowSpacer = '&nbsp; ' .. emSpace .. ' &nbsp;&nbsp;'
        linkSpacer = '&nbsp; ' .. emSpace .. ' &nbsp; ' .. emSpace .. '&nbsp;'
    end
    
    local s = ''
    
    for offset = -5, -3 do
        local link = getSubpageLink(offset)
        if link then
            if offset == -3 then
                s = s .. link .. linkSpacer
            else
                s = s .. link .. ' ←' .. arrowSpacer
            end
            break
        end
    end
    
    for offset = -2, -1 do
        local link = getSubpageLink(offset)
        if link then
            s = s .. link .. linkSpacer
        end
    end

    s = s .. '<span style="font-size:115%;">[[' .. thisPage.fullText .. '|' .. thisPage.subpageText .. ']]</span>'
    
    for offset = 1, 2 do
        local link = getSubpageLink(offset)
        if link then
            s = s .. linkSpacer .. link
        end
    end
    
    for offset = 5, 3, -1 do
        local link = getSubpageLink(offset)
        if link then
            if offset == 3 then
                s = s .. linkSpacer .. link
            else
                s = s .. arrowSpacer .. '→ ' .. link
            end
            break
        end
    end

    return s
end

local function getMessage()
    if args[1] == '1' then
        return ''
    else
        local msg = '----\n'
        if args.text then
            msg = msg .. args.text
        else
            msg = msg .. "This is an '''[[Help:Archiving a talk page|archive]]''' of past discussions"
            if args.period then
                msg = msg .. "&#32;for the period '''" .. args.period .. "'''"
            end
            msg = msg .. ". '''Do not edit the contents of this page.''' If you wish to start a new discussion or revive an old one, please do so on the "
            msg = msg .. "[[" .. thisPage.rootPageTitle.fullText .. "|current talk page]]."
        end
        return msg
    end
end

local function _aan()
    frame = mw.getCurrentFrame()
 
    -- For testing purposes, allow passing in the page name as a param.
    if args.title then
        thisPage = mw.title.new(args.title)
    else
        thisPage = mw.title.getCurrentTitle()
    end
    
    local image = args.image
    if not image then
        image = '[[File:' .. (args.icon or 'Replacement filing cabinet.svg') .. '|40x40px|alt=|link=]]'
    end
    
    local mbox = frame:expandTemplate({title = 'tmbox', args = {
        image = image,
        imageright = args.imageright,
        style = args.style or 'width:80%;margin-left:auto;margin-right:auto;',
        textstyle = args.textstyle or 'text-align:center;',
        text = getLinksText() .. '\n' .. getMessage()
    }})
    
    return mbox .. '__NONEWSECTIONLINK__ __NOEDITSECTION__'
end

function p.aan(frame)
    local origArgs
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
 
    -- ParserFunctions considers the empty string to be false, so to preserve the previous 
    -- template behavior, change any empty arguments to nil, so Lua will consider
    -- them false too.
    args = {}
    for k, v in pairs(origArgs) do
        if v ~= '' then
            args[k] = v
        end
    end
    
    return _aan()
end
 
return p