Module:Util/pages/dpl

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search

Lua error at line 25: attempt to index field 'strings' (a nil value).


local util = {
	str = {
		endsWith = require("Module:Util/strings/endsWith"),
		split = require("Module:Util/strings/split"),
		trim = require("Module:Util/strings/trim"),
	}
}

local SEPARATOR = "#"
local function dpl(args)
	local dplArgs = ""
	for k, v in pairs(args) do
		if k == "format" or type(v) == "table" and v.value == "format" then
			mw.addWarning("<code>format</code> argument cannot be used here. Format the resulting Lua table instead.")
		elseif type(v) == "table" then
			for _, andedValue in ipairs(v) do
				dplArgs = dplArgs .. appendArg(k, andedValue)
			end
		else
			dplArgs = dplArgs .. appendArg(k, v)
		end
	end
	dplArgs = dplArgs .. appendArg("format", SEPARATOR..",,%PAGE%" .. SEPARATOR .. ",")
	local result = mw.getCurrentFrame():preprocess("{{#dpl:" .. dplArgs .. "}}")
	if not util.strings.endsWith(result, SEPARATOR) then
		return {}
	end
	result = string.gsub(result, SEPARATOR .. ":", SEPARATOR) -- strip : prefix from Category results
	result = util.strings.trim(result, SEPARATOR)
	result = util.strings.split(result, SEPARATOR)
	return result
end

function appendArg(param, value)
	value = tostring(value)
	value = string.gsub(value, "\|", "{{!}}")
	if param and value then
		return "|" .. param .. "=" .. value .. "\n"
	else
		return ""
	end
end

return dpl