Module:Test

local p = {}

table.indexOf = function(t, object) if "table" == type(t) then for i = 1, #t do        if object == t[i] then return i        end end

return -1 else error("table.indexOf expects table for first argument, " .. type(t) .. " given") end end

local function rfind(haystack, needle) local i = string.find(string.reverse(haystack), needle, 1, true) if i then i = string.len(haystack) - i  end return i end

local function lrtrim(s) local a = s:match('^%s*') local b = s:match('%s*$', a)  s = s:sub(a, b-1) if s:sub(1, 1) == '|' then s = s:sub(2) end if s:sub(-1) == '|' then s = s:sub(1, -2) end

a = s:match('^%s*') b = s:match('%s*$', a)  return s:sub(a, b-1) end

local function explode(str, delim) if (delim == '') then return false end local pos = 0 local arr = {}

for st, sp in function return string.find(str, delim, pos, true) end do     table.insert(arr, string.sub(str, pos, st - 1)) pos = sp + 1 end

table.insert(arr, string.sub(str, pos)) return arr end

local function parseLink(str) local b = 0 local e = 0 local i = 0 str = lrtrim(str) b = str:find(, 1, true)  if b then      e = str:find(, b, true) end if b > 0 and e > 0 then str = str:sub(b + 2, e - 1) i = str:find('|', 1, true) if i then return str:sub(1, i - 1) else return str end else return str end end

local function parseRequirements(str) local ingred = {} local ing = '' local i = 0 str = lrtrim(str) ingred = explode(str, ' ')

for i, ing in ipairs(ingred) do     ingred[i] = parseLink(ing) end return ingred end

local function getNextProduct(str, idx, page) local row = string.sub(str, 1, idx - 1) local i = 0 local j = 0 local product = '' local result = '' local image  = '' local level  = '' local price  = '' local duration = '' local experience = '' local source = '' local requirements = {} local requirement = '' --Product name i = string.find(row, , 1, true)  if i then      j = string.find(row, , i, true) if j then product = string.sub(row, i, j + 1) row = string.sub(row, j + 2) end end

--Image i = string.find(row, , 1, true)  if i then      j = string.find(row, , i, true) if j then image = string.sub(row, i, j + 1) row = string.sub(row, j + 2) end end

--Level i = string.find(row, 'Level', 1, true) if i then j = string.find(row, '|', i, true) if j then level = tonumber(string.sub(row, i + 6, j - 1)) row = string.sub(row, j + 1) end end

--Price price = string.match(row, "%d+") if price then row = string.sub(row, string.len(price) + 1) price = tonumber(price) end --Duration i = string.find(row, '|', 1, true) if i then j = string.find(row, '|', i + 1, true) if j then duration = string.sub(row, i + 1, j - 1) row = string.sub(row, j + 1) end end

--Experience experience = string.match(row, "%d+") if price then row = string.sub(row, string.len(price) + 1) experience = tonumber(experience) end

--Source i = rfind(row, '[[')

if i then j = string.find(row, ']]', i, true) if j then source = string.sub(row, i, j + 1) row = string.sub(row, 1, i - 1) end end

--Requirements requirements = parseRequirements(row) for i, requirement in ipairs(requirements) do     if requirement == page then return { source, product, image } end end

return {} end

local function formatOutput(arr) return          .. arr[3] .. ' ' .. arr[2] .. ' ' end

function p.list(frame) local master = frame:expandTemplate{ title = ':Master List' } local keys = {} local result = {} local tmp = {} local idx = 0 local cnt = 0 local page = frame.args['page'] local source = '' local final = '' idx = string.find(master, '{|', 1, true) if idx then master = string.sub(master, idx) else return '' end idx = string.find(master, '|}', 1, true) if idx then master = string.sub(master, 1, idx + 1) else return '' end while string.find(master, '|-', 1, true) do     idx = string.find(master, '|-', 1, true)

if string.sub(master, 1, 2) ~= '{|' then tmp = getNextProduct(master, idx, page) if tmp[1] then cnt = table.indexOf(keys, tmp[1])

if cnt > -1 then result[cnt] = result[cnt] .. formatOutput(tmp) else cnt = #keys + 1 keys[cnt] = tmp[1] result[cnt] = formatOutput(tmp) end end end

master = string.sub(master, idx + 2) end tmp = getNextProduct(master, -2, page) if tmp[1] then cnt = table.indexOf(keys, tmp[1]) if cnt > -1 then result[cnt] = result[cnt] .. formatOutput(tmp) else cnt = #keys + 1 keys[cnt] = tmp[1] result[cnt] = formatOutput(tmp) end end final =       for idx, requirements in ipairs(result) do     final = final ..        .. "The '''" .. keys[idx] .. "''' " .. requirements end

final = final .. ' '  return frame:preprocess(final) end return p