FANDOM


local p = {}
 
local type_img_data = mw.loadData( 'Module:Classlink/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 type name and piece name
function getclassInfo(class_type, class_piece)
    class_type = strlower(class_type)
 
    local class_DB = mw.loadData("Module:Classlink/link_icon_data.json")
    local class_chunk = class_DB[class_type]
 
    if class_chunk and class_DB[class_type][class_piece] then
       return class_DB[class_type][class_piece]
    end
end
 
function p.Classlink(frame) -- Implements {{Classlink}}
	local args = getArgs(frame, {
    trim = false,
    removeBlanks = false
    })
    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["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._Classlink(args, icononly, useicon, medium, large, xlarge, float)
end
 
function p._Classlink( args, showonlyicon, showicon, showmedium, showlarge, showxlarge, floating )
	local class_index = args['class'] -- use class param for class
	if args['class'] == nil then -- use param 1, if no class=
	    class_index = args[1]
	elseif args[1] == nil then -- blank type if no param 1
	    class_index = ''
	end
	if class_index ~= nil then -- if not nil, make lowercase
		class_index = string.lower(class_index)
    end
	local img_filename = 'Inv_misc_questionmark.png'
	local img_size = '21px'
    if showmedium then
        img_size = '32px'
    elseif showlarge then
        img_size = '48px'
    elseif showxlarge then
        img_size = '64px'
    end
	local class_link = 'Class'
	local link_override = nil
	if args['link'] ~= nil then
		link_override = args['link'] -- use link param for final link override
	end
	local label_tooltip = 'Class'
	local link_label = nil
	if args['label'] ~= nil then
	    link_label = args['label'] -- use label param for type
	elseif args['label'] == nil and args[2] ~= nil then -- use param 2, if no label=
	    link_label = args[2]
	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_tooltip .. '|link=' .. class_link .. ']]'
 
    if class_index ~= nil then
        -- strip spaces for indexing
        class_index = mw.ustring.gsub(class_index, "%s", "")
 
        -- strip apostrophes for indexing
        class_index = mw.ustring.gsub(class_index, "\'", "")
 
        -- strip dashes for indexing
        class_index = mw.ustring.gsub(class_index, "\-", "")
    end
 
	-- Get image size change if it exists
	if class_index ~= nil and getclassInfo(class_index, "size") ~= nil then
	    img_size = getclassInfo(class_index, "size")
	end
 
	-- Get link (also for for image)
	if class_index ~= nil and getclassInfo(class_index, "link") ~= nil then
	    class_link = getclassInfo(class_index, "link")
	end
 
	-- Get label/tooltip
	if class_index ~= nil and getclassInfo(class_index, "label") ~= nil then
	    label_tooltip = getclassInfo(class_index, "label")
	end
 
    -- Get image filename and build image wikitext
	if class_index ~= nil and getclassInfo(class_index, "img") ~= nil then
	    img_filename = getclassInfo(class_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 getclassInfo(class_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 .. '|' .. label_tooltip .. '|link=' .. class_link .. ']]'
    end
 
    -- If label= passed then use it for what link shows
    if link_label ~= nil then
        label_tooltip = link_label
    end
 
	local final_link = nil
 
	-- Build variants
	local img_only_link = img_wikitext
 
	local img_with_link = img_wikitext .. ' [['
	-- Add link or override, if set
	if link_override ~= nil then
		img_with_link = img_with_link .. link_override .. '|' .. label_tooltip .. ']]'
	else
		img_with_link = img_with_link .. class_link .. '|' .. label_tooltip .. ']]'
	end
 
	local link_only = '[['
	-- Add link or override, if set
	if link_override ~= nil then
		link_only = link_only .. link_override .. '|' .. label_tooltip .. ']]'
	else
		link_only = link_only .. class_link .. '|' .. label_tooltip .. ']]'
	end
 
	-- 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.