FANDOM


-- Mostly done
 
local p = {}
 
local spec_img_data = mw.loadData( 'Module:Speclink/link_icon_data.json' ) -- load json
local getArgs = require('Dev:Arguments').getArgs
local strlower = string.lower -- Convert to lower case
 
-- Get data from json page given main spec name and piece name
function getspecInfo(spec_type, spec_piece)
	spec_type = strlower(spec_type)
 
	local spec_DB = mw.loadData("Module:Speclink/link_icon_data.json")
	local spec_chunk = spec_DB[spec_type]
 
	if spec_chunk and spec_DB[spec_type][spec_piece] then
	   return spec_DB[spec_type][spec_piece]
	end
end
 
function p.Speclink(frame) -- Implements {{Speclink}}
	local args = getArgs(frame, {
	trim = false,
	removeBlanks = false
	})
	local classadd = nil
	local classwithparens = nil
	local icononly = nil
	local useicon = nil
	local medium = false
	local large = false
	local xlarge = false
	local float_check = args["float"]
	local float = nil
 
	if args["full"] then
		classadd = true
 
		if args["parens"] then
			classwithparens = true
		else
			classwithparens = false
		end
	else
		classadd = false
		classwithparens = false
	end
 
	if args["icononly"] then
		icononly = true
	else
		useicon = false
 
		if args["icon"] then
			useicon = true
		else
			useicon = false
		end
	end
 
	if float_check ~= nil then
		float_check = strlower(float_check)
 
		if float_check == "left" then
			float="left"
		end
		if float_check == "right" then
			float="right"
		end
		if float_check == "thumb" then
			float="thumb"
		end
	end
 
	if args["medium"] then
		medium = true
	end
	if args["large"] then
		large = true
	end
	if args["xlarge"] then
		xlarge = true
	end
 
	return p._Speclink(args, classadd, classwithparens, icononly, useicon, medium, large, xlarge, float)
end
 
function p._Speclink( args, showclass, classinparens, showonlyicon, showicon, showmedium, showlarge, showxlarge, floating )
	local class_frag = args['class'] -- use class param for class
	if args['class'] == nil then -- use param 1, if no class=
		class_frag = args[1]
	elseif args[1] == nil then -- blank spec if no param 1
		class_frag = ''
	end
	if class_frag ~= nil then -- if not nil, make lowercase
		class_frag = string.lower(class_frag)
	end
 
	local spec_index = args['spec'] -- use spec param for spec
	if args['spec'] == nil then -- use param 2, if no spec=
		spec_index = args[2]
	elseif args[2] == nil then -- blank spec if no param 2
		spec_index = ''
	end
	if spec_index ~= nil then -- if not nil, make lowercase
		spec_index = string.lower(spec_index)
	end
 
	local img_filename = 'Inv_misc_questionmark.png'
	local img_size = '20px'
	if showmedium then
		img_size = '32px'
	elseif showlarge then
		img_size = '48px'
	elseif showxlarge then
		img_size = '64px'
	end
	local spec_link = 'Specialization'
	local label = 'Unknown spec'
	local class_label = 'Unknown class'
	local ttip = 'Unknown spec'
	local link_label = nil
	if args['label'] ~= nil then
		link_label = args['label'] -- use label param for spec
	elseif args['label'] == nil and args[3] ~= nil then -- use param 3, if no label=
		link_label = args[3]
	end
 
	-- Build beginning default image wikitext
	local img_wikitext = '[[File:' .. img_filename .. '|' .. img_size
	---- Add floating, if icon only
	if floating ~= nil and showonlyicon then
		img_wikitext = img_wikitext .. '|' .. floating
	end
	---- Add default tooltip and link
	img_wikitext = img_wikitext .. '|' .. label ..'|link=' .. spec_link .. ']]'
 
	-- Normalize class_frag by stripping spaces 
	-- (should not have apostrophes or dashes)
 	if class_frag ~= nil then
		-- strip spaces for indexing
		class_frag = mw.ustring.gsub(class_frag, "%s", "")
 
		-- If spec_index not set, set it to class_frag
		if spec_index == nil then
			spec_index = class_frag
		else -- otherwise prepend class_frag to spec_index
			spec_index = class_frag .. spec_index
		end
	end
 
	-- Normalize spec_index by stripping spaces, apostrophes, and dashes
	if spec_index ~= nil then
 
		-- strip spaces for indexing
		spec_index = mw.ustring.gsub(spec_index, "%s", "")
 
		-- strip apostrophes for indexing
		spec_index = mw.ustring.gsub(spec_index, "\'", "")
 
		-- strip dashes for indexing
		spec_index = mw.ustring.gsub(spec_index, "\-", "")
 
 
		if getspecInfo(spec_index, "alias") ~= nil then
			spec_index = getspecInfo(spec_index, "alias")
		end
	end
 
	-- Get image size change if it exists
	if spec_index ~= nil and getspecInfo(spec_index, "size") ~= nil then
		img_size = getspecInfo(spec_index, "size")
	end
 
	-- Get link (also for for image)
	if spec_index ~= nil and getspecInfo(spec_index, "link") ~= nil then
		spec_link = getspecInfo(spec_index, "link")
	end
	-- Override JSON link if link param used
	if args['link'] ~= nil then
		spec_link = args['link']
	end
 
	-- Get label
	if spec_index ~= nil and getspecInfo(spec_index, "label") ~= nil then
		label = getspecInfo(spec_index, "label")
	end
 
	-- Get class if full=
	if showclass and spec_index ~= nil and getspecInfo(spec_index, "class") ~= nil then
		class_label = getspecInfo(spec_index, "class")
	end
 
	-- Get tooltip
	if spec_index ~= nil and getspecInfo(spec_index, "tip") ~= nil then
		ttip = getspecInfo(spec_index, "tip")
	elseif spec_index ~= nil and getspecInfo(spec_index, "tip") == nil and getspecInfo(spec_index, "label") ~= nil then
		ttip = label
	end
 
	-- Get image filename and build image wikitext
	if spec_index ~= nil and getspecInfo(spec_index, "img") ~= nil then
			img_filename = getspecInfo(spec_index, "img") -- Get image filename
 
			-- Change size to 32px if medium= was specified
			if showmedium then
				img_size = '32px'
			-- Or to 48px if large= was specified
			elseif showlarge then
				img_size = '48px'
			-- Or to 64px if xlarge= was specified
			elseif showxlarge then
				img_size = '64px'
			end
 
			-- Build beginning image wikitext starting with filename and size if specified in JSON
			if getspecInfo(spec_index, "size") ~= nil or showmedium or showlarge then -- add size, if it exists
				img_wikitext = '[[File:' .. img_filename .. '|' .. img_size
			else
				img_wikitext = '[[File:' .. img_filename
			end
 
			-- Add float, if icon only
			if floating ~=nil and showonlyicon then
				img_wikitext = img_wikitext .. '|' .. floating
			end
 
			-- Add tooltip and link
			img_wikitext = img_wikitext .. '|' .. ttip .. '|link=' .. spec_link .. ']]'
	end
 
	-- If label= passed then use it for what link shows
	if link_label ~= nil then
		label = link_label
	end
 
	if showclass then -- Add class name to spec name if full=
		if classinparens then -- Put class in parens if parens=
			label = label .. ' (' .. class_label .. ')'
		else
			label = label .. ' ' .. class_label
		end
	end
 
 
	local final_link = nil
	-- Build variants
	local img_only_link = img_wikitext
	local img_with_link = img_wikitext .. ' [[' .. spec_link .. '|' .. label .. ']]'
	local link_only = '[[' .. spec_link .. '|' .. label .. ']]'
 
	-- Determine which variant to return
	if showonlyicon then
		final_link = img_only_link
	else
		if showicon then
			final_link = img_with_link
		else
			final_link = link_only
		end
	end
 
	-- Return variation wikitext
	return final_link
end
 
return p
Community content is available under CC-BY-SA unless otherwise noted.