Module:Page

From The Alchemist Code Wiki
Jump to: navigation, search

TODO


-- Utility

local function remove(t, k)
  local v = t[k]
  t[k] = nil
  return v
end

local function removeAll(t, l)
  local r = {}
  for i, k in ipairs(l) do
    r[k] = t[k]
    t[k] = nil
  end
  return r
end

local function setAll(t1, t2)
  for k, v in pairs(t2) do t1[k] = v end
  return t1
end

local function shallowCopy(orig)
  local orig_type = type(orig)
  local copy
  if orig_type == 'table' then
      copy = {}
      for orig_key, orig_value in pairs(orig) do
          copy[orig_key] = orig_value
      end
  else -- number, string, boolean, etc
      copy = orig
  end
  return copy
end

local function isArray(t)
  local i = 0
  for _ in pairs(t) do
    i = i + 1
    if t[i] == nil then return false end
  end
  return true
end

local function makeWikiTable(t)
  local array = isArray(t)
  local sb = {'{|class="wikitable"'}
  for k, v in pairs(t) do
    table.insert(sb, "\n|-\n|")
    if not array then
      table.insert(sb, k)
      table.insert(sb, "\n|")
    end
    table.insert(sb, type(v) == "table" and ("\n"..makeWikiTable(v).."\n") or tostring(v))
  end
  table.insert(sb, "\n|}")
  return table.concat(sb)
end

local function processLoc(data, loc, keys)
  local v
  for i, k in ipairs(keys) do
    v = remove(data, k:lower())
    if loc[k] == nil then loc[k] = v end
  end
end

local function printUnusedEntries(data, loc, groups)
  local sb = {}
  for k, t in pairs(groups) do
    table.insert(sb, "\n=")
    table.insert(sb, k)
    table.insert(sb, "=\n")
    table.insert(sb, makeWikiTable(t))
  end
  table.insert(sb, "\n=Misc Text=\n")
  table.insert(sb, makeWikiTable(loc))
  table.insert(sb, "\n=Misc Data=\n")
  table.insert(sb, makeWikiTable(data))
  return table.concat(sb)
end

-- Page Functions

local p = {}

local pageFuncs = {
  Item    = require('Module:Page/Item')._main,
  Gear    = require('Module:Page/Gear')._main,
  Memento = require('Module:Page/Memento')._main,
  Job = require('Module:Page/Job')._main,
}

function pageFuncs.Unknown(iname, args)
  return makeWikiTable(args)
end

--[[
{[#invoke:Page
  | Memento
  | TS_WADA_TAMAMO_01
]}
]]
function p.page(frame)
  local pageType  = mw.text.trim(frame.args[1])
  local iname = mw.text.trim(frame.args[2] or "")
  for k, v in pairs(frame:getParent().args) do
    frame.args[k] = v
  end
  frame:getParent().args = frame.args
  local args = require('Module:Arguments').getArgs(frame)
  if args.isPage then
		require('Module:CargoUtil').store{
			_table = 'Pages',
			type = pageType,
			iname = iname,
		}
	end
  return pageFuncs[pageType](iname, args)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {parentFirst = true})
	local pageType = args.type
	local iname = mw.text.trim(args[1])
	if args.isPage then
		require('Module:CargoUtil').store{
			_table = 'Pages',
			type = pageType,
			iname = iname,
		}
		local renderPage = require('Module:Page/'..pageType).renderPage
		-- I shouldn't have to do this...
		if renderPage then
			return renderPage(iname)
		end
	end
	-- Eww
	return pageFuncs[pageType](iname, args)
end

return p