Difference between revisions of "Module:Automatic archive navigator/sandbox"
Jump to navigation
Jump to search
blackwiki>Mr. Stradivarius (use table.concat rather than string concatenation for the getLinksText function) |
blackwiki>Mr. Stradivarius (use currentTitle and subpageTitle instead of title and page) |
||
Line 9: | Line 9: | ||
end | end | ||
− | local function getSubpageLink(offset, | + | local function getSubpageLink(offset, currentTitle) |
-- Get a formatted link to the subpage a certain distance away, or nil | -- 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 | -- if that subpage does not exist. e.g. If the current subpage is | ||
− | -- /Archive 27, then getSubpageLink(3, | + | -- /Archive 27, then getSubpageLink(3, currentTitle) returns a link to Archive 30. |
local subpageName | local subpageName | ||
− | local archiveNum = | + | local archiveNum = currentTitle.subpageText:match('^Archive ([1-9][0-9]*)$') |
archiveNum = tonumber(archiveNum) | archiveNum = tonumber(archiveNum) | ||
if archiveNum then | if archiveNum then | ||
subpageName = 'Archive ' .. (archiveNum + offset) | subpageName = 'Archive ' .. (archiveNum + offset) | ||
− | elseif tonumber( | + | elseif tonumber(currentTitle.subpageText) then |
− | subpageName = | + | subpageName = currentTitle.subpageText + offset |
else | else | ||
return nil -- Couldn't parse out a subpage number | return nil -- Couldn't parse out a subpage number | ||
end | end | ||
− | local | + | local subpageTitle = mw.title.new(currentTitle.baseText .. '/' .. subpageName, currentTitle.namespace) |
− | if | + | if subpageTitle.exists then |
return makeWikilink('/' .. subpageName, subpageName) | return makeWikilink('/' .. subpageName, subpageName) | ||
else | else | ||
Line 32: | Line 32: | ||
end | end | ||
− | local function getLinksText(args, | + | local function getLinksText(args, currentTitle) |
local nbsp = ' ' | local nbsp = ' ' | ||
local arrowSpacer, linkSpacer | local arrowSpacer, linkSpacer | ||
− | if mw.ustring.len( | + | if mw.ustring.len(currentTitle.subpageText) <= 4 then |
-- If page names are short, we want more space. | -- If page names are short, we want more space. | ||
arrowSpacer = nbsp:rep(5) | arrowSpacer = nbsp:rep(5) | ||
Line 47: | Line 47: | ||
for offset = -5, -3 do | for offset = -5, -3 do | ||
− | local link = getSubpageLink(offset, | + | local link = getSubpageLink(offset, currentTitle) |
if link then | if link then | ||
if offset == -3 then | if offset == -3 then | ||
Line 62: | Line 62: | ||
for offset = -2, -1 do | for offset = -2, -1 do | ||
− | local link = getSubpageLink(offset, | + | local link = getSubpageLink(offset, currentTitle) |
if link then | if link then | ||
ret[#ret + 1] = link | ret[#ret + 1] = link | ||
Line 70: | Line 70: | ||
ret[#ret + 1] = '<span style="font-size:115%;">' | ret[#ret + 1] = '<span style="font-size:115%;">' | ||
− | ret[#ret + 1] = makeWikilink( | + | ret[#ret + 1] = makeWikilink(currentTitle.prefixedText, currentTitle.subpageText) |
ret[#ret + 1] = '</span>' | ret[#ret + 1] = '</span>' | ||
for offset = 1, 2 do | for offset = 1, 2 do | ||
− | local link = getSubpageLink(offset, | + | local link = getSubpageLink(offset, currentTitle) |
if link then | if link then | ||
ret[#ret + 1] = linkSpacer | ret[#ret + 1] = linkSpacer | ||
Line 82: | Line 82: | ||
for offset = 5, 3, -1 do | for offset = 5, 3, -1 do | ||
− | local link = getSubpageLink(offset, | + | local link = getSubpageLink(offset, currentTitle) |
if link then | if link then | ||
if offset == 3 then | if offset == 3 then | ||
Line 99: | Line 99: | ||
end | end | ||
− | local function getMessage(args, | + | local function getMessage(args, currentTitle) |
if args[1] == '1' then | if args[1] == '1' then | ||
return '' | return '' | ||
Line 112: | Line 112: | ||
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 .. makeWikilink( | + | msg = msg .. makeWikilink(currentTitle.rootPageTitle.fullText, 'current talk page') .. '.' |
end | end | ||
return msg | return msg | ||
Line 118: | Line 118: | ||
end | end | ||
− | function p._aan(args, | + | function p._aan(args, currentTitle) |
− | + | currentTitle = currentTitle or mw.title.getCurrentTitle() | |
local image = args.image | local image = args.image | ||
Line 131: | Line 131: | ||
style = args.style or 'width:80%;margin-left:auto;margin-right:auto;', | style = args.style or 'width:80%;margin-left:auto;margin-right:auto;', | ||
textstyle = args.textstyle or 'text-align:center;', | textstyle = args.textstyle or 'text-align:center;', | ||
− | text = getLinksText(args, | + | text = getLinksText(args, currentTitle) .. '\n' .. getMessage(args, currentTitle) |
}) | }) | ||
Revision as of 13:12, 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 function makeWikilink(page, display)
return string.format('[[%s|%s]]', page, display)
end
local function getSubpageLink(offset, currentTitle)
-- 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, currentTitle) returns a link to Archive 30.
local subpageName
local archiveNum = currentTitle.subpageText:match('^Archive ([1-9][0-9]*)$')
archiveNum = tonumber(archiveNum)
if archiveNum then
subpageName = 'Archive ' .. (archiveNum + offset)
elseif tonumber(currentTitle.subpageText) then
subpageName = currentTitle.subpageText + offset
else
return nil -- Couldn't parse out a subpage number
end
local subpageTitle = mw.title.new(currentTitle.baseText .. '/' .. subpageName, currentTitle.namespace)
if subpageTitle.exists then
return makeWikilink('/' .. subpageName, subpageName)
else
return nil
end
end
local function getLinksText(args, currentTitle)
local nbsp = ' '
local arrowSpacer, linkSpacer
if mw.ustring.len(currentTitle.subpageText) <= 4 then
-- If page names are short, we want more space.
arrowSpacer = nbsp:rep(5)
linkSpacer = nbsp:rep(7)
else
arrowSpacer = nbsp:rep(2)
linkSpacer = nbsp:rep(4)
end
local ret = {}
for offset = -5, -3 do
local link = getSubpageLink(offset, currentTitle)
if link then
if offset == -3 then
ret[#ret + 1] = link
ret[#ret + 1] = linkSpacer
else
ret[#ret + 1] = link
ret[#ret + 1] = ' ←'
ret[#ret + 1] = arrowSpacer
end
break
end
end
for offset = -2, -1 do
local link = getSubpageLink(offset, currentTitle)
if link then
ret[#ret + 1] = link
ret[#ret + 1] = linkSpacer
end
end
ret[#ret + 1] = '<span style="font-size:115%;">'
ret[#ret + 1] = makeWikilink(currentTitle.prefixedText, currentTitle.subpageText)
ret[#ret + 1] = '</span>'
for offset = 1, 2 do
local link = getSubpageLink(offset, currentTitle)
if link then
ret[#ret + 1] = linkSpacer
ret[#ret + 1] = link
end
end
for offset = 5, 3, -1 do
local link = getSubpageLink(offset, currentTitle)
if link then
if offset == 3 then
ret[#ret + 1] = linkSpacer
ret[#ret + 1] = link
else
ret[#ret + 1] = arrowSpacer
ret[#ret + 1] = '→ '
ret[#ret + 1] = link
end
break
end
end
return table.concat(ret)
end
local function getMessage(args, currentTitle)
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 .. " 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 .. makeWikilink(currentTitle.rootPageTitle.fullText, 'current talk page') .. '.'
end
return msg
end
end
function p._aan(args, currentTitle)
currentTitle = currentTitle or mw.title.getCurrentTitle()
local image = args.image
if not image then
image = '[[File:' .. (args.icon or 'Replacement filing cabinet.svg') .. '|40x40px|alt=|link=]]'
end
local mbox = require('Module:Message box').main('tmbox', {
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(args, currentTitle) .. '\n' .. getMessage(args, currentTitle)
})
return mbox .. '__NONEWSECTIONLINK__ __NOEDITSECTION__'
end
function p.aan(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Automatic archive navigator'
})
return p._aan(args)
end
return p