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

Z Wikipedii, wolnej encyklopedii

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

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

local function loadArg(frame,id)
	local result = false
	local pf = frame:getParent()
	if pf then
		result = pf.args[id]
		mw.logObject(result, "PARENT arg["..id.."]")
	end
	
	if not result then
		result = frame.args[id]
		mw.logObject(result, "FRAME arg["..id.."]")
	end

	return (result and (#result ~= 0)) and result or nil
end
 
local function loadArgs(frame, custom)

	local parseBool = function(arg)
		if arg == moduleData.boolYes then
			mw.logObject(arg, "parseBool->true")
			return true
		elseif arg == moduleData.boolNo then
			mw.logObject(arg, "parseBool->false")
			return false
		end
		mw.logObject(arg, "parseBool->unknown")
	end
 
	local parseSeparator = function(arg)
		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
			return "</li><li>", "</li><li>", "<ul><li>", "</li></ul>"
		elseif arg == moduleData.separatorOrdered then
			return "</li><li>", "</li><li>", "<ol><li>", "</li></ol>"
		elseif arg == moduleData.separatorOptionalBullet then
			return "</li><li>", "</li><li>", "<ul><li>", "</li></ul>", true
		elseif arg == moduleData.separatorOptionalOrdered then
			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, "initial OPTIONS 1")
	mw.logObject(custom, "custom OPTIONS")
	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, "initial OPTIONS 2")
 
	-- global defaults
	result.separator = result.separator or moduleData.defaultSeparator
	result.lastSeparator = result.lastSeparator or moduleData.defaultSeparator
	result.linkCoordinates = (result.linkCoordinates == nil) and moduleData.defaultLinkCoordinates or result.linkCoordinates
	result.linkDate = (result.linkDate == nil) and moduleData.defaultLinkDate or result.linkDate
	result.linkItem = (result.linkItem == nil) and moduleData.defaultLinkItem or result.linkItem
	result.separatorBefore = result.separatorBefore or ""
	result.separatorAfter = result.separatorAfter or ""

	return result
end

local function listToText(items, options)
	if (#items == 0) then
		return
	end
	
	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]
	if not processor then
		local pinfo = mw.wikibase.getEntity(pid)
		if pinfo then
			processor = pinfo and moduleData.processor[2][pinfo.datatype]
		end
		processor = processor or moduleData.processor[1]
	end
	
	mw.logObject(#processor > 0 and processor or moduleData.processor[1], "module name")
	local module = require(#processor > 0 and processor or moduleData.processor[1])
	mw.logObject(module, "module")
	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

	mw.logObject(options, "OPTIONS")
	return runner(module, properties, options)
end,

}