Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Ingredient link list

From Mine in Abyss
Revision as of 13:26, 11 October 2024 by Scyu (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}
local f = mw.getCurrentFrame()

function p.genlink(args)
	if args[1] == nil then
		return { errno = 1 }
	end
	local sep = args.sep or ';'
	local firstarg = args.firstarg or 1
	if tonumber(firstarg) >= 2 and (not args.maxargs) then
		return { errno = 2 }
	end
	local maxargs = args.maxargs or (#args - 1)
	local output_array = {}
	for i = firstarg, maxargs do
		local name_pieces = {}
		for current_name in mw.text.gsplit(mw.text.trim(args[i]), sep) do
			local trimmed_name = mw.text.trim(current_name)
			if trimmed_name ~= '' then
				name_pieces[trimmed_name] = true
			end
		end
		local converted_names = {}
		for name, exist in pairs(name_pieces) do
			if exist then
				local prefix
				if string.find(name, '^Matching ') then
					prefix = 'Matching '
					name = string.gsub(name, '^Matching ', '')
				elseif string.find(name, '^Any ') then
					prefix = 'Any '
					name = string.gsub(name, '^Any ', '')
				else
					prefix = ''
				end
				table.insert(converted_names, f:preprocess(prefix .. '[[' .. name .. ']]'))
			end
		end
		table.insert(output_array, table.concat(converted_names, ' or<br>'))
	end
	return table.concat(output_array, ' +<br>')
end

function p.main()
	local args = require('Module:ProcessArgs').merge(true)
	local result = p.genlink(args)
	if type(result) == 'string' then
		return result
	end
	local error_prompts = {
		'The first anonymous parameter could not be empty!',
		'When the argument "firstarg" is equals to or greater than 2, the argument "maxargs" must be set!'
	}
	return f:expandTemplate{ title = 'Error', args = { error_prompts[result.errno] } }
end

return p