Module:Prop list

From para.wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Prop list/doc

require "table"

local p = {}

-- Create a method which is a simple concatenation of function applications
--  makeExpand(frame) => expand(x)
function expandingMethod(makeExpand)
	return function(frame)
		local args = frame.args
		local list = args.list
		
		if list == "" then
			return ""
		end
		
		local expand = makeExpand(frame)
		
		local link = (args.link == "yes")
		if args.link == 'yes' then
			local oldx = expand
			expand = function(x, i)
				return '[[' .. x .. '|' .. oldx(x, i) .. ']]'
			end
		end
		
		local name = args.name
		local out = ""
		local i = 1
		for x in mw.text.gsplit(list, args.sep) do
			out = out .. expand(x, i)
			mw.smw.set{name, x}
			i = i + 1
		end
		
		return out
	end
end

-- Apply a template to each item in the list
p.apply = expandingMethod(function(frame)
	local tpl = frame.args.tpl
	return function(x)
		return frame:expandTemplate{title=tpl, args={x}}
	end
end)

p.wikilist = expandingMethod(function(frame)
	local args = frame.args
	local tpl = args.tpl
	local prefix = args.prefix
	
	if tpl and tpl ~= "" then
		return function(x)
			return prefix .. frame:expandTemplate{title=tpl, args={x}} .. '\n'
		end
	else
		return function(x)
			return prefix .. x .. '\n'
		end
	end
end)

function listingMethod(makeExpand)
	return function(frame)
		local args = frame.args
		local list = args.list
		
		if list == "" then
			return ""
		end
		
		local expand = makeExpand(frame)
		
		if args.link == 'yes' then
			local oldx = expand
			expand = function(x, i)
				return '[[' .. x .. '|' .. oldx(x, i) .. ']]'
			end
		end
		
		local join = args.join
		local final = args.final
		local xtpl = expand
		expand = function(x, i)
			if i == 1 then
				return xtpl(x)
			elseif i == #args then
				return final .. xtpl(x)
			else
				return join .. xtpl(x)
			end
		end
		
		local name = args.name
		local out = ""
		local i = 1
		for x in mw.text.gsplit(list, args.sep) do
			out = out .. expand(x, i)
			mw.smw.set{name, x}
			i = i + 1
		end
		
		return out
	end
end

p.list = listingMethod(function(frame)
	local tpl = frame.args.tpl
	if tpl and tpl ~= "" then
		return function(x)
			return frame:expandTemplate{title=tpl, args={x}}
		end
	else
		return function(x)
			return x
		end
	end
end)

-- {{tpl|x}} x
p.prefix = listingMethod(function(frame)
	local tpl = frame.args.tpl
	return function(x)
		return frame:expandTemplate{title="pair", args = {
			frame:expandTemplate{title=tpl, args={x}}, x
		}}
	end
end)

-- x {{tpl|x}}
p.suffix = listingMethod(function(frame)
	local tpl = frame.args.tpl
	return function(x)
		return frame:expandTemplate{title="pair", args = {
			x, frame:expandTemplate{title=tpl, args={x}}
		}}
	end
end)

-- Output just the first item
function p.first(frame)
	local args = frame.args
	local list = args.list
	
	if list == "" then
		return ""
	end
	
	local name = args.name
	list = mw.text.split(list, args.sep)
	
	local out = list[1]
	table.remove(list, 1)
	mw.smw.set{name, out}
	for i, x in ipairs(list) do
		mw.smw.set{name, x}
	end
	
	return out
end

-- Output just the last item
function p.last(frame)
	local args = frame.args
	local list = args.list
	
	if list == "" then
		return ""
	end
	
	local name = args.name
	
	local out
	for x in mw.text.gsplit(list, args.sep) do
		out = x
		mw.smw.set{name, x}
	end
	
	return out
end

return p