Difference between revisions of "Module:Item"

From blackwiki
Jump to navigation Jump to search
blackwiki>Petr Matas
blackwiki>Petr Matas
(+ gather())
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
function escape(str)
+
local function escape(str)
 
return str:gsub("[|\\]", function (c) return string.format("\\%03d", c:byte()) end)
 
return str:gsub("[|\\]", function (c) return string.format("\\%03d", c:byte()) end)
 
end
 
end
  
function unescape(str)
+
local function unescape(str)
 
return str:gsub("\\(%d%d%d)", function (d) return string.char(d) end)
 
return str:gsub("\\(%d%d%d)", function (d) return string.char(d) end)
 
end
 
end
Line 18: Line 18:
 
end
 
end
  
function unpack(str)
+
local function unpack(str)
 
local result = { }
 
local result = { }
 
for key, value in str:gfind("|([^|]*)|([^|]*)") do
 
for key, value in str:gfind("|([^|]*)|([^|]*)") do
Line 24: Line 24:
 
end
 
end
 
return result
 
return result
 +
end
 +
 +
local function getItems(frame)
 +
return frame:getParent().args
 +
end
 +
 +
local function getTemplate(frame)
 +
return frame.args.template
 
end
 
end
  
 
function p.each(frame)
 
function p.each(frame)
local parent = frame:getParent()
+
local items = getItems(frame)
local items = parent.args
 
 
local separator = frame.args.separator or ""
 
local separator = frame.args.separator or ""
local template = frame.args.template
+
local template = getTemplate(frame)
 
 
 
local result = ""
 
local result = ""
Line 38: Line 45:
 
result = result .. separator
 
result = result .. separator
 
end
 
end
 +
end
 +
return result
 +
end
 +
 +
function p.gather(frame)
 +
local items = getItems(frame)
 +
local template = getTemplate(frame)
 +
local parameter = frame.args.parameter or "1"
 +
 +
local args = { }
 +
for i, item in ipairs(items) do
 +
args[i] = unpack(item)[parameter]
 
end
 
end
 
 
return result
+
return frame:expandTemplate{ title = template, args = args }
 
end
 
end
  
 
return p
 
return p

Revision as of 12:09, 13 December 2015

This template implements {{Item}}, {{Component}} and {{Format item}}.


local p = {}

local function escape(str)
	return str:gsub("[|\\]", function (c) return string.format("\\%03d", c:byte()) end)
end

local function unescape(str)
	return str:gsub("\\(%d%d%d)", function (d) return string.char(d) end)
end

function p.pack(frame)
	local parent = frame:getParent()
	local result = ''
	for key, value in pairs(parent.args) do
		result = result .. "|" .. escape(tostring(key)) .. "|" .. escape(value)
	end
	return result .. "|";
end

local function unpack(str)
	local result = { }
	for key, value in str:gfind("|([^|]*)|([^|]*)") do
		result[unescape(key)] = unescape(value)
	end
	return result
end

local function getItems(frame)
	return frame:getParent().args
end

local function getTemplate(frame)
	return frame.args.template
end

function p.each(frame)
	local items = getItems(frame)
	local separator = frame.args.separator or ""
	local template = getTemplate(frame)
	
	local result = ""
	for i, item in ipairs(items) do
		result = result .. frame:expandTemplate{ title = template, args = unpack(item) }
		if items[i + 1] then
			result = result .. separator
		end
	end
	return result
end

function p.gather(frame)
	local items = getItems(frame)
	local template = getTemplate(frame)
	local parameter = frame.args.parameter or "1"
	
	local args = { }
	for i, item in ipairs(items) do
		args[i] = unpack(item)[parameter]
	end
	
	return frame:expandTemplate{ title = template, args = args }
end

return p