Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Icon

From The Petit Planet Wiki
Revision as of 09:25, 9 November 2025 by Stevium (talk | contribs)

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

-- Source: https://coralisland.fandom.com/wiki/Module:Icon

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

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		removeBlanks = false,
		wrapper = { 'Template:Icon' }
	})
	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 nolink = lib.isNotEmpty(args.nolink) or false
	local notext = lib.isNotEmpty(args.notext) or false
	local link = nolink and '' or (args.link or item)
	local size = tonumber(args.size or args.s or '30')
	local M_size = size
	local upscale
	if M_size < 51 then
		M_size = 51
		
		--figure out what to size to default back down to
		--  multiples of 8 as they wont become decimal in any screen density
		if     size < 24 then upscale = '16'
		elseif size < 32 then upscale = '24'
		elseif size < 40 then upscale = '32'
		elseif size < 48 then upscale = '40'
		else                  upscale = '48'
		end
	end -- 50px and below gets autoformatted into infoicon
	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
	-- mw.logObject(args)
	local prefix = args.prefix or ''
	local suffix = args.suffix or ''
	prefix = prefix:gsub('{space}', ' ')
	suffix = suffix:gsub('{space}', ' ')
	
	local icon = mw.html.create():tag('span'):addClass('custom-icon')
	local icon_image = icon:tag('span'):addClass('custom-icon-image desktop-only')
	local M_icon_image = icon:tag('span'):addClass('custom-icon-image mobile-only hidden noborder')
	local icon_text = icon:tag('span'):addClass('custom-icon-text')
	
	if upscale then
		M_icon_image:addClass('upscaled upscaled-' .. upscale)
	end
	
	-- main icon image
	local filename = table.concat({prefix, item, suffix})
	icon_image:wikitext('[[File:', filename, '.', ext, '|', size, 'px|link=', link, ']]')
	M_icon_image:wikitext('[[File:', filename, '.', ext, '|', M_size, 'px|link=', link, ']]')
	
	-- auto link unless disable
	if not notext then
		if link == '' then icon_text:wikitext(' ', item)
		elseif link == item then icon_text:wikitext(' ', LL(item))
		else icon_text:wikitext(' [[', link, '|', item, ']]')
		end
	end
	
	-- append text if any
	if lib.isNotEmpty(text) then
		icon_text:wikitext(' ', text)
	end
	
	-- amount if any
	if lib.isNotEmpty(amount) then
		amount = tonumber(amount) ~= nil and lib.thousandsSeparator(amount) or amount
		icon_text:wikitext(' × ', amount)
	end
	
	-- ending note if any
	if lib.isNotEmpty(note) then
		icon_text:wikitext(' (', note, ')')
	end
	-- mw.logObject(icon) -- debug
	return icon
end

return p