Module:See also

From Wikisource
Jump to navigation Jump to search

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local error_message = require('Module:Error')['error']

function p._see_also(args)
	local link_count = 0
	for k, v in pairs(args) do
		local i = tonumber(k)
		if i and i > link_count then
			link_count = i
		end
	end
	
	local links = {}
	for i = 1, link_count do
		if args[i] then
			table.insert(links, '[[:' .. args[i] .. '|' .. (args['l' .. i] or args[i]) .. ']]')
		end
	end
	
	local link
	if #links == 0 then
		link = error_message({'[[Module:See also]] error: Module must be given at least one article name'})
	elseif #links == 1 then
		link = links[1]
	else
		link = table.concat(links, ', ', 1, math.min(#links - 1, 14)) .. ' and ' .. links[math.min(#links, 15)]
		if #links > 15 then
			link = link .. '<br/>' .. error_message({'[[Module:See also]] error: Too many links specified (maximum is 15)'})
		end
	end
	
	local text = (args.altphrase or 'See also') .. ': ' .. link
	
	return tostring(mw.html.create('div'):addClass('rellink boilerplate seealso'):css({['margin-left'] = '2em', ['font-style'] = 'italic'}):wikitext(text))
end

function p.see_also(frame)
	return p._see_also(getArgs(frame))
end

return p