FANDOM


-- Mostly done
 
local p = {}
 
local type_img_data = mw.loadData( 'Module:Icongfx/img_label_link_data.json' ) -- load json
local getArgs = require('Module:Arguments').getArgs
local strlower = string.lower -- Convert to lower case
 
-- Get data from json page given main type name and piece name
function getIconInfo(icon_type, icon_piece)
	icon_type = strlower(icon_type)
 
	local icon_DB = mw.loadData("Module:Icongfx/img_label_link_data.json")
	local icon_chunk = icon_DB[icon_type]
 
	if icon_chunk and icon_DB[icon_type][icon_piece] then
	   return icon_DB[icon_type][icon_piece]
	end
end
 
function p.Icongfx(frame) -- Implements {{Icongfx}}
	local args = getArgs(frame, {
		trim = false,
		removeBlanks = false
		})
	local filelink = false
	local nolink = false
 
	if args["filelink"] then
		filelink = true
	end
 
	if args["nolink"] then
		nolink = true
	end
 
	return p._Icongfx(args, filelink, nolink)
end
 
function p._Icongfx( args, stdlink, donotlink )
	local img_type = args['type'] -- use type param for type
	if args['type'] == nil then -- use param 1, if no type=
		img_type = args[1]
	elseif args[1] == nil then -- blank type if no param 1
		img_type = ''
	end
	if img_type ~= nil then -- if not nil, make lowercase
		img_type = string.lower(img_type)
	end
	local img_filename = 'Inv_misc_questionmark.png'
	local img_wikitext = '[[File:' .. img_filename .. '|18px|Unknown icon|link=]]'
	local img_size = nil
	local img_link = 'Unknown icon'
	local label_tooltip = 'Unknown icon'
 
    if img_type ~= nil then
        -- strip spaces for indexing
        img_type = mw.ustring.gsub(img_type, "%s", "")
 
        -- strip apostrophes for indexing
        img_type = mw.ustring.gsub(img_type, "\'", "")
 
        -- strip dashes for indexing
        img_type = mw.ustring.gsub(img_type, "\-", "")
    end
 
	-- Use new img_type, if alias found
	if img_type ~= nil and getIconInfo(img_type, "alias") ~= nil then
		img_type = getIconInfo(img_type, "alias")
	end
 
	-- Get image size change if it exists
	if img_type ~= nil and getIconInfo(img_type, "size") ~= nil then
		img_size = getIconInfo(img_type, "size")
	end
 
	-- Get link for image
	if img_type ~= nil and getIconInfo(img_type, "link") ~= nil then
		img_link = getIconInfo(img_type, "link")
	end
 
	-- Check for link= and use it if passed
	if args['link'] ~= nil then -- use link= for img_link, if passed
		img_link = args['link']
	-- If link JSON is nil, then stdlink
	elseif args['link'] == nil and img_type ~= nil then
		if getIconInfo(img_type, "link") == nil then
			stdlink = true
		end
	end
 
	-- Get label/tooltip
	if img_type ~= nil and getIconInfo(img_type, "label") ~= nil then
		label_tooltip = getIconInfo(img_type, "label")
	elseif img_link ~= nil then -- Use img_link if not in JSON
		label_tooltip = img_link
	end
 
	if args['label'] ~= nil then -- use label= for label_tooltip, if passed
		label_tooltip = args['label']
	end
 
	if args['size'] ~= nil then -- use size= for img_size, if passed
		img_size = args['size']
	end
 
	-- Get image filename and build image wikitext
	if img_type ~= nil and getIconInfo(img_type, "img") ~= nil then
			img_filename = getIconInfo(img_type, "img") -- Get image filename
			-- Build image wikitext with filename, size, and tooltip
			if getIconInfo(img_type, "size") ~= nil then -- add size, if it exists
				img_wikitext = '[[File:' .. img_filename .. '|' .. img_size .. '|' .. label_tooltip
			else
				img_wikitext = '[[File:' .. img_filename .. '|' .. label_tooltip 
			end
 
			-- Add empty link if donotlink is true
			if donotlink and not stdlink then
			    img_wikitext = img_wikitext .. '|link='
			-- Only add link if stdlink is false
			elseif not stdlink then
				img_wikitext = img_wikitext .. '|link=' .. img_link
			end
			-- Finish image wikitext
			img_wikitext = img_wikitext .. ']]'
	elseif img_type == nil then
		img_type = 'none'
	end
 
	-- Return image wikitext
	return img_wikitext
end
 
return p
Community content is available under CC-BY-SA unless otherwise noted.