Thorium Mod Wiki
Register
Advertisement

This module is intended to provide functionality to the item template.


local function explode(div,str) -- credit: http://richard.warburton.it
	if (div=='') then return false end
	local pos,arr = 0,{}
	-- for each divider found
	for st,sp in function() return string.find(str,div,pos,true) end do
		table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
		pos = sp + 1 -- Jump past current divider
	end
	table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
	return arr
end

local function imagecode(image, link, size)
	local image_output = '[[File:' .. image .. '|link='.. link
	if size then
		return image_output .. '|' .. size .. ']]'
	else
		return image_output .. ']]'
	end
end

local function images(image, link, size)
	
	if not image:find('/') then
		return imagecode(image, link, size)
	end

	image = explode('/', image)
	local result = ''
	if size and size:find('/') then
		size = explode('/', size)
		for k, v in pairs(image) do
			result = result .. imagecode(v, link, size[k])
		end
	else
		for k, v in pairs(image) do
			result = result .. imagecode(v, link, size)
		end
	end
	return result
end

-- main return object
return { go = function(frame, args)

	local getArg = function(key)
		local value
		if args then
			value = args[key]
		else	
			value = frame.args[key]
		end
		if not value then
			return nil
		end
		value = mw.text.trim(value)
		if value == '' then
			return nil
		else
			return value
		end
	end

	local _arg1 = getArg(1) or ''
	local _link = frame.args['link']  -- keep '' input
	local _nolink = getArg('nolink')
	local _link = _nolink and '' or getArg('link') or frame:expandTemplate{ title = 'tr', args = {_arg1, link='y', lang=lang} }

	local class = 'item-link'

	local _mode = getArg('mode')

	local output_image, output_text, output_table = true, true, false
	if _mode then
		if _mode == 'image' or _mode == 'imageonly' or _mode =='onlyimage' then
			output_text = false
		elseif _mode == 'text' or _mode == 'noimage' then
			output_image = false
		elseif _mode == 'table' or _mode == '2-cell' then
			output_table = true
		end
	end

	local image_output = ''
	if output_image then
		image_output = images(getArg('image') or (_arg1 .. '.' .. (getArg('ext') or 'png')), _link, getArg('size'))
	end

	local text_output = ''
	if output_text then
		local _arg2 = getArg(2)
		local _note = getArg('note')
		local _note2 = getArg('note2')


		local text
		if _arg2 then
			if _arg2 == 's' or _arg2 == 'es' then
				text = _arg1 .. _arg2
			elseif _arg2 == 'ies' then
				text = _arg1:sub(0, -2) .. _arg2
			elseif _arg2 == 'ves' then
				text = _arg1:sub(0, -3) .. _arg2
			else
				text = _arg2
			end
		else
			text = _arg1
		end

		local _wrap
		if _note2 then
			_wrap = false
		else
			_wrap = getArg('wrap')
		end


		if _link ~= '' then
			text = '[['.._link..'|'..text..']]'
		end


		local content = mw.text.tag('span', nil, text)
		if _wrap then
			if _note then
				class = 'item-link -w'
				content = content .. mw.text.tag('span',{class='note'}, _note)
			end
		else
			if _note then
				content = content .. mw.text.tag('span',{class='note'}, _note)
			end
			if _note2 then
				class = 'item-link -w'
				content = content .. mw.text.tag('div',{class='note'}, _note2)
			end
		end
		text_output = mw.text.tag{name='span', content=content}
	end

	local _class = getArg('class')
	local _css = getArg('css')

	
	if _class then
		class = class .. ' ' .. _class
	end
	local attr = {class = class}
	if _css then
		attr.style = _css
	end
	
	local anchor = ''
	if getArg('anchor') then
		anchor = mw.text.tag('div', {id=_arg1, class='anchor'}, '')
	end
	if output_table then
		return mw.text.tag('span', attr, image_output) .. '||' .. mw.text.tag('span', attr, text_output .. anchor)
	else
		return mw.text.tag('span', attr, image_output .. text_output .. anchor)
	end
end }
Advertisement