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

From blackwiki
Jump to navigation Jump to search
blackwiki>Mr. Stradivarius
(start work on making global variables local)
blackwiki>Mr. Stradivarius
(finish converting global variables to local, and switch to using Module:Message box)
Line 4: Line 4:
 
   
 
   
 
local p = {}
 
local p = {}
+
 
local frame
+
local messageBox = require('Module:Message box')
local thisPage
 
  
 
-- 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
Line 30: Line 29:
 
end
 
end
  
local function getLinksText(args)
+
local function getLinksText(args, thisPage)
 
local arrowSpacer = '  '
 
local arrowSpacer = '  '
 
local linkSpacer = '    '
 
local linkSpacer = '    '
 
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. 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 = mw.getCurrentFrame():expandTemplate({title = 'Unicode', args = {mw.ustring.char(8195)}})
 
arrowSpacer = '&nbsp; ' .. emSpace .. ' &nbsp;&nbsp;'
 
arrowSpacer = '&nbsp; ' .. emSpace .. ' &nbsp;&nbsp;'
 
linkSpacer = '&nbsp; ' .. emSpace .. ' &nbsp; ' .. emSpace .. '&nbsp;'
 
linkSpacer = '&nbsp; ' .. emSpace .. ' &nbsp; ' .. emSpace .. '&nbsp;'
Line 55: Line 54:
 
 
 
for offset = -2, -1 do
 
for offset = -2, -1 do
local link = getSubpageLink(offset)
+
local link = getSubpageLink(thisPage, offset)
 
if link then
 
if link then
 
s = s .. link .. linkSpacer
 
s = s .. link .. linkSpacer
Line 64: Line 63:
 
 
 
for offset = 1, 2 do
 
for offset = 1, 2 do
local link = getSubpageLink(offset)
+
local link = getSubpageLink(thisPage, offset)
 
if link then
 
if link then
 
s = s .. linkSpacer .. link
 
s = s .. linkSpacer .. link
Line 71: Line 70:
 
 
 
for offset = 5, 3, -1 do
 
for offset = 5, 3, -1 do
local link = getSubpageLink(offset)
+
local link = getSubpageLink(thisPage, offset)
 
if link then
 
if link then
 
if offset == 3 then
 
if offset == 3 then
Line 85: Line 84:
 
end
 
end
  
local function getMessage(args)
+
local function getMessage(args, thisPage)
 
if args[1] == '1' then
 
if args[1] == '1' then
 
return ''
 
return ''
Line 105: Line 104:
  
 
function p._aan(args)
 
function p._aan(args)
frame = mw.getCurrentFrame()
 
 
 
-- For testing purposes, allow passing in the page name as a param.
 
-- For testing purposes, allow passing in the page name as a param.
 
if args.title then
 
if args.title then
Line 113: Line 110:
 
thisPage = mw.title.getCurrentTitle()
 
thisPage = mw.title.getCurrentTitle()
 
end
 
end
 
 
local image = args.image
 
local image = args.image
 
if not image then
 
if not image then
 
image = '[[File:' .. (args.icon or 'Replacement filing cabinet.svg') .. '|40x40px|alt=|link=]]'
 
image = '[[File:' .. (args.icon or 'Replacement filing cabinet.svg') .. '|40x40px|alt=|link=]]'
 
end
 
end
+
local mbox = messageBox.main('tmbox', {
local mbox = frame:expandTemplate({title = 'tmbox', args = {
 
 
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(args) .. '\n' .. getMessage(args)
+
text = getLinksText(args, thisPage) .. '\n' .. getMessage(args, thisPage)
}})
+
})
 
 
return mbox .. '__NONEWSECTIONLINK__ __NOEDITSECTION__'
 
return mbox .. '__NONEWSECTIONLINK__ __NOEDITSECTION__'
 
end
 
end

Revision as of 01:00, 14 November 2013

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

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

local messageBox = require('Module:Message box')

-- 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(thisPage, 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(args, thisPage)
	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 = mw.getCurrentFrame():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(thisPage, 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(thisPage, 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(thisPage, offset)
		if link then
			s = s .. linkSpacer .. link
		end
	end
	
	for offset = 5, 3, -1 do
		local link = getSubpageLink(thisPage, 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(args, thisPage)
	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

function p._aan(args)
	-- 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 = messageBox.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, thisPage) .. '\n' .. getMessage(args, thisPage)
	})
	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
		for k, v in pairs(frame.args) do
			origArgs = frame.args
			break
		end
	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 p._aan(args)
end
 
return p