Module:IL

From Fallen London Wiki (Staging)

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

local p = {}

local workarounds = require('Module:Workarounds')

--[[ 
	--- Generates an image link with optional arguments for customization.

	-- @see the documentation for the {{IL}} template for more information.

	-- @param page The name of the page the image is associated with.
	-- @param quantity (optional) The quantity of items to display.
	-- @param appearance (optional) The appearance or label to use instead of the page name.
	-- @param property (optional) A Semantic MediaWiki property to set for the page.
	-- @param image (optional) The file name of the image to use. If not provided, it attempts to fetch from SMW.
	-- @param size (optional) The size of the image. Defaults to '20px' if not provided.
	-- @param link (optional) The link target for the image. If not provided, defaults to the page name.
	-- @param small (optional) If set to 'yes', applies a smallification workaround to the image.
	-- @return A string representing the HTML output for the image link.
]]
function p.IL_with_args(page, quantity, appearance, property, image, size, link, small)
	if not appearance then
		if short and short == 'yes' then
			-- remove everything before the first :
			appearance = string.match(page, ':(.*)')
		end
	end
	
	-- if property is present, set it. Thank god, no more weird {{Property}}:: hacks
	if property then
		mw.smw.set({property, page})
	end

	if not image then
		image = mw.smw.ask({'[[' .. page .. ']]', '?Has icon'}, 'default=unknown')
		if image and image[1]["Has icon"] then
			image = string.match(image[1]["Has icon"], '%[%[File:(.-)|')
		else
			image = 'Question.png'
		end
	end
	
	if not size then
		if small == 'yes' then
			size = '24px'
		else
			size = '20px'
		end
	end

	-- if link is present |link= should be the image link, otherwise it should be the page link.
	-- Due to the way the interpolation code works its easier to just hack this in here

	if link then
		link = image
	else 
		link = page
	end
	
	if small == 'yes' then
		image = workarounds.smallify(image)
	end

	-- get the game type. Have I mentioned yet I hate smw.ask?
	local game_type = mw.smw.ask({'[[' .. page .. ']]', '?Has Game Type'})
	if game_type and game_type[1]['Has Game Type'] then
		game_type = game_type[1]['Has Game Type']
	else
		game_type = 'Unknown'
	end

	-- css class to apply to the span
	local class = 'il' .. (small == 'yes' and '-small' or '') .. '-' .. string.lower(game_type)

	local image_link = '[[File:' .. image .. '|' .. size .. '|link=' .. link .. '|alt=]]'
	local page_link_text = '[[' .. page .. (appearance and '|' .. appearance or '') .. ']]'

	

	-- replace !! with !!, probably to escape it? This was already in the original template: I'm blaming [[Tale of Terror!!]]
	page_link_text = string.gsub(page_link_text, '!!', '!!')

	if quantity then
		page_link_text = quantity .. '  ' .. page_link_text
	end

	local output = mw.html.create('span')
		:addClass(class)
	
	if appearance == '' then
		output
			:wikitext(image_link)
	else
		output
			:wikitext(image_link .. ' ' .. page_link_text)
	end

	return tostring(output)
end

--[[ 
	--- Wrapper function for IL_with_args to handle frame arguments from a template call.

	-- @see the documentation for the {{IL}} template for more information.

	-- @param frame The frame object from the invoking template.
	-- @return A string representing the HTML output for the image link.
]]
function p.IL(frame)
	local page = workarounds.get_arg(frame, 1)
	local quantity = workarounds.get_arg(frame, 2)
	local appearance = frame.args.Appearance -- Appearance is weird and needs an workaround, because having it set to a
											 -- value of '' is valid for not showing anything. Instead we set the special
											 -- value of nil-appearance in the template if it isn't used, and check for that  	
	local property = workarounds.get_arg(frame, 'Property')
	local image = workarounds.get_arg(frame, 'Image')
	local size = workarounds.get_arg(frame, 'Size')
	local link = workarounds.get_arg(frame, 'Link')
	local small = workarounds.get_arg(frame, 'Small')

	if not page then
		return '<span class="error">No image defined. Please supply an image in the <code>{{{1}}}</code> parameter. If you do not know what this message means please report it on [[Forum:Project Discussion|the Forum]]. Thanks in advance.</span>[[Category:Pages using IL without image parameter]]'
	end

	if apppearance == 'nil-appearance' then
		appearance = nil
	end

	return p.IL_with_args(page, quantity, appearance, property, image, size, link, small)	
end
	
return p