WoWWiki

This wiki contains inaccurate and out-of-date information. Please head over to https://wowpedia.fandom.com for more accurate and up-to-date game information.

READ MORE

WoWWiki
Advertisement

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

local p = {}
 
local type_img_data = mw.loadData( 'Module:Petfamilylink/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 getfamilyInfo(family_type, family_piece)
    family_type = strlower(family_type)
 
    local family_DB = mw.loadData("Module:Petfamilylink/link_icon_data.json")
    local family_chunk = family_DB[family_type]
 
    if family_chunk and family_DB[family_type][family_piece] then
       return family_DB[family_type][family_piece]
    end
end
 
function p.Petfamilylink(frame) -- Implements {{Petfamilylink}}
	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._Petfamilylink(args, icononly, useicon, medium, large, xlarge, float)
end
 
function p._Petfamilylink( args, showonlyicon, showicon, showmedium, showlarge, showxlarge, floating )
	local family_index = args['type'] -- use type param for type
	if args['type'] == nil then -- use param 1, if no type=
	    family_index = args[1]
	elseif args[1] == nil then -- blank type if no param 1
	    family_index = ''
	end
	if family_index ~= nil then -- if not nil, make lowercase
		family_index = string.lower(family_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 family_link = 'Battle pet family'
	local label_tooltip = "''Unknown battle pet family''"
	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=' .. family_link .. ']]'
 
    if family_index ~= nil then
        -- strip spaces for indexing
        family_index = mw.ustring.gsub(family_index, "%s", "")
 
        -- strip apostrophes for indexing
        family_index = mw.ustring.gsub(family_index, "\'", "")
 
        -- strip dashes for indexing
        family_index = mw.ustring.gsub(family_index, "\-", "")
    end
 
	-- Get image size change if it exists
	if family_index ~= nil and getfamilyInfo(family_index, "size") ~= nil then
	    img_size = getfamilyInfo(family_index, "size")
	end
 
	-- Get link (also for for image)
	if family_index ~= nil and getfamilyInfo(family_index, "link") ~= nil then
	    family_link = getfamilyInfo(family_index, "link")
	end
 
	-- Get label/tooltip
	if family_index ~= nil and getfamilyInfo(family_index, "label") ~= nil then
	    label_tooltip = getfamilyInfo(family_index, "label")
	end
 
    -- Get image filename and build image wikitext
	if family_index ~= nil and getfamilyInfo(family_index, "img") ~= nil then
	    	img_filename = getfamilyInfo(family_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 getfamilyInfo(family_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=' .. family_link .. ']]'
    end
 
    -- If label= passed then use it for what link shows
    if link_label ~= nil then
        label_tooltip = link_label
    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 .. ' [[' .. family_link .. '|' .. label_tooltip .. ']]'
	local link_only = '[[' .. family_link .. '|' .. label_tooltip .. ']]'
 
	-- 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
Advertisement