Module:Page

From The Alchemist Code Wiki
Revision as of 16:32, 8 February 2021 by BryghtShadow (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

TODO


local p = {}
local h = {}

-- Page Functions
local pageFuncs = {
	Item = 'Module:Page/Item',
	Gear = 'Module:Page/Gear',
	Job = 'Module:Page/Job',
	Memento = 'Module:Page/Memento',
}

--[[
{[#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 require('Module:Page/'..pageType)._main(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
	-- TODO: Ewwwwww
	local moduleName = pageFuncs[pageType]
	if moduleName then
		return require(moduleName)._main(iname, args)
	else
		return h.makeWikiTable(args)
	end
end

-- Utility

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

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

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

function h.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

function h.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

function h.makeWikiTable(t)
	local array = h.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
		if type(v) == "table" then
			table.insert(sb, "\n"..h.makeWikiTable(v).."\n")
		else
			table.insert(sb, tostring(v))
		end
	end
	table.insert(sb, "\n|}")
	return table.concat(sb)
end

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

function h.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, h.makeWikiTable(t))
	end
	table.insert(sb, "\n==Misc Text==\n")
	table.insert(sb, h.makeWikiTable(loc))
	table.insert(sb, "\n==Misc Data==\n")
	table.insert(sb, h.makeWikiTable(data))
	return table.concat(sb)
end

return p