Moduł:Brudnopis/Paweł Ziemian/Wikidane/P

Z Wikipedii, wolnej encyklopedii

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Brudnopis/Paweł Ziemian/Wikidane/P/opis

local moduleData = mw.loadData("Module:Wikidane/data")

local function loadArg(frame,id)
	local f = frame:getParent() or frame
	local result = f.args[id]
	mw.logObject(result, id)
	return (result and (#result ~= 0)) and result or nil
end
 
local function loadArgs(frame, custom)

	mw.logObject(frame.args, "frame.args")
	mw.logObject(frame:getParent().args, "parent.args")

	local parseBool = function(arg)
		if arg == moduleData.boolYes then
			return true
		elseif arg == moduleData.boolNo then
			return false
		end
	end
 
	local parseSeparator = function(arg)
		mw.log("parseSeparator: "..(arg or "NIL"))
		if arg == moduleData.separatorAnd then
			return moduleData.defaultSeparator, moduleData.defaultLastAndSeparator
		elseif arg == moduleData.separatorOr then
			return moduleData.defaultSeparator, moduleData.defaultLastOrSeparator
		elseif arg == moduleData.separatorBullet then
			mw.log("bullet list")
			return "</li><li>", "</li><li>", "<ul><li>", "</li></ul>"
		elseif arg == moduleData.separatorOrdered then
			mw.log("ordered list")
			return "</li><li>", "</li><li>", "<ol><li>", "</li></ol>"
		elseif arg == moduleData.separatorOptionalBullet then
			mw.log("optional bullet list")
			return "</li><li>", "</li><li>", "<ul><li>", "</li></ul>", true
		elseif arg == moduleData.separatorOptionalOrdered then
			mw.log("optional ordered list")
			return "</li><li>", "</li><li>", "<ol><li>", "</li></ol>", true
		elseif arg == moduleData.separatorOr then
			return moduleData.defaultSeparator, moduleData.defaultLastOrSeparator
		elseif arg ~= nil then -- custom
			return arg, arg
		end
	end
 
	local link = parseBool(loadArg(frame, moduleData.argLink))
	local sep, lastSep, before, after, optional = parseSeparator(loadArg(frame, moduleData.argSeparator))
 
	local result = {
		linkCoordinates = link,
		linkDate = link,
		linkItem = link,
		separator = sep,
		lastSeparator = lastSep,
		separatorBefore = before,
		separatorAfter = after,
		separatorOptional = optional,
		novalue = loadArg(frame, moduleData.argNoValue),
		format = loadArg(frame, moduleData.argFormat),
	}
	
	mw.logObject(result, "opcje A")
 
	if custom then
		for _, v in ipairs(custom) do
			if result[v] == nil then
				result[v] = loadArg(frame, v)
			end
		end
 
		if custom.default then
			for k, v in pairs(custom.default) do
				if result[k] == nil then
					result[k] = v
				end
			end
		end
	end
 
	mw.logObject(result, "opcje B")
	
	-- global defaults
	result.separator = result.separator or moduleData.defaultSeparator
	result.lastSeparator = result.lastSeparator or moduleData.defaultSeparator
	result.linkCoordinates = result.linkCoordinates or moduleData.defaultLinkCoordinates
	result.linkDate = result.linkDate or moduleData.defaultLinkDate
	result.linkItem = result.linkItem or moduleData.defaultLinkItem
	result.separatorBefore = result.separatorBefore or ""
	result.separatorAfter = result.separatorAfter or ""
 
	mw.logObject(result, "opcje C")
	return result
end

local function listToText(items, options)
	if (#items == 1) and options.separatorOptional then
		return items[1]
	end
	
	return options.separatorBefore..mw.text.listToText(items, options.separator, options.lastSeparator)..options.separatorAfter
end

local runners = {
 
	["snak"] = function (processor, properties, options)
		local result = {}
 
		for i, p in ipairs(properties) do
			if (p.type == "statement") and p.mainsnak then
				local s = processor.format(p.mainsnak, options)
				if s and (#s > 0) then
					table.insert(result, s)
				end
			end
		end
 
		return listToText(result, options)
	end,
 
	["prop"] = function (processor, properties, options)
		local result = {}
 
		for i, p in ipairs(properties) do
			if (p.type == "statement") and p.mainsnak then
				local s = processor.format(p, options)
				if s and (#s > 0) then
					table.insert(result, s)
				end
			end
		end
 
		return listToText(result, options)
	end,
 
	["props"] = function (processor, properties, options)
		return processor.format(properties, options)
	end,
}

return {
	
run = function(frame, pid, properties)
	local processor = frame.args.procesor or frame:getParent().args.procesor or moduleData.processor[pid] or moduleData.processor[1]
	local module = require(#processor > 0 and processor or moduleData.processor[1])
	local runner = runners[module.scope]
	if not runner then
		return
	end

	local options
	if module.options == nil then
		options = loadArgs(frame)
	elseif type(module.options) == "table" then
		options = loadArgs(frame, module.options)
	elseif type(module.options) == "function" then
		options = module.options(frame)
	else
		options = {}
	end

	return runner(module, properties, options)
end,

}