Module:ItemLink

From Primal Fear Wiki
Jump to navigation Jump to search

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

local p = {}

local Arguments = require('Module:Arguments')
local DissectDlcItemName = require('Module:DissectDlcItemName')


function p.mainW(f)
	local args = Arguments.getArgs(f, {
		trim = true,
		removeBlanks = true,
	})
	return p.create(args[1] or '1', {
		link = args.link,
		image = args.image,
		size = args.size,
		text = args.text or args[2],
		quantity = args.quantity,
		noDlcIcon = args.noDlcIcon ~= nil,
		noLink = args.noLink ~= nil,
	})
end


function p.create(target, args)
	args = args or {}
	
	assert(target ~= nil, 'item link target must not be a null value')
	
	local linksCore = false
	if target:sub(1, 6) == 'arkgg:' then
		linksCore = true
		target = target:sub(7)
	end
	
	local text = args.text or target
	local linkTarget = args.link or target
	local iconSize = args.size or '20x20px'
	local iconFileName = args.image or (target..'.png')

	if linksCore then
		linkTarget = 'arkgg:'..linkTarget
	end

	-- Escape characters ":+/" in the icon file name
	iconFileName = iconFileName:gsub('[:%+/]', {
		[":"] = "_",
		["+"] = "-",
		["/"] = "_",
	})

	-- If display text had not been specified explicitly by the caller, retrieve it from the target
	if not args.text then
		local title = mw.title.new(text)
		text = title and title.text or text
	end

	-- Build the final output
	local out = string.format('[[File:%s|%s|class=itemlink|alt=|link=%s]] [[%s|%s]]',
							  iconFileName, iconSize, linkTarget, linkTarget, text)
	if args.noLink then
		out = string.format('[[File:%s|%s|class=itemlink|alt=|link=]] %s',
							iconFileName, iconSize, text)
	end
	if args.quantity then
		out = args.quantity..' '..(args.quantitySign or '×')..' '..out
	end
	return out
end


return p