Module:Automatic archive navigator/sandbox

From blackwiki
< Module:Automatic archive navigator
Revision as of 01:49, 14 November 2013 by blackwiki>Mr. Stradivarius (fix args.noredlinks logic)
Jump to navigation Jump to search

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')
local yesno = require('Module:Yesno')

local function getSubpageLink(args, thisPage, offset)
	-- Get a formatted link to the subpage a certain distance away. E.g. If the current subpage is
	-- /Archive 27, then getSubpageLink(3) returns a link to Archive 30. Returns nil for archive numbers
	-- less than 1, and if args.noredlinks is set returns nil if the archive subpage does not exist.
	local subpageName
	local startIdx, endIdx, archiveNum = mw.ustring.find(thisPage.subpageText, '^Archive ([1-9]%d*)')
	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 useRedlinks = not yesno(args.noredlinks)
	if useRedlinks or (not useRedlinks and page.exists) then
		return '[[../' .. subpageName .. '|' .. subpageName .. ']]'
	else
		return nil
	end
end

local function getLinksText(args, thisPage)
	local nbsp = '&nbsp;'
	local arrowSpacer, linkSpacer
	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,
		-- and em spaces, but for simplicity we will use all non-breaking spaces.
		arrowSpacer = string.rep(nbsp, 7)
		linkSpacer = string.rep(nbsp, 10)
	else
		arrowSpacer = string.rep(nbsp, 2)
		linkSpacer = string.rep(nbsp, 4)
	end
	
	local s = ''
	
	for offset = -5, -3 do
		local link = getSubpageLink(args, 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(args, 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(args, thisPage, offset)
		if link then
			s = s .. linkSpacer .. link
		end
	end
	
	for offset = 5, 3, -1 do
		local link = getSubpageLink(args, 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