Difference between revisions of "Module:Automatic archive navigator/sandbox"
Jump to navigation
Jump to search
blackwiki>Mr. Stradivarius (use tabs for indentation) |
blackwiki>Mr. Stradivarius (use local variables) |
||
Line 5: | Line 5: | ||
local p = {} | local p = {} | ||
− | local | + | local function getSubpageLink(offset, title) |
− | + | -- 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, title) returns a link to Archive 30. | |
− | -- 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 subpageName | local subpageName | ||
− | local startIdx, endIdx, archiveNum = mw.ustring.find( | + | local startIdx, endIdx, archiveNum = mw.ustring.find(title.subpageText, '^Archive ([0-9]+)') |
if archiveNum then | if archiveNum then | ||
subpageName = 'Archive ' .. (archiveNum + offset) | subpageName = 'Archive ' .. (archiveNum + offset) | ||
− | elseif tonumber( | + | elseif tonumber(title.subpageText) then |
− | subpageName = | + | subpageName = title.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 page = mw.title.new( | + | local page = mw.title.new(title.baseText .. '/' .. subpageName, title.namespace) |
if page.exists then | if page.exists then | ||
return '[[../' .. subpageName .. '|' .. subpageName .. ']]' | return '[[../' .. subpageName .. '|' .. subpageName .. ']]' | ||
Line 31: | Line 27: | ||
end | end | ||
− | local function getLinksText() | + | local function getLinksText(args, title) |
local arrowSpacer = ' ' | local arrowSpacer = ' ' | ||
local linkSpacer = ' ' | local linkSpacer = ' ' | ||
− | if mw.ustring.len( | + | if mw.ustring.len(title.subpageText) <= 4 then |
-- If page names are short, we want more space. But why the mix of regular, non-breaking, and em spaces? | -- 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)}}) | local emSpace = frame:expandTemplate({title = 'Unicode', args = {mw.ustring.char(8195)}}) | ||
Line 44: | Line 40: | ||
for offset = -5, -3 do | for offset = -5, -3 do | ||
− | local link = getSubpageLink(offset) | + | local link = getSubpageLink(offset, title) |
if link then | if link then | ||
if offset == -3 then | if offset == -3 then | ||
Line 56: | Line 52: | ||
for offset = -2, -1 do | for offset = -2, -1 do | ||
− | local link = getSubpageLink(offset) | + | local link = getSubpageLink(offset, title) |
if link then | if link then | ||
s = s .. link .. linkSpacer | s = s .. link .. linkSpacer | ||
Line 62: | Line 58: | ||
end | end | ||
− | s = s .. '<span style="font-size:115%;">[[' .. | + | s = s .. '<span style="font-size:115%;">[[' .. title.fullText .. '|' .. title.subpageText .. ']]</span>' |
for offset = 1, 2 do | for offset = 1, 2 do | ||
− | local link = getSubpageLink(offset) | + | local link = getSubpageLink(offset, title) |
if link then | if link then | ||
s = s .. linkSpacer .. link | s = s .. linkSpacer .. link | ||
Line 72: | Line 68: | ||
for offset = 5, 3, -1 do | for offset = 5, 3, -1 do | ||
− | local link = getSubpageLink(offset) | + | local link = getSubpageLink(offset, title) |
if link then | if link then | ||
if offset == 3 then | if offset == 3 then | ||
Line 86: | Line 82: | ||
end | end | ||
− | local function getMessage() | + | local function getMessage(args, title) |
if args[1] == '1' then | if args[1] == '1' then | ||
return '' | return '' | ||
Line 99: | Line 95: | ||
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 .. "[[" .. | + | msg = msg .. "[[" .. title.rootPageTitle.fullText .. "|current talk page]]." |
end | end | ||
return msg | return msg | ||
Line 105: | Line 101: | ||
end | end | ||
− | + | function p._aan(args, title) | |
− | + | title = title or mw.title.getCurrentTitle() | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
local image = args.image | local image = args.image | ||
Line 120: | Line 109: | ||
end | end | ||
− | local mbox = | + | local mbox = require('Module:Message box').main('tmbox', { |
image = image, | image = image, | ||
imageright = args.imageright, | imageright = args.imageright, | ||
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() .. '\n' .. getMessage() | + | text = getLinksText(args, title) .. '\n' .. getMessage(args, title) |
− | + | }) | |
return mbox .. '__NONEWSECTIONLINK__ __NOEDITSECTION__' | return mbox .. '__NONEWSECTIONLINK__ __NOEDITSECTION__' | ||
Line 132: | Line 121: | ||
function p.aan(frame) | function p.aan(frame) | ||
− | local | + | local args = require('Module:Arguments').getArgs(frame, { |
− | + | wrappers = 'Template:Automatic archive navigator' | |
− | + | }) | |
− | + | return p._aan(args) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | return _aan() | ||
end | end | ||
return p | return p |
Revision as of 04:47, 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 getSubpageLink(offset, title)
-- 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, title) returns a link to Archive 30.
local subpageName
local startIdx, endIdx, archiveNum = mw.ustring.find(title.subpageText, '^Archive ([0-9]+)')
if archiveNum then
subpageName = 'Archive ' .. (archiveNum + offset)
elseif tonumber(title.subpageText) then
subpageName = title.subpageText + offset
else
return nil -- Couldn't parse out a subpage number
end
local page = mw.title.new(title.baseText .. '/' .. subpageName, title.namespace)
if page.exists then
return '[[../' .. subpageName .. '|' .. subpageName .. ']]'
else
return nil
end
end
local function getLinksText(args, title)
local arrowSpacer = ' '
local linkSpacer = ' '
if mw.ustring.len(title.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 = ' ' .. emSpace .. ' '
linkSpacer = ' ' .. emSpace .. ' ' .. emSpace .. ' '
end
local s = ''
for offset = -5, -3 do
local link = getSubpageLink(offset, title)
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, title)
if link then
s = s .. link .. linkSpacer
end
end
s = s .. '<span style="font-size:115%;">[[' .. title.fullText .. '|' .. title.subpageText .. ']]</span>'
for offset = 1, 2 do
local link = getSubpageLink(offset, title)
if link then
s = s .. linkSpacer .. link
end
end
for offset = 5, 3, -1 do
local link = getSubpageLink(offset, title)
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(args, title)
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 .. "[[" .. title.rootPageTitle.fullText .. "|current talk page]]."
end
return msg
end
end
function p._aan(args, title)
title = title 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, title) .. '\n' .. getMessage(args, title)
})
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