Module:Icon

From Sun Haven Wiki
Jump to navigation Jump to search

This module is used in the {{Icon}} template. This module is also a dependency in Module:Icon list.


Subpages

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 link = args.nolink and '' or (args.link or item)
	local size = tonumber(args.size or args.s or '30')
	local upscale
		
	--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
	
	local ext = args.ext or 'png'
	local amount = args.amount or args.x or args[2]
	local text = args.text or args[3]
    local textPrefix = args.textPrefix
	local note = args.note
	local nolink = args.nolink or false
	local notext = args.notext or false
	local br = args.br
	-- 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'):attr('data-item', item:gsub("'", ""))
	local icon_image = icon:tag('span'):addClass('custom-icon-image')
	local icon_text = icon:tag('span'):addClass('custom-icon-text')
	
	-- main icon image
	local filename = table.concat({prefix, item, suffix})
	icon_image:wikitext('[[File:', filename, '.', ext, '|', size, 'px|link=', link, ']]')
	
	-- prepend some descriptor before the link
	if lib.isNotEmpty(textPrefix) then
        icon_text:wikitext(' ', textPrefix)
    end
	
	-- 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
	
	-- putting icon on its own line
	if lib.isNotEmpty(br) then
		icon_text:wikitext('<br>')
	end
	-- mw.logObject(icon) -- debug
	return icon
end

return p