Tremor Mod Wiki
(Created page with "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 funct...")
 
m (Updated.)
Line 1: Line 1:
 
local trim = mw.text.trim
local function explode(div,str) -- credit: http://richard.warburton.it
 
  +
  +
local currentFrame
  +
 
-- credit: http://richard.warburton.it
  +
-- this version is with trim.
  +
local function explode(div,str)
 
if (div=='') then return false end
 
if (div=='') then return false end
 
local pos,arr = 0,{}
 
local pos,arr = 0,{}
 
-- for each divider found
 
-- for each divider found
 
for st,sp in function() return string.find(str,div,pos,true) end do
 
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
+
table.insert(arr,trim(string.sub(str,pos,st-1))) -- Attach chars left of current divider
 
pos = sp + 1 -- Jump past current divider
 
pos = sp + 1 -- Jump past current divider
 
end
 
end
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
+
table.insert(arr, trim(string.sub(str,pos))) -- Attach chars right of last divider
 
return arr
 
return arr
 
end
 
end
   
local function imagecode(image, link, size)
+
local function imagecode(image, link, text, size, scale)
local image_output = '[[File:' .. image .. '|link='.. link
+
local image_output = '[[File:' .. image .. '|link='.. link .. '|' .. text
if size then
+
if size or scale then
 
local basescale
 
if size then
  +
size, basescale = unpack(explode('*', size))
 
if size == '' then size = nil end
  +
if basescale == '' then basescale = nil end
 
end
 
if not size then
  +
size = currentFrame:callParserFunction( '#imgw', image)..'x'..currentFrame:callParserFunction( '#imgh', image)..'px'
 
end
  +
scale = tonumber(scale or 1) * tonumber(basescale or 1)
  +
if scale ~= 1 then
  +
local str
  +
for i, v in ipairs(explode('x', string.gsub(size, 'px', ''))) do
  +
if str then
 
str = str .. 'x'
 
else
 
str = ''
 
end
 
if v ~= '' then
  +
str = str .. math.ceil(tonumber(v) * tonumber(scale))
  +
end
  +
end
  +
size = str..'px'
  +
end
 
return image_output .. '|' .. size .. ']]'
 
return image_output .. '|' .. size .. ']]'
 
else
 
else
Line 20: Line 50:
 
end
 
end
   
  +
local image_for_cargo
local function images(image, link, size)
+
local function images(image, link, text, size, scale)
 
 
 
if not image:find('/') then
 
if not image:find('/') then
  +
image_for_cargo = image
return imagecode(image, link, size)
+
return imagecode(image, link, text, size, scale)
 
end
 
end
   
Line 30: Line 62:
 
if size and size:find('/') then
 
if size and size:find('/') then
 
size = explode('/', size)
 
size = explode('/', size)
for k, v in pairs(image) do
+
for i, v in ipairs(image) do
result = result .. imagecode(v, link, size[k])
+
result = result .. imagecode(v, link, text, size[i], scale)
 
end
 
end
 
else
 
else
for k, v in pairs(image) do
+
for i, v in ipairs(image) do
result = result .. imagecode(v, link, size)
+
result = result .. imagecode(v, link, text, size, scale)
 
end
 
end
 
end
 
end
 
return result
 
return result
 
end
 
end
  +
  +
   
 
-- main return object
 
-- main return object
return { go = function(frame)
+
return { go = function(frame, args)
  +
currentFrame = frame -- cache
  +
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)
 
local getArg = function(key)
  +
if args then
local value = frame.args[key]
 
 
return args[key]
if not value then
 
return nil
 
end
 
value = mw.text.trim(value)
 
if value == '' then
 
return nil
 
 
else
 
else
return value
+
local value = frame.args[key]
  +
if not value then
  +
return nil
  +
end
  +
value = trim(value)
 
if value == '' then
  +
return nil
  +
else
  +
return value
  +
end
 
end
 
end
 
end
 
end
   
 
local _arg1 = getArg(1) or ''
 
local _arg1 = getArg(1) or ''
local _link = mw.text.trim(frame.args['link']) -- keep '' input
+
local _link = trim(frame.args['link'] or args['link'] or '') -- keep '' input
  +
 
local _arg2 = getArg(2)
  +
local text
  +
if _arg2 then
  +
-- intuitive pluralism
  +
-- The comparisons are performed in order, so putting the more common ones in front gives the best performance.
  +
if _arg2 == 's' then
  +
local suffix1 = _arg1:sub(-1) -- cache for better performance.
  +
local suffix2 = _arg1:sub(-2) -- cache for better performance.
  +
if suffix1 == 'y' then
  +
if suffix2 == 'ay' or suffix2 == 'ey' or suffix2 == 'iy' or suffix2 == 'oy' or suffix2 == 'uy' then
  +
text = _arg1 .. 's' -- eg. ray->rays
  +
else
 
text = _arg1:sub(1, -2) .. 'ies' -- eg. firefly->fireflies
  +
end
  +
elseif suffix2 == 'fe'then
 
text = _arg1:sub(1, -3) .. 'ves' -- eg. knife->knives
  +
elseif suffix1 == 'f' and suffix2 ~= 'ff' then
  +
text = _arg1:sub(1, -2) .. 'ves' -- eg. wolf->wolves, leaf->leaves, but buff->buffs
  +
else
 
text = _arg1 .. 's'
  +
end
  +
elseif _arg2 == 'es' then
  +
text = _arg1 .. 'es'
  +
elseif _arg2 == 'ies' then
  +
text = _arg1:sub(1, -2) .. 'ies'
  +
elseif _arg2 == 'ves' then
  +
if _arg1:sub(-2) == 'fe' then
  +
text = _arg1:sub(1, -3) .. 'ves' --replace last 2 letters, e.g. knife->knives
  +
elseif _arg1:sub(-1) == 'f' or _arg1:sub(-2) ~= 'ff' then
  +
text = _arg1:sub(1, -2) .. 'ves' --replace last letter, e.g. wolf-wolves, leaf->leaves, but buff->buffs
  +
end
  +
else
  +
text = _arg2
  +
end
  +
else
  +
text = _arg1
  +
end
   
 
local class = 'item-link'
 
local class = 'item-link'
Line 77: Line 168:
 
local image_output = ''
 
local image_output = ''
 
if output_image then
 
if output_image then
image_output = images(getArg('image') or (_arg1 .. '.' .. (getArg('ext') or 'png')), _link, getArg('size'))
+
image_output = images(getArg('image') or (_arg1 .. '.' .. (getArg('ext') or 'png')), _link, text, getArg('size'), getArg('scale'))
 
end
 
end
   
 
local text_output = ''
 
local text_output = ''
 
if output_text then
 
if output_text then
local _arg2 = getArg(2)
 
 
local _note = getArg('note')
 
local _note = getArg('note')
 
local _note2 = getArg('note2')
 
local _note2 = getArg('note2')
  +
local _id = getArg('id')
 
 
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
 
local _wrap
if _note2 then
+
if _id or _note2 then
 
_wrap = false
 
_wrap = false
 
else
 
else
Line 111: Line 186:
   
 
if _link ~= '' then
 
if _link ~= '' then
text = '[['.._link..'|'..text..']]'
+
if text == _link then
  +
text = '[['..text..']]'
  +
else
  +
text = '[['.._link..'|'..text..']]'
  +
end
 
end
 
end
   
  +
local _icon = getArg('icons')
  +
local icon = ''
  +
if not (_icon == 'n' or _icon == 'no' or _icon == 'off') then
  +
-- eicons should base on _arg1 first.
  +
local t
  +
if _arg1 == '' then
  +
t = _link
  +
else
  +
t = _arg1
  +
end
  +
local args = {t}
  +
if _id or _note2 or _wrap or getArg('small') then
  +
args['small'] = 'y'
  +
end
  +
icon = frame:expandTemplate{ title = 'eicons', args = args }
  +
end
   
 
local content = mw.text.tag('span', nil, text)
 
local content = mw.text.tag('span', nil, text)
 
if _wrap then
 
if _wrap then
  +
if icon ~= '' then
  +
class = 'item-link -w'
  +
content = content .. icon
  +
end
 
if _note then
 
if _note then
 
class = 'item-link -w'
 
class = 'item-link -w'
Line 124: Line 223:
 
if _note then
 
if _note then
 
content = content .. mw.text.tag('span',{class='note'}, _note)
 
content = content .. mw.text.tag('span',{class='note'}, _note)
  +
end
  +
if icon ~= '' then
  +
content = content .. icon
 
end
 
end
 
if _note2 then
 
if _note2 then
 
class = 'item-link -w'
 
class = 'item-link -w'
 
content = content .. mw.text.tag('div',{class='note'}, _note2)
 
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 = 'Internal [[Item IDs|Item ID]]: '
  +
if output_image and image_for_cargo then
  +
frame:expandTemplate{ title = 'Item/cargo', args = {name=_arg1, image=image_for_cargo, id=_id} }
  +
end
  +
elseif _type == 'tile' then
  +
id_text = 'Internal [[Tile IDs|Tile ID]]: '
  +
elseif _type == 'wall' then
  +
id_text = 'Internal [[Wall IDs|Wall ID]]: '
  +
elseif _type == 'npc' then
  +
id_text = 'Internal [[NPC IDs|NPC ID]]: '
  +
elseif _type == 'mount' then
  +
id_text = 'Internal [[Mount IDs|Mount ID]]: '
  +
elseif _type == 'buff' or _type == 'debuff' then
  +
id_text = 'Internal [[Buff IDs|Buff ID]]: '
  +
elseif _type == 'projectile' then
  +
id_text = 'Internal [[Projectile IDs|Projectile ID]]: '
  +
elseif _type == 'armor' then
  +
id_text = 'Internal [[Armor IDs|Armor ID]]: '
  +
else
  +
id_text = 'Internal [[Item IDs|Item ID]]: '
  +
if output_image and image_for_cargo then
  +
frame:expandTemplate{ title = 'Item/cargo', args = {name=_arg1, image=image_for_cargo, id=_id} }
  +
end
  +
end
  +
content = content .. mw.text.tag('div', {class='id'}, id_text .. _id)
 
end
 
end
 
end
 
end
Line 147: Line 281:
 
local anchor = ''
 
local anchor = ''
 
if getArg('anchor') then
 
if getArg('anchor') then
anchor = mw.text.tag('div', {id=_arg1, class='anchor'}, '')
+
anchor = mw.text.tag('div', {id=frame:callParserFunction('anchorencode', _arg1), class='anchor'}, '')
 
end
 
end
 
if output_table then
 
if output_table then
  +
attr.class = class .. ' block aligncenter'
return mw.text.tag('span', attr, image_output) .. '||' .. mw.text.tag('span', attr, text_output .. anchor)
 
  +
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
 
else
 
return mw.text.tag('span', attr, image_output .. text_output .. anchor)
 
return mw.text.tag('span', attr, image_output .. text_output .. anchor)

Revision as of 03:56, 11 January 2019

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


local trim = mw.text.trim

local currentFrame

-- 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,trim(string.sub(str,pos,st-1))) -- Attach chars left of current divider
		pos = sp + 1 -- Jump past current divider
	end
	table.insert(arr, trim(string.sub(str,pos))) -- Attach chars right of last divider
	return arr
end

local function imagecode(image, link, text, size, scale)
	local image_output = '[[File:' .. image .. '|link='.. link .. '|' .. text
	if size or scale then
		local basescale
		if size then
			size, basescale = unpack(explode('*', size))
			if size == '' then size = nil end
			if basescale == '' then basescale = nil end
		end
		if not size then
			size = currentFrame:callParserFunction( '#imgw', image)..'x'..currentFrame:callParserFunction( '#imgh', image)..'px'
		end
		scale = tonumber(scale or 1) * tonumber(basescale or 1)
		if scale ~= 1 then
			local str
			for i, v in ipairs(explode('x', string.gsub(size, 'px', ''))) do
				if str then
					str = str .. 'x'
				else
					str = ''
				end
				if v ~= '' then
					str = str .. math.ceil(tonumber(v) * tonumber(scale))
				end
			end
			size = str..'px'
		end
		return image_output .. '|' .. size .. ']]'
	else
		return image_output .. ']]'
	end
end

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

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



-- main return object
return { go = function(frame, args)
	currentFrame = frame -- cache
	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 = trim(value)
			if value == '' then
				return nil
			else
				return value
			end
		end
	end

	local _arg1 = getArg(1) or ''
	local _link = trim(frame.args['link'] or args['link'] or '') -- keep '' input

	local _arg2 = getArg(2)
	local text
	if _arg2 then
	-- intuitive pluralism
	-- The comparisons are performed in order, so putting the more common ones in front gives the best performance.
		if _arg2 == 's' then
			local suffix1 = _arg1:sub(-1) -- cache for better performance.
			local suffix2 = _arg1:sub(-2) -- cache for better performance.
			if suffix1 == 'y' then
				if suffix2 == 'ay' or suffix2 == 'ey' or suffix2 == 'iy' or suffix2 == 'oy' or suffix2 == 'uy' then
					text = _arg1 .. 's'                 -- eg. ray->rays
				else
					text = _arg1:sub(1, -2) .. 'ies'    -- eg. firefly->fireflies
				end
			elseif suffix2 == 'fe'then
				text = _arg1:sub(1, -3) .. 'ves'        -- eg. knife->knives
			elseif suffix1 == 'f' and suffix2 ~= 'ff' then
				text = _arg1:sub(1, -2) .. 'ves'        -- eg. wolf->wolves, leaf->leaves, but buff->buffs
			else
				text = _arg1 .. 's'
			end
		elseif _arg2 == 'es' then
			text = _arg1 .. 'es'
		elseif _arg2 == 'ies' then
			text = _arg1:sub(1, -2) .. 'ies'
		elseif _arg2 == 'ves' then
			if _arg1:sub(-2) == 'fe' then
				text = _arg1:sub(1, -3) .. 'ves'    --replace last 2 letters, e.g. knife->knives
			elseif _arg1:sub(-1) == 'f' or _arg1:sub(-2) ~= 'ff' then
				text = _arg1:sub(1, -2) .. 'ves'    --replace last letter, e.g. wolf-wolves, leaf->leaves, but buff->buffs
			end
		else
			text = _arg2
		end
	else
		text = _arg1
	end

	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'), getArg('scale'))
	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
			-- eicons should base on _arg1 first.
			local t
			if _arg1 == '' then
				t = _link
			else
				t = _arg1
			end
			local args = {t}
			if _id or _note2 or _wrap or getArg('small') then
				args['small'] = 'y'
			end
			icon = frame:expandTemplate{ title = 'eicons', args = args }
		end

		local content = mw.text.tag('span', nil, text)
		if _wrap then
			if icon ~= '' then
				class = 'item-link -w'
				content = content .. 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 .. 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 = 'Internal [[Item IDs|Item ID]]: '
					if output_image and image_for_cargo then
						frame:expandTemplate{ title = 'Item/cargo', args = {name=_arg1, image=image_for_cargo, id=_id} }
					end
				elseif _type == 'tile' then
					id_text = 'Internal [[Tile IDs|Tile ID]]: '
				elseif _type == 'wall' then
					id_text = 'Internal [[Wall IDs|Wall ID]]: '
				elseif _type == 'npc' then
					id_text = 'Internal [[NPC IDs|NPC ID]]: '
				elseif _type == 'mount' then
					id_text = 'Internal [[Mount IDs|Mount ID]]: '
				elseif _type == 'buff' or _type == 'debuff' then
					id_text = 'Internal [[Buff IDs|Buff ID]]: '
				elseif _type == 'projectile' then
					id_text = 'Internal [[Projectile IDs|Projectile ID]]: '
				elseif _type == 'armor' then
					id_text = 'Internal [[Armor IDs|Armor ID]]: '
				else
					id_text = 'Internal [[Item IDs|Item ID]]: '
					if output_image and image_for_cargo then
						frame:expandTemplate{ title = 'Item/cargo', args = {name=_arg1, image=image_for_cargo, 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=frame:callParserFunction('anchorencode', _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 }