Terraria Wiki
Advertisement

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



-- credit: http://richard.warburton.it
-- this version is with trim.
local function explode(div,str) 
	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,mw.text.trim(string.sub(str,pos,st-1))) -- Attach chars left of current divider
		pos = sp + 1 -- Jump past current divider
	end
	table.insert(arr, mw.text.trim(string.sub(str,pos))) -- Attach chars right of last divider
	return arr
end

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

local image_for_cargo
local function images(image, link, text, size)
	
	if not image:find('/') then
		image_for_cargo = image
		return imagecode(image, link, text, 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, text, size[k])
		end
	else
		for k, v in pairs(image) do
			result = result .. imagecode(v, link, text, size)
		end
	end
	return result
end



-- main return object
return { go = function(frame, args)
	if args then
		for k, v in pairs(args) do
			if v == '' then 
				args[k] = nil
			end
		end
		if args['nolink'] then
			args['link'] = ''
		else
			if not args['link'] then
				args['link'] = args[1]
			end
		end
	end

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

	local _arg1 = getArg(1) or ''
	local _link = mw.text.trim(frame.args['link'] or args['link'] or '') -- keep '' input
	local rawlink = _link --保留一下原始版本供后面eicon使用
	-- 链接目标调 {{tc}} 翻译
	if _link ~= '' then
		_link = frame:expandTemplate{ title = 'tc', args = {_link, pn = 'y'} }
	end

	local _arg2 = getArg(2)
	--显示文字。 中文直接跳过复数后缀修正
	local text
	if _arg2 then
		if _arg2 == 's' or _arg2 == 'es' or _arg2 == 'ies' or _arg2 == 'ves' then
			text = _arg1
		else
			text = _arg2
		end
	else
		text = _arg1
	end
	--tc翻译显示文字
	text = frame:expandTemplate{ title = 'tc', args = {text} }

	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, text, getArg('size'))
	end

	local text_output = ''
	if output_text then

		local _note = getArg('note')
		local _note2 = getArg('note2')
		local _id = getArg('id')

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

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

		local _icon = getArg('icons')
		local icon = ''
		if not (_icon == 'n' or _icon == 'no' or _icon == 'off') then
			-- 此处中文需要更精细的预判以节约对tc的调用
			local t = _link --翻译后的链接版本
			if _arg1 ~= rawlink and _arg1 ~= '' then --有{{{1}}}且{{{1}}}和输入的link不一样
				t = frame:expandTemplate{ title = 'tc', args = {_arg1} }
			end
			icon = require('Module:eicons').icons(frame:expandTemplate{ title = 'getev/raw', args = {t} }, _id or _note2 or _wrap or getArg('small'), frame)
		end

		local content = mw.text.tag('span', nil, text)
		if _wrap then
			if icon ~= '' then
				class = 'item-link -w'
				content = content .. mw.text.tag('span',{class='eico'}, icon)
			end
			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 icon ~= '' then
				content = content .. mw.text.tag('span',{class='eico'}, icon)
			end
			if _note2 then
				class = 'item-link -w'
				content = content .. mw.text.tag('div',{class='note'}, _note2)
			end
			if _id then
				class = 'item-link -w'
				local _type = getArg('type') or 'item'
				_type = _type:lower()
				local id_text
				if _type == 'item' then -- a shortcut for faster
					id_text = '内部[[物品 ID]]:'
					if output_image and image_for_cargo then
						frame:expandTemplate{ title = 'Item/cargo', args = {name=_arg1, image='[[File:'..image_for_cargo..'|link=]]', id=_id} }
					end
				elseif _type == 'tile' then
					id_text = '内部[[图格 ID]]:'
				elseif _type == 'wall' then
					id_text = '内部[[墙 ID]]:'
				elseif _type == 'npc' then
					id_text = '内部 [[NPC ID]]:'
				elseif _type == 'mount' then
					id_text = '内部[[坐骑 ID]]:'
				elseif _type == 'buff' or _type == 'debuff' then
					id_text = '内部[[增益 ID]]:'
				elseif _type == 'projectile' then
					id_text = '内部[[射弹 ID]]:'
				elseif _type == 'armor' then
					id_text = '内部[[盔甲 ID]]:'
				else
					id_text = '内部[[物品 ID]]:'
					if output_image and image_for_cargo then
						frame:expandTemplate{ title = 'Item/cargo', args = {name=_arg1, image='[[File:'..image_for_cargo..'|link=]]', id=_id} }
					end
				end
				content = content .. mw.text.tag('div', {class='id'}, id_text .. _id)
			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
		attr.class = class .. ' block aligncenter'
		local result = mw.text.tag('span', attr, image_output) .. '||'
		attr.class = class .. ' block alignleft'
		return result .. mw.text.tag('span', attr, text_output .. anchor)
	else
		return mw.text.tag('span', attr, image_output .. text_output .. anchor)
	end
end }
Advertisement