Module:Link label

From Coral Island Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Link label/doc

local p = {}

local TERMS = {
	' %(flower%)',
	' %(group%)',
	' %(character%)',
	' %(game%)',
	' %(item%)',
	' %(artisan product%)',
	' %(cooked dish%)',
}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrapper = { 'Template:Link label' }
	})
	return p._main(args)
end

function p.getTerms()
	local result = mw.html.create()
	for _, term in ipairs(TERMS) do
		local replacedTerm, _ = term:gsub('%%', '')
		result:newline():wikitext('* '):tag('code'):wikitext(replacedTerm):done()
	end
	return result
end

function p._main(args)
	local link = type(args) == 'table' and args[1] or args
	if (link == nil or type(link) == 'table') then return '' end
	
	link = tostring(link)
	if (link:find('%|')) then return link end
	-- if link contains multiple links
	local _, count = link:gsub('%[%[', '')
	if (count > 1) then return link end
	
	if (link:find('%[%[') and link:find('%]%]')) then
		link = link:gsub('%[%[', ''):gsub('%]%]', '')
	end
	
	local label = link
	for _, term in ipairs(TERMS) do
		if (link:find(term)) then label = link:gsub(term, '') end
	end
		
	return '[[' .. link .. '|' .. label .. ']]'
end

return p