The Alchemist Code Wiki

READ MORE

The Alchemist Code Wiki
The Alchemist Code Wiki

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

local wikiSafeName = require('Module:LinkUtils').wikiSafeName
local p = {}

function p.test()
	local args = {
		"ItemIcon,IT_UNKNOWN",
		"Zwei",
		"Zwei Shards",
		4,
		3,
		64,
		420,
		"test-class",
		"Extra content",
		true,
	}
	local kwargs = {
		file = "ItemIcon,IT_UNKNOWN",
		name = "Zwei",
		link = "Zwei Shards",
		rare = 4,
		type = 3,
		size = 64,
		count = 420,
		classes = "test-class",
		content = "Extra content",
		inline = true,
	}
	assert(p.icon(kwargs) == p.icon(args), 'icon(kwargs) != icon(args)')
	assert(p.icon(unpack(args)) == p.icon(args), 'icon(...) != icon{...}')
end

function p.icon(...)
	local args = {...}
	if type(args[1]) == 'table' then
		args = args[1]
	end
	local file = args[1] or args.file
	local name = args[2] or args.name
	local link = args[3] or args.link
	local rare = tonumber(args[4] or args.rare)
	local type = tonumber(args[5] or args.type)
	local size = tonumber(args[6] or args.size) or 128
	local count = tonumber(args[7] or args.count)
	local classes = args[8] or args.classes
	local content = args[9] or args.content
	local inline = (args[10] or args.inline) and 'span' or 'div'
	local padding = math.floor(size*14/128)
	size = size - padding

	local sb = mw.html.create(inline)
		:addClass('item-icon')
		:addClass(classes)
	local img = sb:tag(inline)
		:addClass('img')
		:addClass('x-'..(rare or 0))
		:addClass('y-'..(type or 0))
		:css('width', size..'px')
		:css('height', size..'px')
		:css('padding', (padding/2)..'px')
	if count ~= nil then
		img:tag(inline)
			:addClass('item-count')
			:wikitext('×'..count)
	end
	img:wikitext(string.format(
		'[[File:Game,%s.png|%spx%s%s]]',
		file or '',
		size - padding,
		name and name ~= '' and '|'..wikiSafeName(name) or '',
		link and link ~= '' and '|link='..wikiSafeName(link) or ''
	))
	if content ~= nil then
		sb:wikitext(content)
	end
	return tostring(sb)
end

return p