Jump to content

Module:Numbered subpages

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 15:33, 27 October 2016. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module implements {{numbered subpages}}.

local getArgs = require('Module:Arguments').getArgs

p = {}

-- Gets a mw.title object, using pcall to avoid generating script errors if we
-- are over the expensive function count limit (among other possible causes).
-- (Function imported from [[Module:Redirect]])
local function getTitle(...)
	local success, titleObj = pcall(mw.title.new, ...)
	if success then
		return titleObj
	else
		return nil
	end
end

function p.main(frame)
	local args = getArgs(frame)
	local maxk = tonumber(args.max or '50') or 50
	local mink = tonumber(args.min or '1') or 1
	local root = frame:preprocess('{{FULLPAGENAME}}')
	local missing = args.missing or 'transclude'
	local res = missing
	
	for i=mink,maxk do
		local sname = root .. '/' .. i
		if sname then
			res = res .. frame:expandTemplate{title = 'subpage', args = { i } }
		else
			if missing == 'transclude' then
				res = res .. frame:expandTemplate{title = 'subpage', args = { i } }
			elseif missing == 'link' then
				res = res .. '[[' .. sname .. ']] '
			elseif missing == 'stop' then
				i = maxk + 1
			end
		end
	end
	
	return res
end

return p