Moduł:Wikidane/format/qualifiers

Z Wikibooks, biblioteki wolnych podręczników.
 Dokumentacja modułu [zobacz] [edytuj] [historia] [odśwież]
Pomocniczy moduł zawierający standardowe funkcje agregujące do formatowania kwalifikatorów.
local format = require("Moduł:Wikidane/format/snak").format

local function SELECT(prop, options, name, prefix, separator, lastSeparator)
	if prop.qualifiers and prop.qualifiers[name] then
		local items = {}
		for _, v in ipairs(prop.qualifiers[name]) do
			if v.snaktype == "value" then
				table.insert(items, format(v, options))
			end
		end

		if #items > 0 then
			return prefix..mw.text.listToText(items, separator, lastSeparator)
		end
	end
end

return {
	
wikiUL = function(prop, options, name)
	return SELECT(prop, options, name, "\n*", "\n*", "\n*")
end,

wikiOL = function(prop, options, name)
	return SELECT(prop, options, name, "\n#", "\n#", "\n#")
end,

BR = function(prop, options, name)
	return SELECT(prop, options, name, "", "<br />", "<br />")
end,

AND = function(prop, options, name)
	return SELECT(prop, options, name, "", ", ", " i\194\160")
end,

OR = function(prop, options, name)
	return SELECT(prop, options, name, "", ", ", " lub\194\160")
end,

MINTIME = function(prop, options, name)
	if prop.qualifiers and prop.qualifiers[name] then
		-- select minimum date
		local time = false
		local novalue = false
		local somevalue = false
		for _, v in ipairs(prop.qualifiers[name]) do
			if v.snaktype == "value" then
				if not time or (v.datavalue.value.time < time.datavalue.value.time) then
					time = v
				end
			elseif v.snaktype == "novalue" then
				novalue = v
			elseif v.snaktype == "somevalue" then
				somevalue = v
			end
		end

		local result = time or novalue or somevalue
		if result then
			return options.naked == true and result or format(result, options)
		end
	end
end,

MAXTIME = function(prop, options, name)
	if prop.qualifiers and prop.qualifiers[name] then
		-- select maximum date
		local time = false
		local novalue = false
		local somevalue = false
		for _, v in ipairs(prop.qualifiers[name]) do
			if v.snaktype == "value" then
				if not time or (time.datavalue.value.time < v.datavalue.value.time) then
					time = v
				end
			elseif v.snaktype == "novalue" then
				novalue = v
			elseif v.snaktype == "somevalue" then
				somevalue = v
			end
		end

		local result = time or novalue or somevalue
		if result then
			return options.naked == true and result or format(result, options)
		end
	end
end,

TEXT1 = function(prop, langs, name)
	if prop.qualifiers and prop.qualifiers[name] then
		-- use pl as default if none is given
		if not langs or (#langs == 0) then
			langs = { "pl" }
		end
		
		-- list first occurance of all languages
		local texts = {}
		for _, v in ipairs(prop.qualifiers[name]) do
			if (v.snaktype == "value") and (v.datatype == "monolingualtext") and v.datavalue and (v.datavalue.type == "monolingualtext") and v.datavalue.value then
				local language = v.datavalue.value.language
				local text = v.datavalue.value.text
				if language and text and (#text > 0) and not texts[language] then
					texts[language] = text
				end
			end
		end
	
		-- return first available language
		for _, v in ipairs(langs) do
			if texts[v] then
				return texts[v]
			end
		end
	end
end,

}