Module:Ingredient link list

From Aether Project Wiki
Jump to navigation Jump to search
This is the documentation page. It will be transcluded into the main module page. See Template:Documentation for more information

Create an ingredient link list which compatiable with UI table templates.

This module shouldn't be invoked directly in articles, use templates to invoke instead, following parameters should be set:

  • |firstarg=: Optional, number. Indicates which anonymous parameter should be processed first, based on their index. Defaults to 1.
  • |maxargs=: Optional (required when |firstarg= is equals to or greater than 2, or return error code 2), number. Limits how many anonymous parameters should be processed. If unset, it will detect total numbers of anonymous parameters, and the last anonymous parameter will be excluded.
  • |sep=: Optional, string. Set a seperator character. If unset, ; will used.
  • (Anonymous Parameters): Strings. Source strings to create links, each part should be seperated by |sep=. The first anonymous parameter is required, or returns error code 1.

Entry points:

  • main: For template invocation.
  • genlink: For module invocation.

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

function p.genlink(args)
	if args[1] == nil then
		return { errno = 1 }
	local sep = args.sep or ';'
	local firstarg = args.firstarg or 1
	if tonumber(firstarg) >= 2 and (not args.maxargs) then
		return { errno = 2 }
	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
		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 ', '')
					prefix = ''
				table.insert(converted_names, f:preprocess(prefix .. '[[' .. name .. ']]'))
		table.insert(output_array, table.concat(converted_names, ' or<br>'))
	return table.concat(output_array, ' +<br>')

function p.main()
	local args = require('Module:ProcessArgs').merge(true)
	local result = p.genlink(args)
	if type(result) == 'string' then
		return result
	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] } }

return p