Module:Card

From Coral Island Wiki
Jump to navigation Jump to search

This module implements {{Card}}.


local p = {}
local lib = require('Module:Feature')
local LL = require('Module:Link label')._main
local Ilib = require('Module:Icon')

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

function p._main(args)
	local item = args.name or args[1]
	if not item then return mw.html.create() end -- if no input then return blank
	local mini = tostring(args.mini) == '1'
	local link = args.nolink and '' or (args.link or item)
	local ext = args.ext or 'png'
	local amount = args.amount or args.x or args[2]
	local text = args.text or args[3]
	local note = args.note
	local nolink = tostring(args.nolink) == '1'
	local notext = tostring(args.notext) == '1'
	local quality = args.quality or args.q
	quality = Ilib.verifyQuality(args.q)
	
	local prefix = args.prefix or ''
	local suffix = args.suffix or ''
	prefix = prefix:gsub('{space}', ' ')
	suffix = suffix:gsub('{space}', ' ')
	
	local card = mw.html.create():tag('div'):addClass('custom-card noborder ' .. (mini and 'mini' or '') )
	local card_body = card:tag('div'):addClass('custom-card-body')
	local card_image = card_body:tag('div'):addClass('custom-card-image')
	local card_caption = card:tag('div'):addClass('custom-card-caption')
	
	-- main card image
	local filename = table.concat({prefix, item, suffix})
	card_image:wikitext('[[File:', filename, '.', ext, '|51px|link=', link, ']]')
	
	-- quality icon if valid
	if quality then
		card_body:addClass(' quality-' .. quality)
	end
	
	-- amount if any
	if lib.isNotEmpty(amount) then
		amount = tonumber(amount) ~= nil and lib.thousandsSeparator(amount) or amount
		card_body:tag('div'):addClass('custom-card-amount'):wikitext(amount)
	end
	
	-- default caption
	if args.show_caption then
		card_caption:tag('br')
		if link == '' then card_caption:wikitext(item)
		elseif link == item then card_caption:wikitext(LL(item))
		else card_caption:wikitext('[[', link, '|', item, ']]')
		end
	end
	
	-- append custom text if any
	if lib.isNotEmpty(text) then
		card_caption:tag('br')
		card_caption:wikitext(text)
	end
	
	-- ending note if any
	if lib.isNotEmpty(note) then
		card_caption:tag('br')
		card_caption:tag('small'):wikitext('(', note, ')')
	end
	
	-- mw.logObject(card) -- debug
	return card
end

return p